public class StyleCollection
extends java.lang.Object
implements java.lang.Cloneable, java.lang.Iterable
Style objects that represent both the built-in and user-defined styles in a document.
To learn more, visit the Working with Styles and Themes documentation article.
Examples:
Shows how to create and use a paragraph style with list formatting.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a custom paragraph style.
Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle1");
style.getFont().setSize(24.0);
style.getFont().setName("Verdana");
style.getParagraphFormat().setSpaceAfter(12.0);
// Create a list and make sure the paragraphs that use this style will use this list.
style.getListFormat().setList(doc.getLists().add(ListTemplate.BULLET_DEFAULT));
style.getListFormat().setListLevelNumber(0);
// Apply the paragraph style to the document builder's current paragraph, and then add some text.
builder.getParagraphFormat().setStyle(style);
builder.writeln("Hello World: MyStyle1, bulleted list.");
// Change the document builder's style to one that has no list formatting and write another paragraph.
builder.getParagraphFormat().setStyle(doc.getStyles().get("Normal"));
builder.writeln("Hello World: Normal.");
builder.getDocument().save(getArtifactsDir() + "Styles.ParagraphStyleBulletedList.docx");
| Modifier and Type | Method and Description |
|---|---|
Style |
add(int type,
java.lang.String name) |
Style |
addCopy(Style style)
Copies a style into this collection.
|
void |
clearQuickStyleGallery()
Removes all styles from the Quick Style Gallery panel.
|
Style |
get(int index)
Gets a style by index.
|
Style |
get(java.lang.String name)
|
Style |
getByStyleIdentifier(int sti) |
int |
getCount()
Gets the number of styles in the collection.
|
Font |
getDefaultFont()
Gets document default text formatting.
|
ParagraphFormat |
getDefaultParagraphFormat()
Gets document default paragraph formatting.
|
DocumentBase |
getDocument()
Gets the owner document.
|
java.util.Iterator |
iterator()
Gets an enumerator object that will enumerate styles in the alphabetical order of their names.
|
protected java.lang.Object |
memberwiseClone() |
public void clearQuickStyleGallery()
Examples:
Shows how to remove styles from Style Gallery panel.
Document doc = new Document();
// Note that remove styles work only with DOCX format for now.
doc.getStyles().clearQuickStyleGallery();
doc.save(getArtifactsDir() + "Styles.RemoveStylesFromStyleGallery.docx");
public DocumentBase getDocument()
Examples:
Shows how to access a document's style collection.
Document doc = new Document();
Assert.assertEquals(4, doc.getStyles().getCount());
// Enumerate and list all the styles that a document created using Aspose.Words contains by default.
Iterator<Style> stylesEnum = doc.getStyles().iterator();
while (stylesEnum.hasNext()) {
Style curStyle = stylesEnum.next();
System.out.println(MessageFormat.format("Style name:\t\"{0}\", of type \"{1}\"", curStyle.getName(), curStyle.getType()));
System.out.println(MessageFormat.format("\tSubsequent style:\t{0}", curStyle.getNextParagraphStyleName()));
System.out.println(MessageFormat.format("\tIs heading:\t\t\t{0}", curStyle.isHeading()));
System.out.println(MessageFormat.format("\tIs QuickStyle:\t\t{0}", curStyle.isQuickStyle()));
Assert.assertEquals(curStyle.getDocument(), doc);
}
public Font getDefaultFont()
Remarks:
Note that document-wide defaults were introduced in Microsoft Word 2007 and are fully supported in OOXML formats ( LoadFormat.DOCX) only. Earlier document formats have limited support for this feature and only font names can be stored.
Examples:
Shows how to add a Style to a document's styles collection.
Document doc = new Document();
// Set default parameters for new styles that we may later add to this collection.
StyleCollection styles = doc.getStyles();
styles.getDefaultFont().setName("Courier New");
// If we add a style of the "StyleType.Paragraph", the collection will apply the values of
// its "DefaultParagraphFormat" property to the style's "ParagraphFormat" property.
styles.getDefaultParagraphFormat().setFirstLineIndent(15.0);
// Add a style, and then verify that it has the default settings.
styles.add(StyleType.PARAGRAPH, "MyStyle");
Assert.assertEquals("Courier New", styles.get(4).getFont().getName());
Assert.assertEquals(15.0, styles.get("MyStyle").getParagraphFormat().getFirstLineIndent());
public ParagraphFormat getDefaultParagraphFormat()
Remarks:
Note that document-wide defaults were introduced in Microsoft Word 2007 and are fully supported in OOXML formats ( LoadFormat.DOCX) only. Earlier document formats have no support for document default paragraph formatting.
Examples:
Shows how to add a Style to a document's styles collection.
Document doc = new Document();
// Set default parameters for new styles that we may later add to this collection.
StyleCollection styles = doc.getStyles();
styles.getDefaultFont().setName("Courier New");
// If we add a style of the "StyleType.Paragraph", the collection will apply the values of
// its "DefaultParagraphFormat" property to the style's "ParagraphFormat" property.
styles.getDefaultParagraphFormat().setFirstLineIndent(15.0);
// Add a style, and then verify that it has the default settings.
styles.add(StyleType.PARAGRAPH, "MyStyle");
Assert.assertEquals("Courier New", styles.get(4).getFont().getName());
Assert.assertEquals(15.0, styles.get("MyStyle").getParagraphFormat().getFirstLineIndent());
public int getCount()
Examples:
Shows how to add a Style to a document's styles collection.
Document doc = new Document();
// Set default parameters for new styles that we may later add to this collection.
StyleCollection styles = doc.getStyles();
styles.getDefaultFont().setName("Courier New");
// If we add a style of the "StyleType.Paragraph", the collection will apply the values of
// its "DefaultParagraphFormat" property to the style's "ParagraphFormat" property.
styles.getDefaultParagraphFormat().setFirstLineIndent(15.0);
// Add a style, and then verify that it has the default settings.
styles.add(StyleType.PARAGRAPH, "MyStyle");
Assert.assertEquals("Courier New", styles.get(4).getFont().getName());
Assert.assertEquals(15.0, styles.get("MyStyle").getParagraphFormat().getFirstLineIndent());
public Style get(java.lang.String name)
Remarks:
Case sensitive, returns null if the style with the given name is not found.
If this is an English name of a built in style that does not yet exist, automatically creates it.
Examples:
Shows when to recalculate the page layout of the document.
Document doc = new Document(getMyDir() + "Rendering.docx");
// Saving a document to PDF, to an image, or printing for the first time will automatically
// cache the layout of the document within its pages.
doc.save(getArtifactsDir() + "Document.UpdatePageLayout.1.pdf");
// Modify the document in some way.
doc.getStyles().get("Normal").getFont().setSize(6.0);
doc.getSections().get(0).getPageSetup().setOrientation(Orientation.LANDSCAPE);
doc.getSections().get(0).getPageSetup().setMargins(Margins.MIRRORED);
// In the current version of Aspose.Words, modifying the document does not automatically rebuild
// the cached page layout. If we wish for the cached layout
// to stay up to date, we will need to update it manually.
doc.updatePageLayout();
doc.save(getArtifactsDir() + "Document.UpdatePageLayout.2.pdf");
Style value.public Style getByStyleIdentifier(int sti)
public Style get(int index)
Examples:
Shows how to add a Style to a document's styles collection.
Document doc = new Document();
// Set default parameters for new styles that we may later add to this collection.
StyleCollection styles = doc.getStyles();
styles.getDefaultFont().setName("Courier New");
// If we add a style of the "StyleType.Paragraph", the collection will apply the values of
// its "DefaultParagraphFormat" property to the style's "ParagraphFormat" property.
styles.getDefaultParagraphFormat().setFirstLineIndent(15.0);
// Add a style, and then verify that it has the default settings.
styles.add(StyleType.PARAGRAPH, "MyStyle");
Assert.assertEquals("Courier New", styles.get(4).getFont().getName());
Assert.assertEquals(15.0, styles.get("MyStyle").getParagraphFormat().getFirstLineIndent());
public java.util.Iterator iterator()
Examples:
Shows how to access a document's style collection.
Document doc = new Document();
Assert.assertEquals(4, doc.getStyles().getCount());
// Enumerate and list all the styles that a document created using Aspose.Words contains by default.
Iterator<Style> stylesEnum = doc.getStyles().iterator();
while (stylesEnum.hasNext()) {
Style curStyle = stylesEnum.next();
System.out.println(MessageFormat.format("Style name:\t\"{0}\", of type \"{1}\"", curStyle.getName(), curStyle.getType()));
System.out.println(MessageFormat.format("\tSubsequent style:\t{0}", curStyle.getNextParagraphStyleName()));
System.out.println(MessageFormat.format("\tIs heading:\t\t\t{0}", curStyle.isHeading()));
System.out.println(MessageFormat.format("\tIs QuickStyle:\t\t{0}", curStyle.isQuickStyle()));
Assert.assertEquals(curStyle.getDocument(), doc);
}
iterator in interface java.lang.Iterablepublic Style add(int type, java.lang.String name)
public Style addCopy(Style style)
Remarks:
Style to be copied can belong to the same document as well as to different document.
Linked style is copied.
This method does doesn't copy base styles.
If collection already contains a style with the same name, then new name is automatically generated by adding "_number" suffix starting from 0 e.g. "Normal_0", "Heading 1_1" etc. Use Style.getName() / Style.setName(java.lang.String) setter for changing the name of the imported style.
Examples:
Shows how to import a style from one document into a different document.
Document srcDoc = new Document();
// Create a custom style for the source document.
Style srcStyle = srcDoc.getStyles().add(StyleType.PARAGRAPH, "MyStyle");
srcStyle.getFont().setColor(Color.RED);
// Import the source document's custom style into the destination document.
Document dstDoc = new Document();
Style newStyle = dstDoc.getStyles().addCopy(srcStyle);
// The imported style has an appearance identical to its source style.
Assert.assertEquals("MyStyle", newStyle.getName());
Assert.assertEquals(Color.RED.getRGB(), newStyle.getFont().getColor().getRGB());
Shows how to clone a document's style.
Document doc = new Document();
// The AddCopy method creates a copy of the specified style and
// automatically generates a new name for the style, such as "Heading 1_0".
Style newStyle = doc.getStyles().addCopy(doc.getStyles().get("Heading 1"));
// Use the style's "Name" property to change the style's identifying name.
newStyle.setName("My Heading 1");
// Our document now has two identical looking styles with different names.
// Changing settings of one of the styles do not affect the other.
newStyle.getFont().setColor(Color.RED);
Assert.assertEquals("My Heading 1", newStyle.getName());
Assert.assertEquals("Heading 1", doc.getStyles().get("Heading 1").getName());
Assert.assertEquals(doc.getStyles().get("Heading 1").getType(), newStyle.getType());
Assert.assertEquals(doc.getStyles().get("Heading 1").getFont().getName(), newStyle.getFont().getName());
Assert.assertEquals(doc.getStyles().get("Heading 1").getFont().getSize(), newStyle.getFont().getSize());
Assert.assertNotEquals(doc.getStyles().get("Heading 1").getFont().getColor(), newStyle.getFont().getColor());
style - Style to be copied.protected java.lang.Object memberwiseClone()