Working with Table Styles


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.


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.

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.

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.

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.