public class TableStyle
Example:
Shows how to create custom style settings for the table.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.startTable(); builder.insertCell(); builder.write("Name"); builder.insertCell(); builder.write("مرحبًا"); builder.endRow(); builder.insertCell(); builder.insertCell(); builder.endTable(); TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1"); tableStyle.setAllowBreakAcrossPages(true); tableStyle.setBidi(true); tableStyle.setCellSpacing(5.0); tableStyle.setBottomPadding(20.0); tableStyle.setLeftPadding(5.0); tableStyle.setRightPadding(10.0); tableStyle.setTopPadding(20.0); tableStyle.getShading().setBackgroundPatternColor(Color.WHITE); tableStyle.getBorders().setColor(Color.BLACK); tableStyle.getBorders().setLineStyle(LineStyle.DOT_DASH); table.setStyle(tableStyle); // Some Table attributes are linked to style variables Assert.assertEquals(table.getBidi(), true); Assert.assertEquals(table.getCellSpacing(), 5.0); Assert.assertEquals(table.getStyleName(), "MyTableStyle1"); doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");
Property Getters/Setters Summary | ||
---|---|---|
java.lang.String[] | getAliases() | |
Gets all aliases of this style. If style has no aliases then empty array of string is returned.
|
||
int | getAlignment() | |
void | setAlignment(intvalue) | |
Specifies the alignment for the table style. The value of the property is TableAlignment integer constant. | ||
boolean | getAllowBreakAcrossPages() | |
void | setAllowBreakAcrossPages(booleanvalue) | |
Gets or sets a flag indicating whether text in a table row is allowed to split across a page break. | ||
java.lang.String | getBaseStyleName() | |
void | setBaseStyleName(java.lang.Stringvalue) | |
Gets/sets the name of the style this style is based on. | ||
boolean | getBidi() | |
void | setBidi(booleanvalue) | |
Gets or sets whether this is a style for a right-to-left table. | ||
BorderCollection | getBorders() | |
Gets the collection of default cell borders for the style.
|
||
double | getBottomPadding() | |
void | setBottomPadding(doublevalue) | |
Gets or sets the amount of space (in points) to add below the contents of table cells. | ||
boolean | getBuiltIn() | |
True if this style is one of the built-in styles in MS Word.
|
||
double | getCellSpacing() | |
void | setCellSpacing(doublevalue) | |
Gets or sets the amount of space (in points) between the cells. | ||
int | getColumnStripe() | |
void | setColumnStripe(intvalue) | |
Gets or sets a number of columns to include in the banding when the style specifies odd/even columns banding. | ||
ConditionalStyleCollection | getConditionalStyles() | |
Collection of conditional styles that may be defined for this table style.
|
||
DocumentBase | getDocument() | |
Gets the owner document.
|
||
Font | getFont() | |
Gets the character formatting of the style.
|
||
boolean | isHeading() | |
True when the style is one of the built-in Heading styles.
|
||
boolean | isQuickStyle() | |
void | isQuickStyle(booleanvalue) | |
Specifies whether this style is shown in the Quick Style gallery inside MS Word UI. | ||
double | getLeftIndent() | |
void | setLeftIndent(doublevalue) | |
Gets or sets the value that represents the left indent of a table. | ||
double | getLeftPadding() | |
void | setLeftPadding(doublevalue) | |
Gets or sets the amount of space (in points) to add to the left of the contents of table cells. | ||
java.lang.String | getLinkedStyleName() | |
Gets the name of the Style linked to this one. Returns Empty string if no styles are linked.
|
||
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.
|
||
java.lang.String | getName() | |
void | setName(java.lang.Stringvalue) | |
Gets or sets the name of the style. | ||
java.lang.String | getNextParagraphStyleName() | |
void | setNextParagraphStyleName(java.lang.Stringvalue) | |
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.
|
||
double | getRightPadding() | |
void | setRightPadding(doublevalue) | |
Gets or sets the amount of space (in points) to add to the right of the contents of table cells. | ||
int | getRowStripe() | |
void | setRowStripe(intvalue) | |
Gets or sets a number of rows to include in the banding when the style specifies odd/even row banding. | ||
Shading | getShading() | |
Gets a |
||
int | getStyleIdentifier() | |
Gets the locale independent style identifier for a built-in style.
The value of the property is StyleIdentifier integer constant. |
||
StyleCollection | getStyles() | |
Gets the collection of styles this style belongs to.
|
||
double | getTopPadding() | |
void | setTopPadding(doublevalue) | |
Gets or sets the amount of space (in points) to add above the contents of table cells. | ||
int | getType() | |
Gets the style type (paragraph or character).
The value of the property is StyleType integer constant. |
Method Summary | ||
---|---|---|
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.
|
||
void | remove() | |
Removes the specified style from the document.
|
public java.lang.String[] getAliases()
Example:
Shows how to use style aliases.Document doc = new Document(getMyDir() + "Style with alias.docx"); // If a style's name has multiple values separated by commas, each one is considered to be 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()); // A style can be referenced by alias as well as name Assert.assertEquals(style, doc.getStyles().get("MyStyle Alias 1"));
public int getAlignment() / public void setAlignment(int value)
Example:
Shows how to set table position.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // There are two ways of horizontally aligning a table using a custom table style // One way is to align it to a location on the page, such as the center TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1"); tableStyle.setAlignment(TableAlignment.CENTER); tableStyle.getBorders().setColor(Color.BLUE); tableStyle.getBorders().setLineStyle(LineStyle.SINGLE); // Insert a table and apply the style we created to it Table table = builder.startTable(); builder.insertCell(); builder.write("Aligned to the center of the page"); builder.endTable(); table.setPreferredWidth(PreferredWidth.fromPoints(300.0)); table.setStyle(tableStyle); // We can also set a specific left indent to the style, and apply it to the table tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle2"); tableStyle.setLeftIndent(55.0); tableStyle.getBorders().setColor(Color.GREEN); tableStyle.getBorders().setLineStyle(LineStyle.SINGLE); table = builder.startTable(); builder.insertCell(); builder.write("Aligned according to left indent"); builder.endTable(); table.setPreferredWidth(PreferredWidth.fromPoints(300.0)); table.setStyle(tableStyle); doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");
public boolean getAllowBreakAcrossPages() / public void setAllowBreakAcrossPages(boolean value)
Example:
Shows how to create custom style settings for the table.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.startTable(); builder.insertCell(); builder.write("Name"); builder.insertCell(); builder.write("مرحبًا"); builder.endRow(); builder.insertCell(); builder.insertCell(); builder.endTable(); TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1"); tableStyle.setAllowBreakAcrossPages(true); tableStyle.setBidi(true); tableStyle.setCellSpacing(5.0); tableStyle.setBottomPadding(20.0); tableStyle.setLeftPadding(5.0); tableStyle.setRightPadding(10.0); tableStyle.setTopPadding(20.0); tableStyle.getShading().setBackgroundPatternColor(Color.WHITE); tableStyle.getBorders().setColor(Color.BLACK); tableStyle.getBorders().setLineStyle(LineStyle.DOT_DASH); table.setStyle(tableStyle); // Some Table attributes are linked to style variables Assert.assertEquals(table.getBidi(), true); Assert.assertEquals(table.getCellSpacing(), 5.0); Assert.assertEquals(table.getStyleName(), "MyTableStyle1"); doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");
public java.lang.String getBaseStyleName() / public void setBaseStyleName(java.lang.String value)
Example:
Shows how to use style aliases.Document doc = new Document(getMyDir() + "Style with alias.docx"); // If a style's name has multiple values separated by commas, each one is considered to be 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()); // A style can be referenced by alias as well as name Assert.assertEquals(style, doc.getStyles().get("MyStyle Alias 1"));
public boolean getBidi() / public void setBidi(boolean value)
When true, the cells in rows are laid out right to left.
The default value is false.
Example:
Shows how to create custom style settings for the table.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.startTable(); builder.insertCell(); builder.write("Name"); builder.insertCell(); builder.write("مرحبًا"); builder.endRow(); builder.insertCell(); builder.insertCell(); builder.endTable(); TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1"); tableStyle.setAllowBreakAcrossPages(true); tableStyle.setBidi(true); tableStyle.setCellSpacing(5.0); tableStyle.setBottomPadding(20.0); tableStyle.setLeftPadding(5.0); tableStyle.setRightPadding(10.0); tableStyle.setTopPadding(20.0); tableStyle.getShading().setBackgroundPatternColor(Color.WHITE); tableStyle.getBorders().setColor(Color.BLACK); tableStyle.getBorders().setLineStyle(LineStyle.DOT_DASH); table.setStyle(tableStyle); // Some Table attributes are linked to style variables Assert.assertEquals(table.getBidi(), true); Assert.assertEquals(table.getCellSpacing(), 5.0); Assert.assertEquals(table.getStyleName(), "MyTableStyle1"); doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");
public BorderCollection getBorders()
Example:
Shows how to create custom style settings for the table.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.startTable(); builder.insertCell(); builder.write("Name"); builder.insertCell(); builder.write("مرحبًا"); builder.endRow(); builder.insertCell(); builder.insertCell(); builder.endTable(); TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1"); tableStyle.setAllowBreakAcrossPages(true); tableStyle.setBidi(true); tableStyle.setCellSpacing(5.0); tableStyle.setBottomPadding(20.0); tableStyle.setLeftPadding(5.0); tableStyle.setRightPadding(10.0); tableStyle.setTopPadding(20.0); tableStyle.getShading().setBackgroundPatternColor(Color.WHITE); tableStyle.getBorders().setColor(Color.BLACK); tableStyle.getBorders().setLineStyle(LineStyle.DOT_DASH); table.setStyle(tableStyle); // Some Table attributes are linked to style variables Assert.assertEquals(table.getBidi(), true); Assert.assertEquals(table.getCellSpacing(), 5.0); Assert.assertEquals(table.getStyleName(), "MyTableStyle1"); doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");
public double getBottomPadding() / public void setBottomPadding(double value)
Example:
Shows how to create custom style settings for the table.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.startTable(); builder.insertCell(); builder.write("Name"); builder.insertCell(); builder.write("مرحبًا"); builder.endRow(); builder.insertCell(); builder.insertCell(); builder.endTable(); TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1"); tableStyle.setAllowBreakAcrossPages(true); tableStyle.setBidi(true); tableStyle.setCellSpacing(5.0); tableStyle.setBottomPadding(20.0); tableStyle.setLeftPadding(5.0); tableStyle.setRightPadding(10.0); tableStyle.setTopPadding(20.0); tableStyle.getShading().setBackgroundPatternColor(Color.WHITE); tableStyle.getBorders().setColor(Color.BLACK); tableStyle.getBorders().setLineStyle(LineStyle.DOT_DASH); table.setStyle(tableStyle); // Some Table attributes are linked to style variables Assert.assertEquals(table.getBidi(), true); Assert.assertEquals(table.getCellSpacing(), 5.0); Assert.assertEquals(table.getStyleName(), "MyTableStyle1"); doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");
public boolean getBuiltIn()
Example:
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());
public double getCellSpacing() / public void setCellSpacing(double value)
Example:
Shows how to create custom style settings for the table.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.startTable(); builder.insertCell(); builder.write("Name"); builder.insertCell(); builder.write("مرحبًا"); builder.endRow(); builder.insertCell(); builder.insertCell(); builder.endTable(); TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1"); tableStyle.setAllowBreakAcrossPages(true); tableStyle.setBidi(true); tableStyle.setCellSpacing(5.0); tableStyle.setBottomPadding(20.0); tableStyle.setLeftPadding(5.0); tableStyle.setRightPadding(10.0); tableStyle.setTopPadding(20.0); tableStyle.getShading().setBackgroundPatternColor(Color.WHITE); tableStyle.getBorders().setColor(Color.BLACK); tableStyle.getBorders().setLineStyle(LineStyle.DOT_DASH); table.setStyle(tableStyle); // Some Table attributes are linked to style variables Assert.assertEquals(table.getBidi(), true); Assert.assertEquals(table.getCellSpacing(), 5.0); Assert.assertEquals(table.getStyleName(), "MyTableStyle1"); doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");
public int getColumnStripe() / public void setColumnStripe(int value)
Example:
Shows how to create conditional table styles that alternate between rows.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // The conditional style of a table can be configured to apply a different color to the row/column, // based on whether the row/column is even or odd, creating an alternating color pattern // We can also apply a number n to the row/column banding, meaning that the color alternates after every n rows/columns instead of one // Create a table where the columns will be banded by single columns and rows will banded in threes Table table = builder.startTable(); for (int i = 0; i < 15; i++) { for (int j = 0; j < 4; j++) { builder.insertCell(); builder.writeln(MessageFormat.format("{0} column.", (j % 2 == 0 ? "Even" : "Odd"))); builder.write(MessageFormat.format("Row banding {0}.", (i % 3 == 0 ? "start" : "continuation"))); } builder.endRow(); } builder.endTable(); // Set a line style for all the borders of the table TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1"); tableStyle.getBorders().setColor(Color.BLACK); tableStyle.getBorders().setLineStyle(LineStyle.DOUBLE); // Set the two colors which will alternate over every 3 rows tableStyle.setRowStripe(3); tableStyle.getConditionalStyles().getByConditionalStyleType(ConditionalStyleType.ODD_ROW_BANDING).getShading().setBackgroundPatternColor(Color.BLUE); tableStyle.getConditionalStyles().getByConditionalStyleType(ConditionalStyleType.EVEN_ROW_BANDING).getShading().setBackgroundPatternColor(Color.CYAN); // Set a color to apply to every even column, which will override any custom row coloring tableStyle.setColumnStripe(1); tableStyle.getConditionalStyles().getByConditionalStyleType(ConditionalStyleType.EVEN_COLUMN_BANDING).getShading().setBackgroundPatternColor(Color.RED); // Apply the style to the table table.setStyle(tableStyle); // Row bands are automatically enabled, but column banding needs to be enabled manually like this // Row coloring will only be overridden if the column banding has been explicitly set a color table.setStyleOptions(table.getStyleOptions() | TableStyleOptions.COLUMN_BANDS); doc.save(getArtifactsDir() + "Table.AlternatingRowStyles.docx");
public ConditionalStyleCollection getConditionalStyles()
Example:
Shows how to work with certain area styles of a table.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create a table Table table = builder.startTable(); builder.insertCell(); builder.write("Cell 1"); builder.insertCell(); builder.write("Cell 2"); builder.endRow(); builder.insertCell(); builder.write("Cell 3"); builder.insertCell(); builder.write("Cell 4"); builder.endTable(); // Create a custom table style TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1"); // Conditional styles are formatting changes that affect only some of the cells of the table based on a predicate, // such as the cells being in the last row // We can access these conditional styles by style type like this tableStyle.getConditionalStyles().getByConditionalStyleType(ConditionalStyleType.FIRST_ROW).getShading().setBackgroundPatternColor(Color.BLUE); // The same conditional style can be accessed by index tableStyle.getConditionalStyles().get(0).getBorders().setColor(Color.BLACK); tableStyle.getConditionalStyles().get(0).getBorders().setLineStyle(LineStyle.DOT_DASH); Assert.assertEquals(ConditionalStyleType.FIRST_ROW, tableStyle.getConditionalStyles().get(0).getType()); // It can also be found in the ConditionalStyles collection as an attribute tableStyle.getConditionalStyles().getFirstRow().getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); // Apply padding and text formatting to conditional styles tableStyle.getConditionalStyles().getLastRow().setBottomPadding(10.0); tableStyle.getConditionalStyles().getLastRow().setLeftPadding(10.0); tableStyle.getConditionalStyles().getLastRow().setRightPadding(10.0); tableStyle.getConditionalStyles().getLastRow().setTopPadding(10.0); tableStyle.getConditionalStyles().getLastColumn().getFont().setBold(true); // List all possible style conditions Iterator<ConditionalStyle> enumerator = tableStyle.getConditionalStyles().iterator(); while (enumerator.hasNext()) { ConditionalStyle currentStyle = enumerator.next(); if (currentStyle != null) System.out.println(currentStyle.getType()); } // Apply conditional style to the table table.setStyle(tableStyle); // Changes to the first row are enabled by the table's style options be default, // but need to be manually enabled for some other parts, such as the last column/row table.setStyleOptions(table.getStyleOptions() | TableStyleOptions.LAST_ROW | TableStyleOptions.LAST_COLUMN); doc.save(getArtifactsDir() + "Table.ConditionalStyles.docx");
public DocumentBase getDocument()
Example:
Shows how to access a document's style collection.Document doc = new Document(); // A blank document comes with 4 styles by default Assert.assertEquals(4, doc.getStyles().getCount()); 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 Font getFont()
For list styles this property returns null.
Example:
Shows how to create and use a paragraph style with list formatting.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create a paragraph style and specify some formatting for it Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle1"); style.getFont().setSize(24.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 current paragraph in the document and add some text builder.getParagraphFormat().setStyle(style); builder.writeln("Hello World: MyStyle1, bulleted list."); // 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(getArtifactsDir() + "Styles.ParagraphStyleBulletedList.docx");
Example:
Shows how to create and apply a style.Document doc = new Document(); // Add a custom style and change its appearance Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle"); style.getFont().setName("Times New Roman"); style.getFont().setSize(16.0); style.getFont().setColor(Color.magenta); // Write a paragraph in that style DocumentBuilder builder = new DocumentBuilder(doc); builder.getParagraphFormat().setStyle(doc.getStyles().get("MyStyle")); builder.writeln("Hello world!"); Style firstParagraphStyle = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().getStyle(); Assert.assertEquals(style, firstParagraphStyle); // Styles can also be removed from the collection like this doc.getStyles().get("MyStyle").remove(); firstParagraphStyle = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().getStyle(); // Removing the style reverts the styling of the text that was in that style Assert.assertEquals("Times New Roman", firstParagraphStyle.getFont().getName()); Assert.assertEquals(12.0d, firstParagraphStyle.getFont().getSize()); Assert.assertEquals(0, firstParagraphStyle.getFont().getColor().getRGB());
public boolean isHeading()
Example:
Shows how to access a document's style collection.Document doc = new Document(); // A blank document comes with 4 styles by default Assert.assertEquals(4, doc.getStyles().getCount()); 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 boolean isQuickStyle() / public void isQuickStyle(boolean value)
Example:
Shows how to access a document's style collection.Document doc = new Document(); // A blank document comes with 4 styles by default Assert.assertEquals(4, doc.getStyles().getCount()); 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 double getLeftIndent() / public void setLeftIndent(double value)
Example:
Shows how to set table position.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // There are two ways of horizontally aligning a table using a custom table style // One way is to align it to a location on the page, such as the center TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1"); tableStyle.setAlignment(TableAlignment.CENTER); tableStyle.getBorders().setColor(Color.BLUE); tableStyle.getBorders().setLineStyle(LineStyle.SINGLE); // Insert a table and apply the style we created to it Table table = builder.startTable(); builder.insertCell(); builder.write("Aligned to the center of the page"); builder.endTable(); table.setPreferredWidth(PreferredWidth.fromPoints(300.0)); table.setStyle(tableStyle); // We can also set a specific left indent to the style, and apply it to the table tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle2"); tableStyle.setLeftIndent(55.0); tableStyle.getBorders().setColor(Color.GREEN); tableStyle.getBorders().setLineStyle(LineStyle.SINGLE); table = builder.startTable(); builder.insertCell(); builder.write("Aligned according to left indent"); builder.endTable(); table.setPreferredWidth(PreferredWidth.fromPoints(300.0)); table.setStyle(tableStyle); doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");
public double getLeftPadding() / public void setLeftPadding(double value)
Example:
Shows how to create custom style settings for the table.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.startTable(); builder.insertCell(); builder.write("Name"); builder.insertCell(); builder.write("مرحبًا"); builder.endRow(); builder.insertCell(); builder.insertCell(); builder.endTable(); TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1"); tableStyle.setAllowBreakAcrossPages(true); tableStyle.setBidi(true); tableStyle.setCellSpacing(5.0); tableStyle.setBottomPadding(20.0); tableStyle.setLeftPadding(5.0); tableStyle.setRightPadding(10.0); tableStyle.setTopPadding(20.0); tableStyle.getShading().setBackgroundPatternColor(Color.WHITE); tableStyle.getBorders().setColor(Color.BLACK); tableStyle.getBorders().setLineStyle(LineStyle.DOT_DASH); table.setStyle(tableStyle); // Some Table attributes are linked to style variables Assert.assertEquals(table.getBidi(), true); Assert.assertEquals(table.getCellSpacing(), 5.0); Assert.assertEquals(table.getStyleName(), "MyTableStyle1"); doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");
public java.lang.String getLinkedStyleName()
Example:
Shows how to use style aliases.Document doc = new Document(getMyDir() + "Style with alias.docx"); // If a style's name has multiple values separated by commas, each one is considered to be 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()); // A style can be referenced by alias as well as name Assert.assertEquals(style, doc.getStyles().get("MyStyle Alias 1"));
public List getList()
This property is only valid for list styles. For other style types this property returns null.
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 (ListLevel level : list1.getListLevels()) { 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(getArtifactsDir() + "Lists.CreateAndUseListStyle.docx");
public ListFormat getListFormat()
This property is only valid for paragraph styles. For other style types this property returns null.
Example:
Shows how to create and use a paragraph style with list formatting.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create a paragraph style and specify some formatting for it Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle1"); style.getFont().setSize(24.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 current paragraph in the document and add some text builder.getParagraphFormat().setStyle(style); builder.writeln("Hello World: MyStyle1, bulleted list."); // 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(getArtifactsDir() + "Styles.ParagraphStyleBulletedList.docx");
public java.lang.String getName() / public void setName(java.lang.String value)
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.
Example:
Shows how to copy a style within the same document.Document doc = new Document(getMyDir() + "Document.docx"); // The AddCopy method creates a copy of the specified style and automatically generates a new name for the style, such as "Heading 1_0" Style newStyle = doc.getStyles().addCopy(doc.getStyles().get("Heading 1")); // You can change the new style name if required as the Style.Name property is read-write newStyle.setName("My Heading 1");
Example:
Shows how to access a document's style collection.Document doc = new Document(); // A blank document comes with 4 styles by default Assert.assertEquals(4, doc.getStyles().getCount()); 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 getNextParagraphStyleName() / public void setNextParagraphStyleName(java.lang.String value)
Example:
Shows how to access a document's style collection.Document doc = new Document(); // A blank document comes with 4 styles by default Assert.assertEquals(4, doc.getStyles().getCount()); 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 ParagraphFormat getParagraphFormat()
For character and list styles this property returns null.
Example:
Shows how to create and use a paragraph style with list formatting.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create a paragraph style and specify some formatting for it Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle1"); style.getFont().setSize(24.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 current paragraph in the document and add some text builder.getParagraphFormat().setStyle(style); builder.writeln("Hello World: MyStyle1, bulleted list."); // 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(getArtifactsDir() + "Styles.ParagraphStyleBulletedList.docx");
public double getRightPadding() / public void setRightPadding(double value)
Example:
Shows how to create custom style settings for the table.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.startTable(); builder.insertCell(); builder.write("Name"); builder.insertCell(); builder.write("مرحبًا"); builder.endRow(); builder.insertCell(); builder.insertCell(); builder.endTable(); TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1"); tableStyle.setAllowBreakAcrossPages(true); tableStyle.setBidi(true); tableStyle.setCellSpacing(5.0); tableStyle.setBottomPadding(20.0); tableStyle.setLeftPadding(5.0); tableStyle.setRightPadding(10.0); tableStyle.setTopPadding(20.0); tableStyle.getShading().setBackgroundPatternColor(Color.WHITE); tableStyle.getBorders().setColor(Color.BLACK); tableStyle.getBorders().setLineStyle(LineStyle.DOT_DASH); table.setStyle(tableStyle); // Some Table attributes are linked to style variables Assert.assertEquals(table.getBidi(), true); Assert.assertEquals(table.getCellSpacing(), 5.0); Assert.assertEquals(table.getStyleName(), "MyTableStyle1"); doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");
public int getRowStripe() / public void setRowStripe(int value)
Example:
Shows how to create conditional table styles that alternate between rows.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // The conditional style of a table can be configured to apply a different color to the row/column, // based on whether the row/column is even or odd, creating an alternating color pattern // We can also apply a number n to the row/column banding, meaning that the color alternates after every n rows/columns instead of one // Create a table where the columns will be banded by single columns and rows will banded in threes Table table = builder.startTable(); for (int i = 0; i < 15; i++) { for (int j = 0; j < 4; j++) { builder.insertCell(); builder.writeln(MessageFormat.format("{0} column.", (j % 2 == 0 ? "Even" : "Odd"))); builder.write(MessageFormat.format("Row banding {0}.", (i % 3 == 0 ? "start" : "continuation"))); } builder.endRow(); } builder.endTable(); // Set a line style for all the borders of the table TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1"); tableStyle.getBorders().setColor(Color.BLACK); tableStyle.getBorders().setLineStyle(LineStyle.DOUBLE); // Set the two colors which will alternate over every 3 rows tableStyle.setRowStripe(3); tableStyle.getConditionalStyles().getByConditionalStyleType(ConditionalStyleType.ODD_ROW_BANDING).getShading().setBackgroundPatternColor(Color.BLUE); tableStyle.getConditionalStyles().getByConditionalStyleType(ConditionalStyleType.EVEN_ROW_BANDING).getShading().setBackgroundPatternColor(Color.CYAN); // Set a color to apply to every even column, which will override any custom row coloring tableStyle.setColumnStripe(1); tableStyle.getConditionalStyles().getByConditionalStyleType(ConditionalStyleType.EVEN_COLUMN_BANDING).getShading().setBackgroundPatternColor(Color.RED); // Apply the style to the table table.setStyle(tableStyle); // Row bands are automatically enabled, but column banding needs to be enabled manually like this // Row coloring will only be overridden if the column banding has been explicitly set a color table.setStyleOptions(table.getStyleOptions() | TableStyleOptions.COLUMN_BANDS); doc.save(getArtifactsDir() + "Table.AlternatingRowStyles.docx");
public Shading getShading()
Example:
Shows how to create custom style settings for the table.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.startTable(); builder.insertCell(); builder.write("Name"); builder.insertCell(); builder.write("مرحبًا"); builder.endRow(); builder.insertCell(); builder.insertCell(); builder.endTable(); TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1"); tableStyle.setAllowBreakAcrossPages(true); tableStyle.setBidi(true); tableStyle.setCellSpacing(5.0); tableStyle.setBottomPadding(20.0); tableStyle.setLeftPadding(5.0); tableStyle.setRightPadding(10.0); tableStyle.setTopPadding(20.0); tableStyle.getShading().setBackgroundPatternColor(Color.WHITE); tableStyle.getBorders().setColor(Color.BLACK); tableStyle.getBorders().setLineStyle(LineStyle.DOT_DASH); table.setStyle(tableStyle); // Some Table attributes are linked to style variables Assert.assertEquals(table.getBidi(), true); Assert.assertEquals(table.getCellSpacing(), 5.0); Assert.assertEquals(table.getStyleName(), "MyTableStyle1"); doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");
public int getStyleIdentifier()
For user defined (custom) styles, this property returns
Example:
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 formatted using the 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); // Remove the old tab from the collection para.getParagraphFormat().getTabStops().removeByPosition(tab.getPosition()); // Insert a new tab using the same properties but at a modified position // We could also change the separators used (dots) by passing a different Leader type para.getParagraphFormat().getTabStops().add(tab.getPosition() - 50, tab.getAlignment(), tab.getLeader()); } } doc.save(getArtifactsDir() + "Styles.ChangeTocsTabStops.docx");
public StyleCollection getStyles()
Example:
Shows how to access a document's style collection.Document doc = new Document(); // A blank document comes with 4 styles by default Assert.assertEquals(4, doc.getStyles().getCount()); 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 double getTopPadding() / public void setTopPadding(double value)
Example:
Shows how to create custom style settings for the table.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.startTable(); builder.insertCell(); builder.write("Name"); builder.insertCell(); builder.write("مرحبًا"); builder.endRow(); builder.insertCell(); builder.insertCell(); builder.endTable(); TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1"); tableStyle.setAllowBreakAcrossPages(true); tableStyle.setBidi(true); tableStyle.setCellSpacing(5.0); tableStyle.setBottomPadding(20.0); tableStyle.setLeftPadding(5.0); tableStyle.setRightPadding(10.0); tableStyle.setTopPadding(20.0); tableStyle.getShading().setBackgroundPatternColor(Color.WHITE); tableStyle.getBorders().setColor(Color.BLACK); tableStyle.getBorders().setLineStyle(LineStyle.DOT_DASH); table.setStyle(tableStyle); // Some Table attributes are linked to style variables Assert.assertEquals(table.getBidi(), true); Assert.assertEquals(table.getCellSpacing(), 5.0); Assert.assertEquals(table.getStyleName(), "MyTableStyle1"); doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");
public int getType()
Example:
Shows how to access a document's style collection.Document doc = new Document(); // A blank document comes with 4 styles by default Assert.assertEquals(4, doc.getStyles().getCount()); 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 boolean equals(Style style)
Example:
Shows how to use style aliases.Document doc = new Document(getMyDir() + "Style with alias.docx"); // If a style's name has multiple values separated by commas, each one is considered to be 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()); // A style can be referenced by alias as well as name Assert.assertEquals(style, doc.getStyles().get("MyStyle Alias 1"));
public void remove()
Example:
Shows how to create and apply a style.Document doc = new Document(); // Add a custom style and change its appearance Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle"); style.getFont().setName("Times New Roman"); style.getFont().setSize(16.0); style.getFont().setColor(Color.magenta); // Write a paragraph in that style DocumentBuilder builder = new DocumentBuilder(doc); builder.getParagraphFormat().setStyle(doc.getStyles().get("MyStyle")); builder.writeln("Hello world!"); Style firstParagraphStyle = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().getStyle(); Assert.assertEquals(style, firstParagraphStyle); // Styles can also be removed from the collection like this doc.getStyles().get("MyStyle").remove(); firstParagraphStyle = doc.getFirstSection().getBody().getFirstParagraph().getParagraphFormat().getStyle(); // Removing the style reverts the styling of the text that was in that style Assert.assertEquals("Times New Roman", firstParagraphStyle.getFont().getName()); Assert.assertEquals(12.0d, firstParagraphStyle.getFont().getSize()); Assert.assertEquals(0, firstParagraphStyle.getFont().getColor().getRGB());