public class Style
extends java.lang.Object
implements java.lang.Cloneable
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());
| Modifier | Constructor and Description |
|---|---|
protected |
Style(int styleType)
Initializes a new instance of this class.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clearParaAttrs() |
void |
clearRunAttrs() |
boolean |
equals(Style style)
Compares with the specified style.
|
java.lang.Object |
fetchInheritedParaAttr(int key) |
java.lang.Object |
fetchInheritedRunAttr(int key) |
java.lang.Object |
fetchParaAttr(int key) |
java.lang.String[] |
getAliases()
Gets all aliases of this style.
|
boolean |
getAutomaticallyUpdate()
Specifies whether this style is automatically redefined based on the appropriate value.
|
java.lang.String |
getBaseStyleName()
Gets/sets the name of the style this style is based on.
|
boolean |
getBuiltIn()
True if this style is one of the built-in styles in MS Word.
|
java.lang.Object |
getDirectParaAttr(int key) |
java.lang.Object |
getDirectParaAttr(int key,
int revisionsView) |
java.lang.Object |
getDirectRunAttr(int key) |
java.lang.Object |
getDirectRunAttr(int key,
int revisionsView) |
DocumentBase |
getDocument()
Gets the owner document.
|
Font |
getFont()
Gets the character formatting of the style.
|
java.lang.String |
getLinkedStyleName()
Gets/sets the name of the
Style linked to this one. |
List |
getList()
Gets the list that defines formatting of this list style.
|
ListFormat |
getListFormat()
Provides access to the list formatting properties of a paragraph style.
|
boolean |
getLocked()
Specifies whether this style is locked.
|
java.lang.String |
getName()
Gets the name of the style.
|
java.lang.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.
|
ParagraphFormat |
getParagraphFormat()
Gets the paragraph formatting of the style.
|
int |
getPriority()
Gets/sets the integer value that represents the priority for sorting the styles in the Styles task pane.
|
boolean |
getSemiHidden()
Gets/sets whether the style hides from the Styles gallery and from the Styles task pane.
|
int |
getStyleIdentifier()
Gets the locale independent style identifier for a built-in style.
|
StyleCollection |
getStyles()
Gets the collection of styles this style belongs to.
|
int |
getType()
Gets the style type (paragraph or character).
|
boolean |
getUnhideWhenUsed()
Gets/sets whether the style used in the current document unhides from the Styles gallery and from the Styles task pane.
|
boolean |
isHeading()
True when the style is one of the built-in Heading styles.
|
boolean |
isQuickStyle()
Specifies whether this style is shown in the Quick Style gallery inside MS Word UI.
|
void |
isQuickStyle(boolean value)
Specifies whether this style is shown in the Quick Style gallery inside MS Word UI.
|
protected java.lang.Object |
memberwiseClone() |
void |
remove()
Removes the specified style from the document.
|
void |
removeParaAttr(int key) |
void |
removeRunAttr(int key) |
void |
setAutomaticallyUpdate(boolean value)
Specifies whether this style is automatically redefined based on the appropriate value.
|
void |
setBaseStyleName(java.lang.String value)
Gets/sets the name of the style this style is based on.
|
void |
setLinkedStyleName(java.lang.String value)
Gets/sets the name of the
Style linked to this one. |
void |
setLocked(boolean value)
Specifies whether this style is locked.
|
void |
setName(java.lang.String value)
Sets the name of the style.
|
void |
setNextParagraphStyleName(java.lang.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.
|
void |
setParaAttr(int key,
java.lang.Object value) |
void |
setPriority(int value)
Gets/sets the integer value that represents the priority for sorting the styles in the Styles task pane.
|
void |
setRunAttr(int key,
java.lang.Object value) |
void |
setSemiHidden(boolean value)
Gets/sets whether the style hides from the Styles gallery and from the Styles task pane.
|
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.
|
public java.lang.String getName()
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<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);
}
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());
public void setName(java.lang.String value)
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<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);
}
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());
value - The name of the style.public int getStyleIdentifier()
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<Paragraph>) 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");
StyleIdentifier constants.getName(),
setName(java.lang.String)public java.lang.String[] getAliases()
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());
public boolean isHeading()
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<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);
}
boolean value.public int getType()
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<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);
}
StyleType constants.public DocumentBase getDocument()
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<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);
}
public java.lang.String getLinkedStyleName()
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());
String value.public void setLinkedStyleName(java.lang.String value)
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());
value - The corresponding String value.public java.lang.String getBaseStyleName()
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());
String value.public void setBaseStyleName(java.lang.String value)
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());
value - The corresponding String value.public java.lang.String getNextParagraphStyleName()
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<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);
}
String value.public void setNextParagraphStyleName(java.lang.String value)
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<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);
}
value - The corresponding String value.public boolean getBuiltIn()
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());
boolean value.public Font getFont()
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());
public ParagraphFormat getParagraphFormat()
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");
public boolean getSemiHidden()
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");
boolean value.public void setSemiHidden(boolean value)
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");
value - The corresponding boolean value.public boolean getUnhideWhenUsed()
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");
boolean value.public void setUnhideWhenUsed(boolean value)
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");
value - The corresponding boolean value.public int getPriority()
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");
int value.public void setPriority(int value)
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");
value - The corresponding int value.public List getList()
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");
public ListFormat getListFormat()
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");
ListFormat value.public boolean isQuickStyle()
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<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);
}
boolean value.public void isQuickStyle(boolean value)
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<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);
}
value - The corresponding boolean value.public boolean getAutomaticallyUpdate()
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());
boolean value.public void setAutomaticallyUpdate(boolean 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());
value - The corresponding boolean value.public boolean getLocked()
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");
boolean value.public void setLocked(boolean value)
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");
value - The corresponding boolean value.public StyleCollection getStyles()
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<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);
}
public void remove()
Remarks:
Style removal has following effects on the document model:
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());
public boolean equals(Style style)
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());
public java.lang.Object getDirectParaAttr(int key)
public java.lang.Object getDirectParaAttr(int key,
int revisionsView)
public java.lang.Object fetchParaAttr(int key)
public java.lang.Object fetchInheritedParaAttr(int key)
public void setParaAttr(int key,
java.lang.Object value)
public void removeParaAttr(int key)
public void clearParaAttrs()
public java.lang.Object getDirectRunAttr(int key)
public java.lang.Object getDirectRunAttr(int key,
int revisionsView)
public java.lang.Object fetchInheritedRunAttr(int key)
public void setRunAttr(int key,
java.lang.Object value)
public void removeRunAttr(int key)
public void clearRunAttrs()
protected java.lang.Object memberwiseClone()