StyleCollection

Inheritance: java.lang.Object

All Implemented Interfaces: java.lang.Cloneable, java.lang.Iterable

public class StyleCollection implements Cloneable, Iterable

A collection of 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");
 

Methods

MethodDescription
add(int type, String name)
addCopy(Style style)Copies a style into this collection.
clearQuickStyleGallery()Removes all styles from the Quick Style Gallery panel.
get(int index)Gets a style by index.
get(String name)Retrieves a style from the collection.
getByStyleIdentifier(int sti)
getCount()Gets the number of styles in the collection.
getDefaultFont()Gets document default text formatting.
getDefaultParagraphFormat()Gets document default paragraph formatting.
getDocument()Gets the owner document.
iterator()Gets an enumerator object that will enumerate styles in the alphabetical order of their names.

add(int type, String name)

public Style add(int type, String name)

Parameters:

ParameterTypeDescription
typeint
namejava.lang.String

Returns: Style

addCopy(Style style)

public Style addCopy(Style style)

Copies a style into this collection.

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());
 

Parameters:

ParameterTypeDescription
styleStyleStyle to be copied.

Returns: Style - Copied style ready for usage.

clearQuickStyleGallery()

public void clearQuickStyleGallery()

Removes all styles from the Quick Style Gallery panel.

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");
 

get(int index)

public Style get(int index)

Gets a style by 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());
 

Parameters:

ParameterTypeDescription
indexint

Returns: Style - A style by index.

get(String name)

public Style get(String name)

Retrieves a style from the collection. Gets a style by name or alias.

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");
 

Parameters:

ParameterTypeDescription
namejava.lang.String

Returns: Style - The corresponding Style value.

getByStyleIdentifier(int sti)

public Style getByStyleIdentifier(int sti)

Parameters:

ParameterTypeDescription
stiint

Returns: Style

getCount()

public int getCount()

Gets the number of styles in the collection.

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());
 

Returns: int - The number of styles in the collection.

getDefaultFont()

public Font getDefaultFont()

Gets document default text formatting.

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());
 

Returns: Font - Document default text formatting.

getDefaultParagraphFormat()

public ParagraphFormat getDefaultParagraphFormat()

Gets document default paragraph formatting.

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());
 

Returns: ParagraphFormat - Document default paragraph formatting.

getDocument()

public DocumentBase getDocument()

Gets the owner document.

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

Returns: DocumentBase - The owner document.

iterator()

public Iterator iterator()

Gets an enumerator object that will enumerate styles in the alphabetical order of their names.

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

Returns: java.util.Iterator