java.lang.Objectcom.aspose.words.StyleCollection
public class StyleCollection
Example:
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 | ||
---|---|---|
int | getCount() | |
Gets the number of styles in the collection. | ||
DocumentBase | getDocument() | |
Gets the owner document. | ||
Style | get(int index) | |
Gets a style by index. | ||
Style | get(java.lang.String name) | |
Gets a style by name or alias. | ||
Style | getByStyleIdentifier(int sti) | |
Gets a built-in style by its locale independent identifier. |
Method Summary | ||
---|---|---|
Style | add(int type, java.lang.String name) | |
Creates a new user defined style and adds it the collection. | ||
java.util.Iterator | iterator() | |
Gets an enumerator object that will enumerate styles in the alphabetical order of their names. |
Property Getters/Setters Detail |
---|
getDocument | |
public DocumentBase getDocument() |
getCount | |
public int getCount() |
get | |
public Style get(java.lang.String name) |
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.
getByStyleIdentifier | |
public Style getByStyleIdentifier(int sti) |
When accessing a style that does not yet exist, automatically creates it.
sti
- A get | |
public Style get(int index) |
Method Detail |
---|
iterator | |
public java.util.Iterator iterator() |
add | |
public Style add(int type, java.lang.String name) throws java.lang.Exception |
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.
type
- A 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. Assert.assertTrue(list1.isListStyleDefinition()); Assert.assertFalse(list1.isListStyleReference()); Assert.assertTrue(list1.isMultiLevel()); Assert.assertEquals(listStyle, list1.getStyle()); // 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. Assert.assertFalse(list2.isListStyleDefinition()); Assert.assertTrue(list2.isListStyleReference()); Assert.assertEquals(listStyle,list2.getStyle()); // 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");