com.aspose.words
Class StyleCollection

java.lang.Object
    extended by 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 paragraph style and specify some formatting for it.
Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle1");
style.getFont().setSize(24);
style.getFont().setName("Verdana");
style.getParagraphFormat().setSpaceAfter(12);

// 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 current paragraph in the document and add some text.
builder.getParagraphFormat().setStyle(style);
builder.writeln("Hello World: MyStyle1, bulleted.");

// Change to a paragraph style that has no list formatting.
builder.getParagraphFormat().setStyle(doc.getStyles().get("Normal"));
builder.writeln("Hello World: Normal.");

builder.getDocument().save(getMyDir() + "Lists.ParagraphStyleBulleted Out.doc");

Property Getters/Setters Summary
intgetCount()
           Gets the number of styles in the collection.
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.
java.util.Iteratoriterator()
           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.

getDocument

public DocumentBase getDocument()
Gets the owner document.

get

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

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 request page layout of the document to be recalculated.
Document doc = new Document(getMyDir() + "Rendering.doc");

// Saving a document to PDF or to image or printing for the first time will automatically
// layout document pages and this information will be cached inside the document.
doc.save(getMyDir() + "Rendering.UpdatePageLayout1 Out.pdf");

// Modify the document in any way.
doc.getStyles().get("Normal").getFont().setSize(6);
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 you want to save to PDF or render a modified document again,
// you need to manually request page layout to be updated.
doc.updatePageLayout();

doc.save(getMyDir() + "Rendering.UpdatePageLayout2 Out.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.

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

// Create a new list style.
// List formatting associated with this list style is default numbered.
Style listStyle = doc.getStyles().add(StyleType.LIST, "MyListStyle");

// This list defines the formatting of the list style.
// Note this list can not be used directly to apply formatting to paragraphs (see below).
List list1 = listStyle.getList();

// Check some basic rules about the list that defines a list style.
System.out.println("IsListStyle: " + list1.isListStyleDefinition()); // Will be true
System.out.println("IsListStyleReference: " + list1.isListStyleReference()); // Will be false
System.out.println("IsMultiLevel: " + list1.isMultiLevel()); // Will be true
System.out.println("List style has been set: " + (listStyle == list1.getStyle())); // Are equal

// Modify formatting of the list style to our liking.
for (int i = 0; i < list1.getListLevels().getCount(); i++)
{
    ListLevel level = list1.getListLevels().get(i);
    level.getFont().setName("Verdana");
    level.getFont().setColor(Color.BLUE);
    level.getFont().setBold(true);
}


// Add some text to our document and use the list style.
DocumentBuilder builder = new DocumentBuilder(doc);

builder.writeln("Using list style first time:");

// This creates a list based on the list style.
List list2 = doc.getLists().add(listStyle);

// Check some basic rules about the list that references a list style.
System.out.println("IsListStyleDefinition: " + list2.isListStyleDefinition()); // Will be false
System.out.println("IsListStyleReference: " + list2.isListStyleReference()); // Will be true
System.out.println("List Style has been set: " + (listStyle == list2.getStyle())); // Are equal

// Apply the list that references the list style.
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(getMyDir() + "Lists.CreateAndUseListStyle Out.doc");

addCopy

public Style addCopy(Style style)
             throws java.lang.Exception
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:

Demonstrates how to copy a style from one document to another and overide an existing style in the destination document.
// Change the font of the heading style to red in the source document.
Style srcStyle = srcDoc.getStyles().get("Heading 1");
srcStyle.getFont().setColor(Color.RED);

// The AddCopy method can be used to copy a style from a different document.
Style newStyle = dstDoc.getStyles().addCopy(srcStyle);

// The name of the new style can be changed to the name of any existing style. Doing this will override the existing style.
newStyle.setName("Heading 1");

Example:

Demonstrates how to copy a style within the same 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"));

// You can change the new style name if required as the Style.Name property is read-write.
newStyle.setName("My Heading 1");

Example:

Demonstrates how to copy style from one document into a different document.
// Change the font of the heading style to red in the source document.
Style srcStyle = srcDoc.getStyles().get("Heading 1");
srcStyle.getFont().setColor(Color.RED);

// The AddCopy method can be used to copy a style from a different document.
Style newStyle = dstDoc.getStyles().addCopy(srcStyle);

iterator

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

See Also:
          Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
          Aspose.Words Support Forum - our preferred method of support.