Aspose.Words

Applying Formatting to Table, Row and Cell

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.

Example TableBordersOutline

Shows how to apply a outline border to a table.

[Java]

 

Document doc = new Document(getMyDir() + "Table.EmptyTable.doc");

Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);

 

// Align the table to the center of the page.

table.setAlignment(TableAlignment.CENTER);

 

// Clear any existing borders from the table.

table.clearBorders();

 

// Set a green border around the table but not inside.

table.setBorder(BorderType.LEFT, LineStyle.SINGLE, 1.5, Color.GREEN, true);

table.setBorder(BorderType.RIGHT, LineStyle.SINGLE, 1.5, Color.GREEN, true);

table.setBorder(BorderType.TOP, LineStyle.SINGLE, 1.5, Color.GREEN, true);

table.setBorder(BorderType.BOTTOM, LineStyle.SINGLE, 1.5, Color.GREEN, true);

 

// Fill the cells with a light green solid color.

table.setShading(TextureIndex.TEXTURE_SOLID, Color.GREEN, Color.GREEN);

 

doc.save(getMyDir() + "Table.SetOutlineBorders Out.doc");

 

 

Example TableBordersAll

Shows how to build a table with all borders enabled (grid).

[Java]

 

Document doc = new Document(getMyDir() + "Table.EmptyTable.doc");

Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);

 

// Clear any existing borders from the table.

table.clearBorders();

 

// Set a green border around and inside the table.

table.setBorders(LineStyle.SINGLE, 1.5, Color.GREEN);

 

doc.save(getMyDir() + "Table.SetAllBorders Out.doc");

 

 

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.

Example RowFormatProperties

Shows how to modify formatting of a table row.

[Java]

 

Document doc = new Document(getMyDir() + "Table.Document.doc");

Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);

 

// Retrieve the first row in the table.

Row firstRow = table.getFirstRow();

 

// Modify some row level properties.

firstRow.getRowFormat().getBorders().setLineStyle(LineStyle.NONE);

firstRow.getRowFormat().setHeightRule(HeightRule.AUTO);

firstRow.getRowFormat().setAllowBreakAcrossPages(true);

 

 

Applying Formatting on the Cell Level

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

Example CellFormatProperties

Shows how to modify formatting of a table cell.

[Java]

 

Document doc = new Document(getMyDir() + "Table.Document.doc");

Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);

 

// Retrieve the first cell in the table.

Cell firstCell = table.getFirstRow().getFirstCell();

 

// Modify some row level properties.

firstCell.getCellFormat().setWidth(30); // in points

firstCell.getCellFormat().setOrientation(TextOrientation.DOWNWARD);

firstCell.getCellFormat().getShading().setForegroundPatternColor(Color.GREEN);

 

 

Applying Borders and Shading

Borders and shading can be applied either table wide using Table.SetBorder, Table.SetBorders and Table.SetShading or to particular cells only by using CellFormat.Borders and CellFormat.Shading. Additionally borders can be set on a row by using RowFormat.Borders, however shading cannot be applied in this way.

Example TableBordersAndShading

Shows how to format table and cell with different borders and shadings

[Java]

 

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

 

Table table = builder.startTable();

builder.insertCell();

 

// Set the borders for the entire table.

table.setBorders(LineStyle.SINGLE, 2.0, Color.BLACK);

// Set the cell shading for this cell.

builder.getCellFormat().getShading().setBackgroundPatternColor(Color.RED);

builder.writeln("Cell #1");

 

builder.insertCell();

// Specify a different cell shading for the second cell.

builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN);

builder.writeln("Cell #2");

 

// End this row.

builder.endRow();

 

// Clear the cell formatting from previous operations.

builder.getCellFormat().clearFormatting();

 

// Create the second row.

builder.insertCell();

 

// Create larger borders for the first cell of this row. This will be different

// compared to the borders set for the table.

builder.getCellFormat().getBorders().getLeft().setLineWidth(4.0);

builder.getCellFormat().getBorders().getRight().setLineWidth(4.0);

builder.getCellFormat().getBorders().getTop().setLineWidth(4.0);

builder.getCellFormat().getBorders().getBottom().setLineWidth(4.0);

builder.writeln("Cell #3");

 

builder.insertCell();

// Clear the cell formatting from the previous cell.

builder.getCellFormat().clearFormatting();

builder.writeln("Cell #4");

 

doc.save(getMyDir() + "Table.SetBordersAndShading Out.doc");