Skip to end of metadata
Go to start of metadata
Contents Summary

Formatting Overview

Each element of a table can be applied with different formatting. For instance, table formatting will be applied over the entire table while row formatting will only affect particular rows etc.

Aspose.Words provides a rich API to retrieve and apply formatting to a table. You can use the Table, RowFormat and CellFormat nodes to set formatting.

Applying Formatting on the Table Level

To apply formatting to a table you can use the properties available on the corresponding Table node. A visual view of table formatting features in Microsoft Word and their corresponding properties in Aspose.Words are given below.

Below example shows how to apply a outline border to a table.

Below example shows how to build a table with all borders enabled (grid).

Note that before you apply table properties there must be at least one row present in the table. This means when building a table using DocumentBuilder, such formatting must be done after the first call to DocumentBuilder.InsertCell or after adding the first row to a table or when inserting nodes directly into the DOM.

Applying Formatting on the Row Level

Formatting on the row level can be controlled using the RowFormat property of the Row.

Below example shows how to modify formatting of a table row.

Applying Formatting on the Cell Level

Formatting on the cell level is controlled using the CellFormat property of the Cell.

Below example shows how to modify formatting of a table cell.

Following code example shows how to set the amount of space (in points) to add to the left/top/right/bottom of the contents of cell.

Specifying Row Heights

The height of a table row is controlled using height and height rule properties. These can be set differently for each row in the table which allows for wide control over the height of each row. In Aspose.Words these are represented by the RowFormat.Height and RowFormat.HeightRule properties of the given Row.

HeightRule Value



This is the default height rule given to a new row. Technically this means that no height rule is defined. The row is sized to fit the largest content within the cells of the row.

At Least

With this setting the height of the row will grow to accommodate the content of the row, but will never be smaller than the specified size in RowFormat.Height .


The size of the row is set exactly to the value found in RowFormat.Height and does not grow to fit content.

The simplest way to set row height is using DocumentBuilder. Using the appropriate RowFormat properties you can set a default height setting or apply a different height for each row in the table.

Below example shows how to create a table that contains a single cell and apply row formatting.

Applying Borders and Shading

Borders and shading can be applied either table wide using Table.setBorder(int,int,double,java.awt.Color,boolean), Table.setBorders(int,double,java.awt.Color) and Table.setShading(int,java.awt.Color,java.awt.Color) or to particular cells only by using CellFormat.getBorders() and CellFormat.getShading(). Additionally borders can be set on a row by using RowFormat.getBorders(), however shading cannot be applied in this way.

Below example shows how to format table and cell with different borders and shadings.

Get Distance between Table and Surrounding Text

Below example shows how to get distance between table and its surrounding text. 

Table Position

How to Get the Table's Alignment

If the table's text wrapping is Around, you can get the table's horizontal and vertical alignment using Table.getRelativeHorizontalAlignment() and Table.getRelativeVerticalAlignment() properties. In other case, you can get table's inline alignment using Table.getAlignment() property. Below example shows how to get the table's alignment.

Get Position of Floating Table

Below example shows how to get the position of floating table. 

Working with Alternative Text of Table

The title and description of table provides alternative text-based representations of the information contained in table. MS Word allows to set this information (Right-click the table, click Table Properties, click the Alt Text tab, and then set the title and description of table). With Aspose.Words you can achieve this by using Table.Title and Table.Description properties. These properties are meaningful for ISO/IEC 29500 compliant DOCX documents. When saved to pre-ISO/IEC 29500 formats, these properties are ignored. Below code example shows how to set table's title and description properties.

Allow Spacing Between Cells

MS Word does not add any additional space between the cells in a table by default. You can find this option in table's properties in MS Word. You can get or set "Allow spacing between cells" option using Table.AllowCellSpacing property. Below code example shows how to set space between cells.

  • No labels