com.aspose.words

Class StyleCollection

  • java.lang.Object
    • com.aspose.words.StyleCollection
  • All Implemented Interfaces:
    java.lang.Cloneable, java.lang.Iterable
    public class StyleCollection 
    extends java.lang.Object

A collection of Style objects that represent both the built-in and user-defined styles in a document.

Example:

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

Property Getters/Setters Summary
intgetCount()
Gets the number of styles in the collection.
FontgetDefaultFont()
Gets document default text formatting.
ParagraphFormatgetDefaultParagraphFormat()
Gets document default paragraph formatting.
DocumentBasegetDocument()
Gets the owner document.
Styleget(int index)
Gets a style by index.
Styleget(java.lang.String name)
Gets a style by name or alias.
StylegetByStyleIdentifier(int sti)
Gets a built-in style by its locale independent identifier.
 
Method Summary
Styleadd(int type, java.lang.String name)
Creates a new user defined style and adds it the collection.
StyleaddCopy(Style style)
Copies a style into this collection.
voidclearQuickStyleGallery()
Removes all styles from the Quick Style Gallery panel.
java.util.Iterator<Style>iterator()
Gets an enumerator object that will enumerate styles in the alphabetical order of their names.
 

    • Property Getters/Setters Detail

      • getCount

        public int getCount()
        
        Gets the number of styles in the collection.

        Example:

        Shows how to add a Style to a document's styles collection.
        Document doc = new Document();
        StyleCollection styles = doc.getStyles();
        
        // Set default parameters for new styles that we may later add to this collection.
        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());
      • getDefaultFont

        public Font getDefaultFont()
        
        Gets document default text formatting.

        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.

        Example:

        Shows how to add a Style to a document's styles collection.
        Document doc = new Document();
        StyleCollection styles = doc.getStyles();
        
        // Set default parameters for new styles that we may later add to this collection.
        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());
      • getDefaultParagraphFormat

        public ParagraphFormat getDefaultParagraphFormat()
        
        Gets document default paragraph formatting.

        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.

        Example:

        Shows how to add a Style to a document's styles collection.
        Document doc = new Document();
        StyleCollection styles = doc.getStyles();
        
        // Set default parameters for new styles that we may later add to this collection.
        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());
      • getDocument

        public DocumentBase getDocument()
        
        Gets the owner document.

        Example:

        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);
        }
      • get

        public Style get(int index)
        
        Gets a style by index.

        Example:

        Shows how to add a Style to a document's styles collection.
        Document doc = new Document();
        StyleCollection styles = doc.getStyles();
        
        // Set default parameters for new styles that we may later add to this collection.
        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());
      • get

        public Style get(java.lang.String name)
        
        Gets a style by name or alias.

        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.

        Example:

        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(com.aspose.words.Orientation.LANDSCAPE);
        
        // 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");
      • getByStyleIdentifier

        public Style getByStyleIdentifier(int sti)
        
        Gets a built-in style by its locale independent identifier.

        When accessing a style that does not yet exist, automatically creates it.

        Parameters:
        sti - A StyleIdentifier value. A StyleIdentifier value that specifies the built in style to retrieve.

        Example:

        Shows how to add a Style to a document's styles collection.
        Document doc = new Document();
        StyleCollection styles = doc.getStyles();
        
        // Set default parameters for new styles that we may later add to this collection.
        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());
    • Method Detail

      • add

        public Style add(int type, java.lang.String name)
        Creates a new user defined style and adds it the collection.

        You can create character, paragraph or a list style.

        When creating a list style, the style is created with default numbered list formatting (1 \ a \ i).

        Throws an exception if a style with this name already exists.

        Parameters:
        type - A StyleType value that specifies the type of the style to create.
        name - Case sensitive name of the style to create.

        Example:

        Shows how to create a list style and use it in a document.
        Document doc = new Document();
        
        // A list allows us to organize and decorate sets of paragraphs with prefix symbols and indents.
        // We can create nested lists by increasing the indent level. 
        // We can begin and end a list by using a document builder's "ListFormat" property. 
        // Each paragraph that we add between a list's start and the end will become an item in the list.
        // We can contain an entire List object within a style.
        Style listStyle = doc.getStyles().add(StyleType.LIST, "MyListStyle");
        
        List list1 = listStyle.getList();
        
        Assert.assertTrue(list1.isListStyleDefinition());
        Assert.assertFalse(list1.isListStyleReference());
        Assert.assertTrue(list1.isMultiLevel());
        Assert.assertEquals(listStyle, list1.getStyle());
        
        // Change the appearance of all list levels in our list.
        for (ListLevel level : list1.getListLevels()) {
            level.getFont().setName("Verdana");
            level.getFont().setColor(Color.BLUE);
            level.getFont().setBold(true);
        }
        
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        builder.writeln("Using list style first time:");
        
        // Create another list from a list within a style.
        List list2 = doc.getLists().add(listStyle);
        
        Assert.assertFalse(list2.isListStyleDefinition());
        Assert.assertTrue(list2.isListStyleReference());
        Assert.assertEquals(listStyle, list2.getStyle());
        
        // Add some list items that our list will format.
        builder.getListFormat().setList(list2);
        builder.writeln("Item 1");
        builder.writeln("Item 2");
        builder.getListFormat().removeNumbers();
        
        builder.writeln("Using list style second time:");
        
        // Create and apply another list based on the list style.
        List list3 = doc.getLists().add(listStyle);
        builder.getListFormat().setList(list3);
        builder.writeln("Item 1");
        builder.writeln("Item 2");
        builder.getListFormat().removeNumbers();
        
        builder.getDocument().save(getArtifactsDir() + "Lists.CreateAndUseListStyle.docx");

        Example:

        Shows how to add a Style to a document's styles collection.
        Document doc = new Document();
        StyleCollection styles = doc.getStyles();
        
        // Set default parameters for new styles that we may later add to this collection.
        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());
      • addCopy

        public Style addCopy(Style style)
        Copies a style into this collection.

        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.Name setter for changing the name of the imported style.

        Parameters:
        style - Style to be copied.
        Returns:
        Copied style ready for usage.

        Example:

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

        Example:

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

        public void clearQuickStyleGallery()
        Removes all styles from the Quick Style Gallery panel.

        Example:

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

        public java.util.Iterator<Style> iterator()
        Gets an enumerator object that will enumerate styles in the alphabetical order of their names.

        Example:

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