Working with Table Styles

Overview

Aspose.Words supports applying a table style to a table and also reading properties of any table style. Table styles are preserved during loading and saving in the following ways:

  • Table styles in DOCX and WordML formats are preserved when loading and saving to these formats.
  • Table styles are preserved when loading and saving in the DOC format (but not to any other format).
  • When exporting to other formats, rendering or printing, table styles are expanded to direct formatting on the table so all formatting is preserved.

table-style-aspose-words-java

Applying a Table Style

In Aspose.Words you can apply a table style by using any of the Table.getStyle(), Table.setStyleIdentifier(int) and Table.getStyleName() properties. You can also choose which features of the table style to apply, for example, first column, last column, banded rows. These are listed under the TableStyleOptions enumeration and are applied by using Table.setStyleOptions(int) property. The TableStyleOptions enumeration allows a bitwise combination of these features. Below example shows how to build a new table with a table style applied.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
public static void applyATableStyle() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
// We must insert at least one row first before setting any table formatting.
builder.insertCell();
// Set the table style used based of the unique style identifier.
// Note that not all table styles are available when saving as .doc format.
table.setStyleIdentifier(StyleIdentifier.MEDIUM_SHADING_1_ACCENT_1);
// Apply which features should be formatted by the style.
table.setStyleOptions(TableStyleOptions.FIRST_COLUMN | TableStyleOptions.ROW_BANDS | TableStyleOptions.FIRST_ROW);
table.autoFit(AutoFitBehavior.AUTO_FIT_TO_CONTENTS);
// Continue with building the table as normal.
builder.writeln("Item");
builder.getCellFormat().setRightPadding(40);
builder.insertCell();
builder.writeln("Quantity (kg)");
builder.endRow();
builder.insertCell();
builder.writeln("Apples");
builder.insertCell();
builder.writeln("20");
builder.endRow();
builder.insertCell();
builder.writeln("Bananas");
builder.insertCell();
builder.writeln("40");
builder.endRow();
builder.insertCell();
builder.writeln("Carrots");
builder.insertCell();
builder.writeln("50");
builder.endRow();
doc.save(dataDir + "DocumentBuilder.SetTableStyle Out.docx");
}

Aspose.Words also provides a method to take formatting found on a table style and expands it onto the rows and cells of the table as direct formatting. Test combine formatting with table style and cell style. This method will not override any other formatting that is already applied to the table through a row or cell format.

Below examples shows how to expand the formatting from styles onto the rows and cells of the table as direct formatting.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
public static void expandFormattingFromStylesOnToRowsAndCells() throws Exception {
Document doc = new Document(dataDir + "Table.TableStyle.docx");
// Get the first cell of the first table in the document.
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
Cell firstCell = table.getFirstRow().getFirstCell();
// First print the color of the cell shading. This should be empty as the current shading
// is stored in the table style.
Color cellShadingBefore = firstCell.getCellFormat().getShading().getBackgroundPatternColor();
System.out.println("Cell shading before style expansion: " + cellShadingBefore);
// Expand table style formatting to direct formatting.
doc.expandTableStylesToDirectFormatting();
// Now print the cell shading after expanding table styles. A blue background pattern color
// should have been applied from the table style.
Color cellShadingAfter = firstCell.getCellFormat().getShading().getBackgroundPatternColor();
System.out.println("Cell shading after style expansion: " + cellShadingAfter);
}

Using the TableStyle

Aspose.Words provides TableStyle inherited from Style class. The TableStyle facilitates user to apply different styling options like as shading, padding, indentation, CellSpacing and Font etc. Aspose.Words also provides ConditionalStyle class which represents special formatting applied to some area of a table with an assigned table style and the ConditionalStyleCollection representing a collection of ConditionalStyle objects. This collection contains a permanent set of items representing one item for each value of the ConditionalStyleType enumeration type. The ConditionalStyleType enumeration defines all possible table areas to which conditional formatting may be defined in a table style.

Create a Table Style

A table style can be created using the StyleCollection.Add method. The code example given below shows how to create a table style.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
builder.insertCell();
builder.write("Name");
builder.insertCell();
builder.write("Value");
builder.endRow();
builder.insertCell();
builder.insertCell();
builder.endTable();
TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1");
tableStyle.getBorders().setLineStyle(LineStyle.DOUBLE);
tableStyle.getBorders().setLineWidth(1);
tableStyle.setLeftPadding(18);
tableStyle.setRightPadding(18);
tableStyle.setTopPadding(12);
tableStyle.setBottomPadding(12);
table.setStyle(tableStyle);
doc.save(dataDir + "TableStyleCreation.docx");

Defining Conditional Formatting

Conditional formatting can be defined for all possible table area defined under ConditionalStyleType enumeration type. The following code example shows how to define conditional formatting for the header row of the table.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
builder.insertCell();
builder.write("Name");
builder.insertCell();
builder.write("Value");
builder.endRow();
builder.insertCell();
builder.insertCell();
builder.endTable();
TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1");
// Define background color to the first row of table.
tableStyle.getConditionalStyles().getFirstRow().getShading().setBackgroundPatternColor(Color.YELLOW);
tableStyle.getConditionalStyles().getFirstRow().getShading().setTexture(TextureIndex.TEXTURE_NONE);
table.setStyle(tableStyle);
doc.save(dataDir + "TableConditionalStyle.docx");