Style
Inheritance: java.lang.Object
All Implemented Interfaces: java.lang.Cloneable
public class Style implements Cloneable
Represents a single built-in or user-defined style.
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");
Shows how to create and apply a custom style.
Document doc = new Document();
Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle");
style.getFont().setName("Times New Roman");
style.getFont().setSize(16.0);
style.getFont().setColor(Color.magenta);
// Automatically redefine style.
style.setAutomaticallyUpdate(true);
DocumentBuilder builder = new DocumentBuilder(doc);
// Apply one of the styles from the document to the paragraph that the document builder is creating.
builder.getParagraphFormat().setStyle(doc.getStyles().get("MyStyle"));
builder.writeln("Hello world!");
Style firstParagraphStyle = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().getStyle();
Assert.assertEquals(style, firstParagraphStyle);
// Remove our custom style from the document's styles collection.
doc.getStyles().get("MyStyle").remove();
firstParagraphStyle = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().getStyle();
// Any text that used a removed style reverts to the default formatting.
Assert.assertFalse(IterableUtils.matchesAny(doc.getStyles(), s -> s.getName() == "MyStyle"));
Assert.assertEquals("Times New Roman", firstParagraphStyle.getFont().getName());
Assert.assertEquals(12.0d, firstParagraphStyle.getFont().getSize());
Assert.assertEquals(0, firstParagraphStyle.getFont().getColor().getRGB());
Methods
Method | Description |
---|---|
clearParaAttrs() | |
clearRunAttrs() | |
equals(Style style) | Compares with the specified style. |
fetchInheritedParaAttr(int key) | |
fetchInheritedRunAttr(int key) | |
fetchParaAttr(int key) | |
getAliases() | Gets all aliases of this style. |
getAutomaticallyUpdate() | Specifies whether this style is automatically redefined based on the appropriate value. |
getBaseStyleName() | Gets/sets the name of the style this style is based on. |
getBuiltIn() | True if this style is one of the built-in styles in MS Word. |
getDirectParaAttr(int key) | |
getDirectParaAttr(int key, int revisionsView) | |
getDirectRunAttr(int key) | |
getDirectRunAttr(int key, int revisionsView) | |
getDocument() | Gets the owner document. |
getFont() | Gets the character formatting of the style. |
getLinkedStyleName() | Gets/sets the name of the Style linked to this one. |
getList() | Gets the list that defines formatting of this list style. |
getListFormat() | Provides access to the list formatting properties of a paragraph style. |
getLocked() | Specifies whether this style is locked. |
getName() | Gets the name of the style. |
getNextParagraphStyleName() | Gets/sets the name of the style to be applied automatically to a new paragraph inserted after a paragraph formatted with the specified style. |
getParagraphFormat() | Gets the paragraph formatting of the style. |
getPriority() | Gets/sets the integer value that represents the priority for sorting the styles in the Styles task pane. |
getSemiHidden() | Gets/sets whether the style hides from the Styles gallery and from the Styles task pane. |
getStyleIdentifier() | Gets the locale independent style identifier for a built-in style. |
getStyles() | Gets the collection of styles this style belongs to. |
getType() | Gets the style type (paragraph or character). |
getUnhideWhenUsed() | Gets/sets whether the style used in the current document unhides from the Styles gallery and from the Styles task pane. |
isHeading() | True when the style is one of the built-in Heading styles. |
isQuickStyle() | Specifies whether this style is shown in the Quick Style gallery inside MS Word UI. |
isQuickStyle(boolean value) | Specifies whether this style is shown in the Quick Style gallery inside MS Word UI. |
remove() | Removes the specified style from the document. |
removeParaAttr(int key) | |
removeRunAttr(int key) | |
setAutomaticallyUpdate(boolean value) | Specifies whether this style is automatically redefined based on the appropriate value. |
setBaseStyleName(String value) | Gets/sets the name of the style this style is based on. |
setLinkedStyleName(String value) | Gets/sets the name of the Style linked to this one. |
setLocked(boolean value) | Specifies whether this style is locked. |
setName(String value) | Sets the name of the style. |
setNextParagraphStyleName(String value) | Gets/sets the name of the style to be applied automatically to a new paragraph inserted after a paragraph formatted with the specified style. |
setParaAttr(int key, Object value) | |
setPriority(int value) | Gets/sets the integer value that represents the priority for sorting the styles in the Styles task pane. |
setRunAttr(int key, Object value) | |
setSemiHidden(boolean value) | Gets/sets whether the style hides from the Styles gallery and from the Styles task pane. |
setUnhideWhenUsed(boolean value) | Gets/sets whether the style used in the current document unhides from the Styles gallery and from the Styles task pane. |
clearParaAttrs()
public void clearParaAttrs()
clearRunAttrs()
public void clearRunAttrs()
equals(Style style)
public boolean equals(Style style)
Compares with the specified style. Styles Istds are compared for built-in styles only. Styles defaults are not included in comparison. Base style, linked style and next paragraph style are recursively compared.
Examples:
Shows how to use style aliases.
Document doc = new Document(getMyDir() + "Style with alias.docx");
// This document contains a style named "MyStyle,MyStyle Alias 1,MyStyle Alias 2".
// If a style's name has multiple values separated by commas, each clause is a separate alias.
Style style = doc.getStyles().get("MyStyle");
Assert.assertEquals(new String[]{"MyStyle Alias 1", "MyStyle Alias 2"}, style.getAliases());
Assert.assertEquals("Title", style.getBaseStyleName());
Assert.assertEquals("MyStyle Char", style.getLinkedStyleName());
// We can reference a style using its alias, as well as its name.
Assert.assertEquals(doc.getStyles().get("MyStyle Alias 1"), doc.getStyles().get("MyStyle Alias 2"));
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentEnd();
builder.getParagraphFormat().setStyle(doc.getStyles().get("MyStyle Alias 1"));
builder.writeln("Hello world!");
builder.getParagraphFormat().setStyle(doc.getStyles().get("MyStyle Alias 2"));
builder.write("Hello again!");
Assert.assertEquals(doc.getFirstSection().getBody().getParagraphs().get(0).getParagraphFormat().getStyle(),
doc.getFirstSection().getBody().getParagraphs().get(1).getParagraphFormat().getStyle());
Parameters:
Parameter | Type | Description |
---|---|---|
style | Style |
Returns: boolean
fetchInheritedParaAttr(int key)
public Object fetchInheritedParaAttr(int key)
Parameters:
Parameter | Type | Description |
---|---|---|
key | int |
Returns: java.lang.Object
fetchInheritedRunAttr(int key)
public Object fetchInheritedRunAttr(int key)
Parameters:
Parameter | Type | Description |
---|---|---|
key | int |
Returns: java.lang.Object
fetchParaAttr(int key)
public Object fetchParaAttr(int key)
Parameters:
Parameter | Type | Description |
---|---|---|
key | int |
Returns: java.lang.Object
getAliases()
public String[] getAliases()
Gets all aliases of this style. If style has no aliases then empty array of string is returned.
Examples:
Shows how to use style aliases.
Document doc = new Document(getMyDir() + "Style with alias.docx");
// This document contains a style named "MyStyle,MyStyle Alias 1,MyStyle Alias 2".
// If a style's name has multiple values separated by commas, each clause is a separate alias.
Style style = doc.getStyles().get("MyStyle");
Assert.assertEquals(new String[]{"MyStyle Alias 1", "MyStyle Alias 2"}, style.getAliases());
Assert.assertEquals("Title", style.getBaseStyleName());
Assert.assertEquals("MyStyle Char", style.getLinkedStyleName());
// We can reference a style using its alias, as well as its name.
Assert.assertEquals(doc.getStyles().get("MyStyle Alias 1"), doc.getStyles().get("MyStyle Alias 2"));
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentEnd();
builder.getParagraphFormat().setStyle(doc.getStyles().get("MyStyle Alias 1"));
builder.writeln("Hello world!");
builder.getParagraphFormat().setStyle(doc.getStyles().get("MyStyle Alias 2"));
builder.write("Hello again!");
Assert.assertEquals(doc.getFirstSection().getBody().getParagraphs().get(0).getParagraphFormat().getStyle(),
doc.getFirstSection().getBody().getParagraphs().get(1).getParagraphFormat().getStyle());
Returns: java.lang.String[] - All aliases of this style.
getAutomaticallyUpdate()
public boolean getAutomaticallyUpdate()
Specifies whether this style is automatically redefined based on the appropriate value.
Remarks:
If the property value is set to true, MS Word automatically redefines the current style when the appropriate paragraph formatting has been changed.
AutomaticallyUpdate property is applicable to paragraph styles only.
The default value is false .
Examples:
Shows how to create and apply a custom style.
Document doc = new Document();
Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle");
style.getFont().setName("Times New Roman");
style.getFont().setSize(16.0);
style.getFont().setColor(Color.magenta);
// Automatically redefine style.
style.setAutomaticallyUpdate(true);
DocumentBuilder builder = new DocumentBuilder(doc);
// Apply one of the styles from the document to the paragraph that the document builder is creating.
builder.getParagraphFormat().setStyle(doc.getStyles().get("MyStyle"));
builder.writeln("Hello world!");
Style firstParagraphStyle = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().getStyle();
Assert.assertEquals(style, firstParagraphStyle);
// Remove our custom style from the document's styles collection.
doc.getStyles().get("MyStyle").remove();
firstParagraphStyle = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().getStyle();
// Any text that used a removed style reverts to the default formatting.
Assert.assertFalse(IterableUtils.matchesAny(doc.getStyles(), s -> s.getName() == "MyStyle"));
Assert.assertEquals("Times New Roman", firstParagraphStyle.getFont().getName());
Assert.assertEquals(12.0d, firstParagraphStyle.getFont().getSize());
Assert.assertEquals(0, firstParagraphStyle.getFont().getColor().getRGB());
Returns: boolean - The corresponding boolean value.
getBaseStyleName()
public String getBaseStyleName()
Gets/sets the name of the style this style is based on.
Remarks:
This will be an empty string if the style is not based on any other style and it can be set to an empty string.
Examples:
Shows how to use style aliases.
Document doc = new Document(getMyDir() + "Style with alias.docx");
// This document contains a style named "MyStyle,MyStyle Alias 1,MyStyle Alias 2".
// If a style's name has multiple values separated by commas, each clause is a separate alias.
Style style = doc.getStyles().get("MyStyle");
Assert.assertEquals(new String[]{"MyStyle Alias 1", "MyStyle Alias 2"}, style.getAliases());
Assert.assertEquals("Title", style.getBaseStyleName());
Assert.assertEquals("MyStyle Char", style.getLinkedStyleName());
// We can reference a style using its alias, as well as its name.
Assert.assertEquals(doc.getStyles().get("MyStyle Alias 1"), doc.getStyles().get("MyStyle Alias 2"));
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentEnd();
builder.getParagraphFormat().setStyle(doc.getStyles().get("MyStyle Alias 1"));
builder.writeln("Hello world!");
builder.getParagraphFormat().setStyle(doc.getStyles().get("MyStyle Alias 2"));
builder.write("Hello again!");
Assert.assertEquals(doc.getFirstSection().getBody().getParagraphs().get(0).getParagraphFormat().getStyle(),
doc.getFirstSection().getBody().getParagraphs().get(1).getParagraphFormat().getStyle());
Returns: java.lang.String - The corresponding java.lang.String value.
getBuiltIn()
public boolean getBuiltIn()
True if this style is one of the built-in styles in MS Word.
Examples:
Shows how to differentiate custom styles from built-in styles.
Document doc = new Document();
// When we create a document using Microsoft Word, or programmatically using Aspose.Words,
// the document will come with a collection of styles to apply to its text to modify its appearance.
// We can access these built-in styles via the document's "Styles" collection.
// These styles will all have the "BuiltIn" flag set to "true".
Style style = doc.getStyles().get("Emphasis");
Assert.assertTrue(style.getBuiltIn());
// Create a custom style and add it to the collection.
// Custom styles such as this will have the "BuiltIn" flag set to "false".
style = doc.getStyles().add(StyleType.CHARACTER, "MyStyle");
style.getFont().setColor(Color.RED);
style.getFont().setName("Courier New");
Assert.assertFalse(style.getBuiltIn());
Returns: boolean - The corresponding boolean value.
getDirectParaAttr(int key)
public Object getDirectParaAttr(int key)
Parameters:
Parameter | Type | Description |
---|---|---|
key | int |
Returns: java.lang.Object
getDirectParaAttr(int key, int revisionsView)
public Object getDirectParaAttr(int key, int revisionsView)
Parameters:
Parameter | Type | Description |
---|---|---|
key | int | |
revisionsView | int |
Returns: java.lang.Object
getDirectRunAttr(int key)
public Object getDirectRunAttr(int key)
Parameters:
Parameter | Type | Description |
---|---|---|
key | int |
Returns: java.lang.Object
getDirectRunAttr(int key, int revisionsView)
public Object getDirectRunAttr(int key, int revisionsView)
Parameters:
Parameter | Type | Description |
---|---|---|
key | int | |
revisionsView | int |
Returns: java.lang.Object
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.
getFont()
public Font getFont()
Gets the character formatting of the style.
Remarks:
For list styles this property returns null .
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");
Shows how to create and apply a custom style.
Document doc = new Document();
Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle");
style.getFont().setName("Times New Roman");
style.getFont().setSize(16.0);
style.getFont().setColor(Color.magenta);
// Automatically redefine style.
style.setAutomaticallyUpdate(true);
DocumentBuilder builder = new DocumentBuilder(doc);
// Apply one of the styles from the document to the paragraph that the document builder is creating.
builder.getParagraphFormat().setStyle(doc.getStyles().get("MyStyle"));
builder.writeln("Hello world!");
Style firstParagraphStyle = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().getStyle();
Assert.assertEquals(style, firstParagraphStyle);
// Remove our custom style from the document's styles collection.
doc.getStyles().get("MyStyle").remove();
firstParagraphStyle = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().getStyle();
// Any text that used a removed style reverts to the default formatting.
Assert.assertFalse(IterableUtils.matchesAny(doc.getStyles(), s -> s.getName() == "MyStyle"));
Assert.assertEquals("Times New Roman", firstParagraphStyle.getFont().getName());
Assert.assertEquals(12.0d, firstParagraphStyle.getFont().getSize());
Assert.assertEquals(0, firstParagraphStyle.getFont().getColor().getRGB());
Returns: Font - The character formatting of the style.
getLinkedStyleName()
public String getLinkedStyleName()
Gets/sets the name of the Style linked to this one. Returns empty string if no styles are linked.
Remarks:
It is only allowed to link the paragraph style to the character style and vice versa.
Setting LinkedStyleName for the current style automatically leads to setting LinkedStyleName for the linked style.
Assigning the empty string is equivalent to unlinking the previously linked style.
Examples:
Shows how to link styles among themselves.
Document doc = new Document();
Style styleHeading1 = doc.getStyles().getByStyleIdentifier(StyleIdentifier.HEADING_1);
Style styleHeading1Char = doc.getStyles().add(StyleType.CHARACTER, "Heading 1 Char");
styleHeading1Char.getFont().setName("Verdana");
styleHeading1Char.getFont().setBold(true);
styleHeading1Char.getFont().getBorder().setLineStyle(LineStyle.DOT);
styleHeading1Char.getFont().getBorder().setLineWidth(15.0);
styleHeading1.setLinkedStyleName("Heading 1 Char");
Assert.assertEquals("Heading 1 Char", styleHeading1.getLinkedStyleName());
Assert.assertEquals("Heading 1", styleHeading1Char.getLinkedStyleName());
Shows how to use style aliases.
Document doc = new Document(getMyDir() + "Style with alias.docx");
// This document contains a style named "MyStyle,MyStyle Alias 1,MyStyle Alias 2".
// If a style's name has multiple values separated by commas, each clause is a separate alias.
Style style = doc.getStyles().get("MyStyle");
Assert.assertEquals(new String[]{"MyStyle Alias 1", "MyStyle Alias 2"}, style.getAliases());
Assert.assertEquals("Title", style.getBaseStyleName());
Assert.assertEquals("MyStyle Char", style.getLinkedStyleName());
// We can reference a style using its alias, as well as its name.
Assert.assertEquals(doc.getStyles().get("MyStyle Alias 1"), doc.getStyles().get("MyStyle Alias 2"));
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentEnd();
builder.getParagraphFormat().setStyle(doc.getStyles().get("MyStyle Alias 1"));
builder.writeln("Hello world!");
builder.getParagraphFormat().setStyle(doc.getStyles().get("MyStyle Alias 2"));
builder.write("Hello again!");
Assert.assertEquals(doc.getFirstSection().getBody().getParagraphs().get(0).getParagraphFormat().getStyle(),
doc.getFirstSection().getBody().getParagraphs().get(1).getParagraphFormat().getStyle());
Returns: java.lang.String - The corresponding java.lang.String value.
getList()
public List getList()
Gets the list that defines formatting of this list style.
Remarks:
This property is only valid for list styles. For other style types this property returns null .
Examples:
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");
Returns: List - The list that defines formatting of this list style.
getListFormat()
public ListFormat getListFormat()
Provides access to the list formatting properties of a paragraph style.
Remarks:
This property is only valid for paragraph styles. For other style types this property returns null .
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");
Returns: ListFormat - The corresponding ListFormat value.
getLocked()
public boolean getLocked()
Specifies whether this style is locked.
Examples:
Shows how to lock style.
Document doc = new Document();
Style styleHeading1 = doc.getStyles().getByStyleIdentifier(StyleIdentifier.HEADING_1);
if (!styleHeading1.getLocked())
styleHeading1.setLocked(true);
doc.save(getArtifactsDir() + "Styles.LockStyle.docx");
Returns: boolean - The corresponding boolean value.
getName()
public String getName()
Gets the name of the style.
Remarks:
Can not be empty string.
If there already is a style with such name in the collection, then this style will override it. All affected nodes will reference new style.
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.lang.String - The name of the style.
getNextParagraphStyleName()
public String getNextParagraphStyleName()
Gets/sets the name of the style to be applied automatically to a new paragraph inserted after a paragraph formatted with the specified style.
Remarks:
This property is not used by Aspose.Words. The next paragraph style will only be applied automatically when you edit the document in MS Word.
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.lang.String - The corresponding java.lang.String value.
getParagraphFormat()
public ParagraphFormat getParagraphFormat()
Gets the paragraph formatting of the style.
Remarks:
For character and list styles this property returns null .
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");
Returns: ParagraphFormat - The paragraph formatting of the style.
getPriority()
public int getPriority()
Gets/sets the integer value that represents the priority for sorting the styles in the Styles task pane.
Examples:
Shows how to prioritize and hide a style.
Document doc = new Document();
Style styleTitle = doc.getStyles().getByStyleIdentifier(StyleIdentifier.SUBTITLE);
if (styleTitle.getPriority() == 9)
styleTitle.setPriority(10);
if (!styleTitle.getUnhideWhenUsed())
styleTitle.setUnhideWhenUsed(true);
if (styleTitle.getSemiHidden())
styleTitle.setSemiHidden(true);
doc.save(getArtifactsDir() + "Styles.StylePriority.docx");
Returns: int - The corresponding int value.
getSemiHidden()
public boolean getSemiHidden()
Gets/sets whether the style hides from the Styles gallery and from the Styles task pane.
Examples:
Shows how to prioritize and hide a style.
Document doc = new Document();
Style styleTitle = doc.getStyles().getByStyleIdentifier(StyleIdentifier.SUBTITLE);
if (styleTitle.getPriority() == 9)
styleTitle.setPriority(10);
if (!styleTitle.getUnhideWhenUsed())
styleTitle.setUnhideWhenUsed(true);
if (styleTitle.getSemiHidden())
styleTitle.setSemiHidden(true);
doc.save(getArtifactsDir() + "Styles.StylePriority.docx");
Returns: boolean - The corresponding boolean value.
getStyleIdentifier()
public int getStyleIdentifier()
Gets the locale independent style identifier for a built-in style.
Remarks:
For user defined (custom) styles, this property returns StyleIdentifier.USER.
Examples:
Shows how to modify the position of the right tab stop in TOC related paragraphs.
Document doc = new Document(getMyDir() + "Table of contents.docx");
// Iterate through all paragraphs with TOC result-based styles; this is any style between TOC and TOC9.
for (Paragraph para : (Iterable) doc.getChildNodes(NodeType.PARAGRAPH, true)) {
if (para.getParagraphFormat().getStyle().getStyleIdentifier() >= StyleIdentifier.TOC_1
&& para.getParagraphFormat().getStyle().getStyleIdentifier() <= StyleIdentifier.TOC_9) {
// Get the first tab used in this paragraph, this should be the tab used to align the page numbers.
TabStop tab = para.getParagraphFormat().getTabStops().get(0);
// Replace the first default tab, stop with a custom tab stop.
para.getParagraphFormat().getTabStops().removeByPosition(tab.getPosition());
para.getParagraphFormat().getTabStops().add(tab.getPosition() - 50.0, tab.getAlignment(), tab.getLeader());
}
}
doc.save(getArtifactsDir() + "Styles.ChangeTocsTabStops.docx");
Returns: int - The locale independent style identifier for a built-in style. The returned value is one of StyleIdentifier constants.
getStyles()
public StyleCollection getStyles()
Gets the collection of styles this style belongs to.
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: StyleCollection - The collection of styles this style belongs to.
getType()
public int getType()
Gets the style type (paragraph or character).
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: int - The style type (paragraph or character). The returned value is one of StyleType constants.
getUnhideWhenUsed()
public boolean getUnhideWhenUsed()
Gets/sets whether the style used in the current document unhides from the Styles gallery and from the Styles task pane. True when the used style should be shown in the Styles gallery.
Examples:
Shows how to prioritize and hide a style.
Document doc = new Document();
Style styleTitle = doc.getStyles().getByStyleIdentifier(StyleIdentifier.SUBTITLE);
if (styleTitle.getPriority() == 9)
styleTitle.setPriority(10);
if (!styleTitle.getUnhideWhenUsed())
styleTitle.setUnhideWhenUsed(true);
if (styleTitle.getSemiHidden())
styleTitle.setSemiHidden(true);
doc.save(getArtifactsDir() + "Styles.StylePriority.docx");
Returns: boolean - The corresponding boolean value.
isHeading()
public boolean isHeading()
True when the style is one of the built-in Heading styles.
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: boolean - The corresponding boolean value.
isQuickStyle()
public boolean isQuickStyle()
Specifies whether this style is shown in the Quick Style gallery inside MS Word UI.
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: boolean - The corresponding boolean value.
isQuickStyle(boolean value)
public void isQuickStyle(boolean value)
Specifies whether this style is shown in the Quick Style gallery inside MS Word UI.
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
Parameters:
Parameter | Type | Description |
---|---|---|
value | boolean | The corresponding boolean value. |
remove()
public void remove()
Removes the specified style from the document.
Remarks:
Style removal has following effects on the document model:
- All references to the style are removed from corresponding paragraphs, runs and tables.
- If base style is removed its formatting is moved to child styles.
- If style to be deleted has a linked style, then both of these are deleted.
Examples:
Shows how to create and apply a custom style.
Document doc = new Document();
Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle");
style.getFont().setName("Times New Roman");
style.getFont().setSize(16.0);
style.getFont().setColor(Color.magenta);
// Automatically redefine style.
style.setAutomaticallyUpdate(true);
DocumentBuilder builder = new DocumentBuilder(doc);
// Apply one of the styles from the document to the paragraph that the document builder is creating.
builder.getParagraphFormat().setStyle(doc.getStyles().get("MyStyle"));
builder.writeln("Hello world!");
Style firstParagraphStyle = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().getStyle();
Assert.assertEquals(style, firstParagraphStyle);
// Remove our custom style from the document's styles collection.
doc.getStyles().get("MyStyle").remove();
firstParagraphStyle = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().getStyle();
// Any text that used a removed style reverts to the default formatting.
Assert.assertFalse(IterableUtils.matchesAny(doc.getStyles(), s -> s.getName() == "MyStyle"));
Assert.assertEquals("Times New Roman", firstParagraphStyle.getFont().getName());
Assert.assertEquals(12.0d, firstParagraphStyle.getFont().getSize());
Assert.assertEquals(0, firstParagraphStyle.getFont().getColor().getRGB());
removeParaAttr(int key)
public void removeParaAttr(int key)
Parameters:
Parameter | Type | Description |
---|---|---|
key | int |
removeRunAttr(int key)
public void removeRunAttr(int key)
Parameters:
Parameter | Type | Description |
---|---|---|
key | int |
setAutomaticallyUpdate(boolean value)
public void setAutomaticallyUpdate(boolean value)
Specifies whether this style is automatically redefined based on the appropriate value.
Remarks:
If the property value is set to true, MS Word automatically redefines the current style when the appropriate paragraph formatting has been changed.
AutomaticallyUpdate property is applicable to paragraph styles only.
The default value is false .
Examples:
Shows how to create and apply a custom style.
Document doc = new Document();
Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle");
style.getFont().setName("Times New Roman");
style.getFont().setSize(16.0);
style.getFont().setColor(Color.magenta);
// Automatically redefine style.
style.setAutomaticallyUpdate(true);
DocumentBuilder builder = new DocumentBuilder(doc);
// Apply one of the styles from the document to the paragraph that the document builder is creating.
builder.getParagraphFormat().setStyle(doc.getStyles().get("MyStyle"));
builder.writeln("Hello world!");
Style firstParagraphStyle = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().getStyle();
Assert.assertEquals(style, firstParagraphStyle);
// Remove our custom style from the document's styles collection.
doc.getStyles().get("MyStyle").remove();
firstParagraphStyle = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().getStyle();
// Any text that used a removed style reverts to the default formatting.
Assert.assertFalse(IterableUtils.matchesAny(doc.getStyles(), s -> s.getName() == "MyStyle"));
Assert.assertEquals("Times New Roman", firstParagraphStyle.getFont().getName());
Assert.assertEquals(12.0d, firstParagraphStyle.getFont().getSize());
Assert.assertEquals(0, firstParagraphStyle.getFont().getColor().getRGB());
Parameters:
Parameter | Type | Description |
---|---|---|
value | boolean | The corresponding boolean value. |
setBaseStyleName(String value)
public void setBaseStyleName(String value)
Gets/sets the name of the style this style is based on.
Remarks:
This will be an empty string if the style is not based on any other style and it can be set to an empty string.
Examples:
Shows how to use style aliases.
Document doc = new Document(getMyDir() + "Style with alias.docx");
// This document contains a style named "MyStyle,MyStyle Alias 1,MyStyle Alias 2".
// If a style's name has multiple values separated by commas, each clause is a separate alias.
Style style = doc.getStyles().get("MyStyle");
Assert.assertEquals(new String[]{"MyStyle Alias 1", "MyStyle Alias 2"}, style.getAliases());
Assert.assertEquals("Title", style.getBaseStyleName());
Assert.assertEquals("MyStyle Char", style.getLinkedStyleName());
// We can reference a style using its alias, as well as its name.
Assert.assertEquals(doc.getStyles().get("MyStyle Alias 1"), doc.getStyles().get("MyStyle Alias 2"));
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentEnd();
builder.getParagraphFormat().setStyle(doc.getStyles().get("MyStyle Alias 1"));
builder.writeln("Hello world!");
builder.getParagraphFormat().setStyle(doc.getStyles().get("MyStyle Alias 2"));
builder.write("Hello again!");
Assert.assertEquals(doc.getFirstSection().getBody().getParagraphs().get(0).getParagraphFormat().getStyle(),
doc.getFirstSection().getBody().getParagraphs().get(1).getParagraphFormat().getStyle());
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The corresponding java.lang.String value. |
setLinkedStyleName(String value)
public void setLinkedStyleName(String value)
Gets/sets the name of the Style linked to this one. Returns empty string if no styles are linked.
Remarks:
It is only allowed to link the paragraph style to the character style and vice versa.
Setting LinkedStyleName for the current style automatically leads to setting LinkedStyleName for the linked style.
Assigning the empty string is equivalent to unlinking the previously linked style.
Examples:
Shows how to link styles among themselves.
Document doc = new Document();
Style styleHeading1 = doc.getStyles().getByStyleIdentifier(StyleIdentifier.HEADING_1);
Style styleHeading1Char = doc.getStyles().add(StyleType.CHARACTER, "Heading 1 Char");
styleHeading1Char.getFont().setName("Verdana");
styleHeading1Char.getFont().setBold(true);
styleHeading1Char.getFont().getBorder().setLineStyle(LineStyle.DOT);
styleHeading1Char.getFont().getBorder().setLineWidth(15.0);
styleHeading1.setLinkedStyleName("Heading 1 Char");
Assert.assertEquals("Heading 1 Char", styleHeading1.getLinkedStyleName());
Assert.assertEquals("Heading 1", styleHeading1Char.getLinkedStyleName());
Shows how to use style aliases.
Document doc = new Document(getMyDir() + "Style with alias.docx");
// This document contains a style named "MyStyle,MyStyle Alias 1,MyStyle Alias 2".
// If a style's name has multiple values separated by commas, each clause is a separate alias.
Style style = doc.getStyles().get("MyStyle");
Assert.assertEquals(new String[]{"MyStyle Alias 1", "MyStyle Alias 2"}, style.getAliases());
Assert.assertEquals("Title", style.getBaseStyleName());
Assert.assertEquals("MyStyle Char", style.getLinkedStyleName());
// We can reference a style using its alias, as well as its name.
Assert.assertEquals(doc.getStyles().get("MyStyle Alias 1"), doc.getStyles().get("MyStyle Alias 2"));
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentEnd();
builder.getParagraphFormat().setStyle(doc.getStyles().get("MyStyle Alias 1"));
builder.writeln("Hello world!");
builder.getParagraphFormat().setStyle(doc.getStyles().get("MyStyle Alias 2"));
builder.write("Hello again!");
Assert.assertEquals(doc.getFirstSection().getBody().getParagraphs().get(0).getParagraphFormat().getStyle(),
doc.getFirstSection().getBody().getParagraphs().get(1).getParagraphFormat().getStyle());
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The corresponding java.lang.String value. |
setLocked(boolean value)
public void setLocked(boolean value)
Specifies whether this style is locked.
Examples:
Shows how to lock style.
Document doc = new Document();
Style styleHeading1 = doc.getStyles().getByStyleIdentifier(StyleIdentifier.HEADING_1);
if (!styleHeading1.getLocked())
styleHeading1.setLocked(true);
doc.save(getArtifactsDir() + "Styles.LockStyle.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | boolean | The corresponding boolean value. |
setName(String value)
public void setName(String value)
Sets the name of the style.
Remarks:
Can not be empty string.
If there already is a style with such name in the collection, then this style will override it. All affected nodes will reference new style.
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
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The name of the style. |
setNextParagraphStyleName(String value)
public void setNextParagraphStyleName(String value)
Gets/sets the name of the style to be applied automatically to a new paragraph inserted after a paragraph formatted with the specified style.
Remarks:
This property is not used by Aspose.Words. The next paragraph style will only be applied automatically when you edit the document in MS Word.
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
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The corresponding java.lang.String value. |
setParaAttr(int key, Object value)
public void setParaAttr(int key, Object value)
Parameters:
Parameter | Type | Description |
---|---|---|
key | int | |
value | java.lang.Object |
setPriority(int value)
public void setPriority(int value)
Gets/sets the integer value that represents the priority for sorting the styles in the Styles task pane.
Examples:
Shows how to prioritize and hide a style.
Document doc = new Document();
Style styleTitle = doc.getStyles().getByStyleIdentifier(StyleIdentifier.SUBTITLE);
if (styleTitle.getPriority() == 9)
styleTitle.setPriority(10);
if (!styleTitle.getUnhideWhenUsed())
styleTitle.setUnhideWhenUsed(true);
if (styleTitle.getSemiHidden())
styleTitle.setSemiHidden(true);
doc.save(getArtifactsDir() + "Styles.StylePriority.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | int | The corresponding int value. |
setRunAttr(int key, Object value)
public void setRunAttr(int key, Object value)
Parameters:
Parameter | Type | Description |
---|---|---|
key | int | |
value | java.lang.Object |
setSemiHidden(boolean value)
public void setSemiHidden(boolean value)
Gets/sets whether the style hides from the Styles gallery and from the Styles task pane.
Examples:
Shows how to prioritize and hide a style.
Document doc = new Document();
Style styleTitle = doc.getStyles().getByStyleIdentifier(StyleIdentifier.SUBTITLE);
if (styleTitle.getPriority() == 9)
styleTitle.setPriority(10);
if (!styleTitle.getUnhideWhenUsed())
styleTitle.setUnhideWhenUsed(true);
if (styleTitle.getSemiHidden())
styleTitle.setSemiHidden(true);
doc.save(getArtifactsDir() + "Styles.StylePriority.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | boolean | The corresponding boolean value. |
setUnhideWhenUsed(boolean value)
public void setUnhideWhenUsed(boolean value)
Gets/sets whether the style used in the current document unhides from the Styles gallery and from the Styles task pane. True when the used style should be shown in the Styles gallery.
Examples:
Shows how to prioritize and hide a style.
Document doc = new Document();
Style styleTitle = doc.getStyles().getByStyleIdentifier(StyleIdentifier.SUBTITLE);
if (styleTitle.getPriority() == 9)
styleTitle.setPriority(10);
if (!styleTitle.getUnhideWhenUsed())
styleTitle.setUnhideWhenUsed(true);
if (styleTitle.getSemiHidden())
styleTitle.setSemiHidden(true);
doc.save(getArtifactsDir() + "Styles.StylePriority.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | boolean | The corresponding boolean value. |