java.lang.ObjectStyle
com.aspose.words.TableStyle
public class TableStyle
To learn more, visit the Working with Tables documentation article. Example:
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);
tableStyle.setVerticalAlignment(CellVerticalAlignment.CENTER);
table.setStyle(tableStyle);
// Setting the style properties of a table may affect the properties of the table itself.
Assert.assertTrue(table.getBidi());
Assert.assertEquals(5.0d, table.getCellSpacing());
Assert.assertEquals("MyTableStyle1", table.getStyleName());
doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");
| Property Getters/Setters Summary | ||
|---|---|---|
java.lang.String[] | getAliases() | → inherited from Style |
| Gets all aliases of this style. If style has no aliases then empty array of string is returned. | ||
int | getAlignment() | |
void | setAlignment(int value) | |
| Specifies the alignment for the table style. The value of the property is TableAlignment integer constant. | ||
boolean | getAllowBreakAcrossPages() | |
void | setAllowBreakAcrossPages(boolean value) | |
| Gets or sets a flag indicating whether text in a table row is allowed to split across a page break. | ||
java.lang.String | getBaseStyleName() | → inherited from Style |
void | setBaseStyleName(java.lang.String value) | |
| Gets/sets the name of the style this style is based on. | ||
boolean | getBidi() | |
void | setBidi(boolean value) | |
| 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(double value) | |
| Gets or sets the amount of space (in points) to add below the contents of table cells. | ||
boolean | getBuiltIn() | → inherited from Style |
| True if this style is one of the built-in styles in MS Word. | ||
double | getCellSpacing() | |
void | setCellSpacing(double value) | |
| Gets or sets the amount of space (in points) between the cells. | ||
int | getColumnStripe() | |
void | setColumnStripe(int value) | |
| 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() | → inherited from Style |
| Gets the owner document. | ||
Font | getFont() | → inherited from Style |
| Gets the character formatting of the style. | ||
boolean | isHeading() | → inherited from Style |
| True when the style is one of the built-in Heading styles. | ||
boolean | isQuickStyle() | → inherited from Style |
void | isQuickStyle(boolean value) | |
| Specifies whether this style is shown in the Quick Style gallery inside MS Word UI. | ||
double | getLeftIndent() | |
void | setLeftIndent(double value) | |
| Gets or sets the value that represents the left indent of a table. | ||
double | getLeftPadding() | |
void | setLeftPadding(double value) | |
| Gets or sets the amount of space (in points) to add to the left of the contents of table cells. | ||
java.lang.String | getLinkedStyleName() | → inherited from Style |
|
Gets the name of the |
||
List | getList() | → inherited from Style |
| Gets the list that defines formatting of this list style. | ||
ListFormat | getListFormat() | → inherited from Style |
| Provides access to the list formatting properties of a paragraph style. | ||
java.lang.String | getName() | → inherited from Style |
void | setName(java.lang.String value) | |
| Gets or sets the name of the style. | ||
java.lang.String | getNextParagraphStyleName() | → inherited from 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. | ||
ParagraphFormat | getParagraphFormat() | → inherited from Style |
| Gets the paragraph formatting of the style. | ||
double | getRightPadding() | |
void | setRightPadding(double value) | |
| Gets or sets the amount of space (in points) to add to the right of the contents of table cells. | ||
int | getRowStripe() | |
void | setRowStripe(int value) | |
| 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() | → inherited from Style |
| Gets the locale independent style identifier for a built-in style. The value of the property is StyleIdentifier integer constant. | ||
StyleCollection | getStyles() | → inherited from Style |
| Gets the collection of styles this style belongs to. | ||
double | getTopPadding() | |
void | setTopPadding(double value) | |
| Gets or sets the amount of space (in points) to add above the contents of table cells. | ||
int | getType() | → inherited from Style |
| Gets the style type (paragraph or character). The value of the property is StyleType integer constant. | ||
int | getVerticalAlignment() | |
void | setVerticalAlignment(int value) | |
| Specifies the vertical alignment for the cells. The value of the property is CellVerticalAlignment integer constant. | ||
| Method Summary | ||
|---|---|---|
boolean | equals(Style style) | → inherited from 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() | → inherited from Style |
| Removes the specified style from the document. | ||
| Property Getters/Setters Detail |
|---|
getAliases | → inherited from Style |
public java.lang.String[] getAliases() | |
Example:
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());getAlignment/setAlignment | |
public int getAlignment() / public void setAlignment(int value) | |
Example:
Shows how to set the position of a table.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Below are two ways of aligning a table horizontally.
// 1 - Use the "Alignment" property 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);
// 2 - Use the "LeftIndent" to specify an indent from the left margin of the page:
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.SetTableAlignment.docx");getAllowBreakAcrossPages/setAllowBreakAcrossPages | |
public boolean getAllowBreakAcrossPages() / public void setAllowBreakAcrossPages(boolean value) | |
true.
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);
tableStyle.setVerticalAlignment(CellVerticalAlignment.CENTER);
table.setStyle(tableStyle);
// Setting the style properties of a table may affect the properties of the table itself.
Assert.assertTrue(table.getBidi());
Assert.assertEquals(5.0d, table.getCellSpacing());
Assert.assertEquals("MyTableStyle1", table.getStyleName());
doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");getBaseStyleName/setBaseStyleName | → inherited from Style |
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");
// 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());getBidi/setBidi | |
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);
tableStyle.setVerticalAlignment(CellVerticalAlignment.CENTER);
table.setStyle(tableStyle);
// Setting the style properties of a table may affect the properties of the table itself.
Assert.assertTrue(table.getBidi());
Assert.assertEquals(5.0d, table.getCellSpacing());
Assert.assertEquals("MyTableStyle1", table.getStyleName());
doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");getBorders | |
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);
tableStyle.setVerticalAlignment(CellVerticalAlignment.CENTER);
table.setStyle(tableStyle);
// Setting the style properties of a table may affect the properties of the table itself.
Assert.assertTrue(table.getBidi());
Assert.assertEquals(5.0d, table.getCellSpacing());
Assert.assertEquals("MyTableStyle1", table.getStyleName());
doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");getBottomPadding/setBottomPadding | |
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);
tableStyle.setVerticalAlignment(CellVerticalAlignment.CENTER);
table.setStyle(tableStyle);
// Setting the style properties of a table may affect the properties of the table itself.
Assert.assertTrue(table.getBidi());
Assert.assertEquals(5.0d, table.getCellSpacing());
Assert.assertEquals("MyTableStyle1", table.getStyleName());
doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");getBuiltIn | → inherited from Style |
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());getCellSpacing/setCellSpacing | |
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);
tableStyle.setVerticalAlignment(CellVerticalAlignment.CENTER);
table.setStyle(tableStyle);
// Setting the style properties of a table may affect the properties of the table itself.
Assert.assertTrue(table.getBidi());
Assert.assertEquals(5.0d, table.getCellSpacing());
Assert.assertEquals("MyTableStyle1", table.getStyleName());
doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");getColumnStripe/setColumnStripe | |
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);
// We can configure a conditional style of a table 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 single columns and rows will band the columns 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();
// Apply a line style to 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);
table.setStyle(tableStyle);
// The "StyleOptions" property enables row banding by default.
Assert.assertEquals(TableStyleOptions.FIRST_ROW | TableStyleOptions.FIRST_COLUMN | TableStyleOptions.ROW_BANDS,
table.getStyleOptions());
// Use the "StyleOptions" property also to enable column banding.
table.setStyleOptions(table.getStyleOptions() | TableStyleOptions.COLUMN_BANDS);
doc.save(getArtifactsDir() + "Table.AlternatingRowStyles.docx");getConditionalStyles | |
public ConditionalStyleCollection getConditionalStyles() | |
Example:
Shows how to work with certain area styles of a table.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
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 table's cells
// based on a predicate, such as the cells being in the last row.
// Below are three ways of accessing a table style's conditional styles from the "ConditionalStyles" collection.
// 1 - By style type:
tableStyle.getConditionalStyles().getByConditionalStyleType(ConditionalStyleType.FIRST_ROW).getShading().setBackgroundPatternColor(Color.BLUE);
// 2 - 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());
// 3 - As a property:
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 the custom style, which contains all conditional styles, to the table.
table.setStyle(tableStyle);
// Our style applies some conditional styles by default.
Assert.assertEquals(TableStyleOptions.FIRST_ROW | TableStyleOptions.FIRST_COLUMN | TableStyleOptions.ROW_BANDS,
table.getStyleOptions());
// We will need to enable all other styles ourselves via the "StyleOptions" property.
table.setStyleOptions(table.getStyleOptions() | TableStyleOptions.LAST_ROW | TableStyleOptions.LAST_COLUMN);
doc.save(getArtifactsDir() + "Table.ConditionalStyles.docx");getDocument | → inherited from Style |
public DocumentBase getDocument() | |
Example:
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);
}getFont | → inherited from Style |
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 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");Example:
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);
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());isHeading | → inherited from Style |
public boolean isHeading() | |
Example:
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);
}isQuickStyle/isQuickStyle | → inherited from Style |
public boolean isQuickStyle() / public void isQuickStyle(boolean value) | |
Example:
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);
}getLeftIndent/setLeftIndent | |
public double getLeftIndent() / public void setLeftIndent(double value) | |
Example:
Shows how to set the position of a table.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Below are two ways of aligning a table horizontally.
// 1 - Use the "Alignment" property 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);
// 2 - Use the "LeftIndent" to specify an indent from the left margin of the page:
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.SetTableAlignment.docx");getLeftPadding/setLeftPadding | |
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);
tableStyle.setVerticalAlignment(CellVerticalAlignment.CENTER);
table.setStyle(tableStyle);
// Setting the style properties of a table may affect the properties of the table itself.
Assert.assertTrue(table.getBidi());
Assert.assertEquals(5.0d, table.getCellSpacing());
Assert.assertEquals("MyTableStyle1", table.getStyleName());
doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");getLinkedStyleName | → inherited from Style |
public java.lang.String getLinkedStyleName() | |
Example:
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());getList | → inherited from Style |
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();
// 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");getListFormat | → inherited from Style |
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 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");getName/setName | → inherited from Style |
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 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);
}Example:
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());getNextParagraphStyleName/setNextParagraphStyleName | → inherited from Style |
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();
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);
}getParagraphFormat | → inherited from Style |
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 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");getRightPadding/setRightPadding | |
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);
tableStyle.setVerticalAlignment(CellVerticalAlignment.CENTER);
table.setStyle(tableStyle);
// Setting the style properties of a table may affect the properties of the table itself.
Assert.assertTrue(table.getBidi());
Assert.assertEquals(5.0d, table.getCellSpacing());
Assert.assertEquals("MyTableStyle1", table.getStyleName());
doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");getRowStripe/setRowStripe | |
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);
// We can configure a conditional style of a table 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 single columns and rows will band the columns 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();
// Apply a line style to 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);
table.setStyle(tableStyle);
// The "StyleOptions" property enables row banding by default.
Assert.assertEquals(TableStyleOptions.FIRST_ROW | TableStyleOptions.FIRST_COLUMN | TableStyleOptions.ROW_BANDS,
table.getStyleOptions());
// Use the "StyleOptions" property also to enable column banding.
table.setStyleOptions(table.getStyleOptions() | TableStyleOptions.COLUMN_BANDS);
doc.save(getArtifactsDir() + "Table.AlternatingRowStyles.docx");getShading | |
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);
tableStyle.setVerticalAlignment(CellVerticalAlignment.CENTER);
table.setStyle(tableStyle);
// Setting the style properties of a table may affect the properties of the table itself.
Assert.assertTrue(table.getBidi());
Assert.assertEquals(5.0d, table.getCellSpacing());
Assert.assertEquals("MyTableStyle1", table.getStyleName());
doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");getStyleIdentifier | → inherited from Style |
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 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");getStyles | → inherited from Style |
public StyleCollection getStyles() | |
Example:
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);
}getTopPadding/setTopPadding | |
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);
tableStyle.setVerticalAlignment(CellVerticalAlignment.CENTER);
table.setStyle(tableStyle);
// Setting the style properties of a table may affect the properties of the table itself.
Assert.assertTrue(table.getBidi());
Assert.assertEquals(5.0d, table.getCellSpacing());
Assert.assertEquals("MyTableStyle1", table.getStyleName());
doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");getType | → inherited from Style |
public int getType() | |
Example:
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);
}getVerticalAlignment/setVerticalAlignment | |
public int getVerticalAlignment() / public void setVerticalAlignment(int 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);
tableStyle.setVerticalAlignment(CellVerticalAlignment.CENTER);
table.setStyle(tableStyle);
// Setting the style properties of a table may affect the properties of the table itself.
Assert.assertTrue(table.getBidi());
Assert.assertEquals(5.0d, table.getCellSpacing());
Assert.assertEquals("MyTableStyle1", table.getStyleName());
doc.save(getArtifactsDir() + "Table.TableStyleCreation.docx");| Method Detail |
|---|
equals | → inherited from Style |
public boolean equals(Style style) | |
Example:
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());remove | → inherited from Style |
public void remove() | |
Example:
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);
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());