com.aspose.words

Class CellFormat

  • java.lang.Object
    • com.aspose.words.CellFormat
public class CellFormat 
extends java.lang.Object

Represents all formatting for a table cell.

Example:

Shows how to modify formatting of a table cell.
Document doc = new Document(getMyDir() + "Tables.docx");
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.0); // in points
firstCell.getCellFormat().setOrientation(TextOrientation.DOWNWARD);
firstCell.getCellFormat().getShading().setForegroundPatternColor(Color.GREEN);

doc.save(getArtifactsDir() + "Table.CellFormat.docx");

Example:

Shows how to modify the format of rows and cells.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Table table = builder.startTable();
builder.insertCell();
builder.write("City");
builder.insertCell();
builder.write("Country");
builder.endRow();
builder.insertCell();
builder.write("London");
builder.insertCell();
builder.write("U.K.");
builder.endTable();

// The appearance of rows and individual cells can be edited using the respective formatting objects
RowFormat rowFormat = table.getFirstRow().getRowFormat();
rowFormat.setHeight(25.0);
rowFormat.getBorders().getByBorderType(BorderType.BOTTOM).setColor(Color.RED);

CellFormat cellFormat = table.getLastRow().getFirstCell().getCellFormat();
cellFormat.setWidth(100.0);
cellFormat.getShading().setBackgroundPatternColor(Color.ORANGE);

doc.save(getArtifactsDir() + "Table.RowCellFormat.docx");

Example:

Shows how to build a table with custom borders.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.startTable();

// Setting table formatting options for a document builder
// will apply them to every row and cell that we add with it.
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);

builder.getCellFormat().clearFormatting();
builder.getCellFormat().setWidth(150.0);
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN);
builder.getCellFormat().setWrapText(false);
builder.getCellFormat().setFitText(true);

builder.getRowFormat().clearFormatting();
builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);
builder.getRowFormat().setHeight(50.0);
builder.getRowFormat().getBorders().setLineStyle(LineStyle.ENGRAVE_3_D);
builder.getRowFormat().getBorders().setColor(Color.ORANGE);

builder.insertCell();
builder.write("Row 1, Col 1");

builder.insertCell();
builder.write("Row 1, Col 2");
builder.endRow();

// Changing the formatting will apply it to the current cell,
// and any new cells that we create with the builder afterward.
// This will not affect the cells that we have added previously.
builder.getCellFormat().getShading().clearFormatting();

builder.insertCell();
builder.write("Row 2, Col 1");

builder.insertCell();
builder.write("Row 2, Col 2");

builder.endRow();

// Increase row height to fit the vertical text.
builder.insertCell();
builder.getRowFormat().setHeight(150.0);
builder.getCellFormat().setOrientation(TextOrientation.UPWARD);
builder.write("Row 3, Col 1");

builder.insertCell();
builder.getCellFormat().setOrientation(TextOrientation.DOWNWARD);
builder.write("Row 3, Col 2");

builder.endRow();
builder.endTable();

doc.save(getArtifactsDir() + "DocumentBuilder.InsertTable.docx");

Property Getters/Setters Summary
BorderCollectiongetBorders()
Gets collection of borders of the cell.
doublegetBottomPadding()
void
setBottomPadding(doublevalue)
           Returns or sets the amount of space (in points) to add below the contents of cell.
booleangetFitText()
void
setFitText(booleanvalue)
           If true, fits text in the cell, compressing each paragraph to the width of the cell.
intgetHorizontalMerge()
void
           Specifies how the cell is merged horizontally with other cells in the row. The value of the property is CellMerge integer constant.
doublegetLeftPadding()
void
setLeftPadding(doublevalue)
           Returns or sets the amount of space (in points) to add to the left of the contents of cell.
intgetOrientation()
void
setOrientation(intvalue)
           Returns or sets the orientation of text in a table cell. The value of the property is TextOrientation integer constant.
PreferredWidthgetPreferredWidth()
void
           Returns or sets the preferred width of the cell.
doublegetRightPadding()
void
setRightPadding(doublevalue)
           Returns or sets the amount of space (in points) to add to the right of the contents of cell.
ShadinggetShading()
Returns a Shading object that refers to the shading formatting for the cell.
doublegetTopPadding()
void
setTopPadding(doublevalue)
           Returns or sets the amount of space (in points) to add above the contents of cell.
intgetVerticalAlignment()
void
           Returns or sets the vertical alignment of text in the cell. The value of the property is CellVerticalAlignment integer constant.
intgetVerticalMerge()
void
           Specifies how the cell is merged with other cells vertically. The value of the property is CellMerge integer constant.
doublegetWidth()
void
setWidth(doublevalue)
           Gets the width of the cell in points.
booleangetWrapText()
void
setWrapText(booleanvalue)
           If true, wrap text for the cell.
 
Method Summary
voidclearFormatting()
Resets to default cell formatting. Does not change the width of the cell.
voidsetPaddings(double leftPadding, double topPadding, double rightPadding, double bottomPadding)
Sets the amount of space (in points) to add to the left/top/right/bottom of the contents of cell.
 

    • Property Getters/Setters Detail

      • getBorders

        public BorderCollection getBorders()
        
        Gets collection of borders of the cell.

        Example:

        Shows how to combine the rows from two tables into one.
        // Load the document
        Document doc = new Document(getMyDir() + "Tables.docx");
        
        // Get the first and second table in the document
        // The rows from the second table will be appended to the end of the first table
        Table firstTable = (Table) doc.getChild(NodeType.TABLE, 0, true);
        Table secondTable = (Table) doc.getChild(NodeType.TABLE, 1, true);
        
        // Append all rows from the current table to the next
        // Due to the design of tables even tables with different cell count and widths can be joined into one table
        while (secondTable.hasChildNodes())
            firstTable.getRows().add(secondTable.getFirstRow());
        
        // Remove the empty table container
        secondTable.remove();
        
        doc.save(getArtifactsDir() + "Table.CombineTables.docx");
      • getBottomPadding/setBottomPadding

        public double getBottomPadding() / public void setBottomPadding(double value)
        
        Returns or sets the amount of space (in points) to add below the contents of cell.

        Example:

        Shows how to format cells with a document builder.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        Table table = builder.startTable();
        builder.insertCell();
        builder.write("Row 1, cell 1.");
        
        // Insert a second cell, and then configure cell text padding options.
        // The builder will apply these settings at its current cell, and any new cells creates afterwards.
        builder.insertCell();
        
        CellFormat cellFormat = builder.getCellFormat();
        cellFormat.setWidth(250.0);
        cellFormat.setLeftPadding(30.0);
        cellFormat.setRightPadding(30.0);
        cellFormat.setTopPadding(30.0);
        cellFormat.setBottomPadding(30.0);
        
        builder.write("Row 1, cell 2.");
        builder.endRow();
        builder.endTable();
        
        // The first cell was unaffected by the padding reconfiguration, and still holds the default values.
        Assert.assertEquals(0.0d, table.getFirstRow().getCells().get(0).getCellFormat().getWidth());
        Assert.assertEquals(5.4d, table.getFirstRow().getCells().get(0).getCellFormat().getLeftPadding());
        Assert.assertEquals(5.4d, table.getFirstRow().getCells().get(0).getCellFormat().getRightPadding());
        Assert.assertEquals(0.0d, table.getFirstRow().getCells().get(0).getCellFormat().getTopPadding());
        Assert.assertEquals(0.0d, table.getFirstRow().getCells().get(0).getCellFormat().getBottomPadding());
        
        Assert.assertEquals(250.0d, table.getFirstRow().getCells().get(1).getCellFormat().getWidth());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getLeftPadding());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getRightPadding());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getTopPadding());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getBottomPadding());
        
        // The first cell will still grow in the output document to match the size of its neighboring cell.
        doc.save(getArtifactsDir() + "DocumentBuilder.SetCellFormatting.docx");
      • getFitText/setFitText

        public boolean getFitText() / public void setFitText(boolean value)
        
        If true, fits text in the cell, compressing each paragraph to the width of the cell.

        Example:

        Shows how to build a table with custom borders.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        builder.startTable();
        
        // Setting table formatting options for a document builder
        // will apply them to every row and cell that we add with it.
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
        
        builder.getCellFormat().clearFormatting();
        builder.getCellFormat().setWidth(150.0);
        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
        builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN);
        builder.getCellFormat().setWrapText(false);
        builder.getCellFormat().setFitText(true);
        
        builder.getRowFormat().clearFormatting();
        builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);
        builder.getRowFormat().setHeight(50.0);
        builder.getRowFormat().getBorders().setLineStyle(LineStyle.ENGRAVE_3_D);
        builder.getRowFormat().getBorders().setColor(Color.ORANGE);
        
        builder.insertCell();
        builder.write("Row 1, Col 1");
        
        builder.insertCell();
        builder.write("Row 1, Col 2");
        builder.endRow();
        
        // Changing the formatting will apply it to the current cell,
        // and any new cells that we create with the builder afterward.
        // This will not affect the cells that we have added previously.
        builder.getCellFormat().getShading().clearFormatting();
        
        builder.insertCell();
        builder.write("Row 2, Col 1");
        
        builder.insertCell();
        builder.write("Row 2, Col 2");
        
        builder.endRow();
        
        // Increase row height to fit the vertical text.
        builder.insertCell();
        builder.getRowFormat().setHeight(150.0);
        builder.getCellFormat().setOrientation(TextOrientation.UPWARD);
        builder.write("Row 3, Col 1");
        
        builder.insertCell();
        builder.getCellFormat().setOrientation(TextOrientation.DOWNWARD);
        builder.write("Row 3, Col 2");
        
        builder.endRow();
        builder.endTable();
        
        doc.save(getArtifactsDir() + "DocumentBuilder.InsertTable.docx");
      • getHorizontalMerge/setHorizontalMerge

        public int getHorizontalMerge() / public void setHorizontalMerge(int value)
        
        Specifies how the cell is merged horizontally with other cells in the row. The value of the property is CellMerge integer constant.

        Example:

        Shows how to merge table cells horizontally.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Insert a cell into the first column of the first row.
        // This cell will be the first in a range of horizontally merged cells.
        builder.insertCell();
        builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
        builder.write("Text in merged cells.");
        
        // Insert a cell into the second column of the first row. Instead of adding text contents,
        // we will merge this cell with the first cell that we added directly to the left, and end the row afterward.
        builder.insertCell();
        builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
        builder.endRow();
        
        // Insert two more unmerged cells to the second row.
        builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
        builder.insertCell();
        builder.write("Text in unmerged cell.");
        builder.insertCell();
        builder.write("Text in unmerged cell.");
        builder.endRow();
        builder.endTable();
        
        doc.save(getArtifactsDir() + "CellFormat.HorizontalMerge.docx");

        Example:

        Prints the horizontal and vertical merge type of a cell.
        public void checkCellsMerged() throws Exception {
            Document doc = new Document(getMyDir() + "Table with merged cells.docx");
        
            // Retrieve the first table in the document
            Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
        
            for (Row row : table.getRows()) {
                for (Cell cell : row.getCells()) {
                    System.out.println(printCellMergeType(cell));
                }
            }
        
        }
        
        public String printCellMergeType(final Cell cell) {
            boolean isHorizontallyMerged = cell.getCellFormat().getHorizontalMerge() != CellMerge.NONE;
            boolean isVerticallyMerged = cell.getCellFormat().getVerticalMerge() != CellMerge.NONE;
            String cellLocation = MessageFormat.format("R{0}, C{1}", cell.getParentRow().getParentTable().indexOf(cell.getParentRow()) + 1, cell.getParentRow().indexOf(cell) + 1);
        
            if (isHorizontallyMerged && isVerticallyMerged) {
                return MessageFormat.format("The cell at {0} is both horizontally and vertically merged", cellLocation);
            } else if (isHorizontallyMerged) {
                return MessageFormat.format("The cell at {0} is horizontally merged.", cellLocation);
            } else if (isVerticallyMerged) {
                return MessageFormat.format("The cell at {0} is vertically merged", cellLocation);
            } else {
                return MessageFormat.format("The cell at {0} is not merged", cellLocation);
            }
        }
        See Also:
        VerticalMerge
      • getLeftPadding/setLeftPadding

        public double getLeftPadding() / public void setLeftPadding(double value)
        
        Returns or sets the amount of space (in points) to add to the left of the contents of cell.

        Example:

        Shows how to format cells with a document builder.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        Table table = builder.startTable();
        builder.insertCell();
        builder.write("Row 1, cell 1.");
        
        // Insert a second cell, and then configure cell text padding options.
        // The builder will apply these settings at its current cell, and any new cells creates afterwards.
        builder.insertCell();
        
        CellFormat cellFormat = builder.getCellFormat();
        cellFormat.setWidth(250.0);
        cellFormat.setLeftPadding(30.0);
        cellFormat.setRightPadding(30.0);
        cellFormat.setTopPadding(30.0);
        cellFormat.setBottomPadding(30.0);
        
        builder.write("Row 1, cell 2.");
        builder.endRow();
        builder.endTable();
        
        // The first cell was unaffected by the padding reconfiguration, and still holds the default values.
        Assert.assertEquals(0.0d, table.getFirstRow().getCells().get(0).getCellFormat().getWidth());
        Assert.assertEquals(5.4d, table.getFirstRow().getCells().get(0).getCellFormat().getLeftPadding());
        Assert.assertEquals(5.4d, table.getFirstRow().getCells().get(0).getCellFormat().getRightPadding());
        Assert.assertEquals(0.0d, table.getFirstRow().getCells().get(0).getCellFormat().getTopPadding());
        Assert.assertEquals(0.0d, table.getFirstRow().getCells().get(0).getCellFormat().getBottomPadding());
        
        Assert.assertEquals(250.0d, table.getFirstRow().getCells().get(1).getCellFormat().getWidth());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getLeftPadding());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getRightPadding());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getTopPadding());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getBottomPadding());
        
        // The first cell will still grow in the output document to match the size of its neighboring cell.
        doc.save(getArtifactsDir() + "DocumentBuilder.SetCellFormatting.docx");
      • getOrientation/setOrientation

        public int getOrientation() / public void setOrientation(int value)
        
        Returns or sets the orientation of text in a table cell. The value of the property is TextOrientation integer constant.

        Example:

        Shows how to build a table with custom borders.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        builder.startTable();
        
        // Setting table formatting options for a document builder
        // will apply them to every row and cell that we add with it.
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
        
        builder.getCellFormat().clearFormatting();
        builder.getCellFormat().setWidth(150.0);
        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
        builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN);
        builder.getCellFormat().setWrapText(false);
        builder.getCellFormat().setFitText(true);
        
        builder.getRowFormat().clearFormatting();
        builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);
        builder.getRowFormat().setHeight(50.0);
        builder.getRowFormat().getBorders().setLineStyle(LineStyle.ENGRAVE_3_D);
        builder.getRowFormat().getBorders().setColor(Color.ORANGE);
        
        builder.insertCell();
        builder.write("Row 1, Col 1");
        
        builder.insertCell();
        builder.write("Row 1, Col 2");
        builder.endRow();
        
        // Changing the formatting will apply it to the current cell,
        // and any new cells that we create with the builder afterward.
        // This will not affect the cells that we have added previously.
        builder.getCellFormat().getShading().clearFormatting();
        
        builder.insertCell();
        builder.write("Row 2, Col 1");
        
        builder.insertCell();
        builder.write("Row 2, Col 2");
        
        builder.endRow();
        
        // Increase row height to fit the vertical text.
        builder.insertCell();
        builder.getRowFormat().setHeight(150.0);
        builder.getCellFormat().setOrientation(TextOrientation.UPWARD);
        builder.write("Row 3, Col 1");
        
        builder.insertCell();
        builder.getCellFormat().setOrientation(TextOrientation.DOWNWARD);
        builder.write("Row 3, Col 2");
        
        builder.endRow();
        builder.endTable();
        
        doc.save(getArtifactsDir() + "DocumentBuilder.InsertTable.docx");

        Example:

        Shows how to build a formatted 2x2 table.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        Table table = builder.startTable();
        builder.insertCell();
        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
        builder.write("Row 1, cell 1.");
        builder.insertCell();
        builder.write("Row 1, cell 2.");
        builder.endRow();
        
        // While building the table, the document builder will apply its current RowFormat/CellFormat attribute values
        // to the current row/cell that its cursor is in and any new rows/cells as it creates them.
        Assert.assertEquals(CellVerticalAlignment.CENTER, table.getRows().get(0).getCells().get(0).getCellFormat().getVerticalAlignment());
        Assert.assertEquals(CellVerticalAlignment.CENTER, table.getRows().get(0).getCells().get(1).getCellFormat().getVerticalAlignment());
        
        builder.insertCell();
        builder.getRowFormat().setHeight(100.0);
        builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);
        builder.getCellFormat().setOrientation(TextOrientation.UPWARD);
        builder.write("Row 2, cell 1.");
        builder.insertCell();
        builder.getCellFormat().setOrientation(TextOrientation.DOWNWARD);
        builder.write("Row 2, cell 2.");
        builder.endRow();
        builder.endTable();
        
        // Previously added rows and cells are not retroactively affected by changes to the builder's formatting.
        Assert.assertEquals(0.0, table.getRows().get(0).getRowFormat().getHeight());
        Assert.assertEquals(HeightRule.AUTO, table.getRows().get(0).getRowFormat().getHeightRule());
        Assert.assertEquals(100.0, table.getRows().get(1).getRowFormat().getHeight());
        Assert.assertEquals(HeightRule.EXACTLY, table.getRows().get(1).getRowFormat().getHeightRule());
        Assert.assertEquals(TextOrientation.UPWARD, table.getRows().get(1).getCells().get(0).getCellFormat().getOrientation());
        Assert.assertEquals(TextOrientation.DOWNWARD, table.getRows().get(1).getCells().get(1).getCellFormat().getOrientation());
        
        doc.save(getArtifactsDir() + "DocumentBuilder.BuildTable.docx");
      • getPreferredWidth/setPreferredWidth

        public PreferredWidth getPreferredWidth() / public void setPreferredWidth(PreferredWidth value)
        
        Returns or sets the preferred width of the cell.

        The preferred width (along with the table's Auto Fit option) determines how the actual width of the cell is calculated by the table layout algorithm. Table layout can be performed by Aspose.Words when it saves the document or by Microsoft Word when it displays the document.

        The preferred width can be specified in points or in percent. The preferred width can also be specified as "auto", which means no preferred width is specified.

        The default value is PreferredWidth.AUTO.

        Example:

        Shows how to set a preferred width for table cells.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        Table table = builder.startTable();
        
        // There are two ways of applying the PreferredWidth class to table cells.
        // 1 -  Set an absolute preferred width based on points.
        builder.insertCell();
        builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(40.0));
        builder.getCellFormat().getShading().setBackgroundPatternColor(Color.YELLOW);
        builder.writeln(MessageFormat.format("Cell with a width of {0}.", builder.getCellFormat().getPreferredWidth()));
        
        // 2 -  Set a relative preferred width based on percent of the table's width.
        builder.insertCell();
        builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(20.0));
        builder.getCellFormat().getShading().setBackgroundPatternColor(Color.BLUE);
        builder.writeln(MessageFormat.format("Cell with a width of {0}.", builder.getCellFormat().getPreferredWidth()));
        
        builder.insertCell();
        
        // A cell with no preferred width specified will take up the rest of the available space.
        builder.getCellFormat().setPreferredWidth(PreferredWidth.AUTO);
        
        // Each configuration of the PreferredWidth attribute creates a new object.
        Assert.assertNotEquals(table.getFirstRow().getCells().get(1).getCellFormat().getPreferredWidth().hashCode(),
            builder.getCellFormat().getPreferredWidth().hashCode());
        
        builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN);
        builder.writeln("Automatically sized cell.");
        
        doc.save(getArtifactsDir() + "DocumentBuilder.InsertCellsWithPreferredWidths.docx");
        See Also:
        Width
      • getRightPadding/setRightPadding

        public double getRightPadding() / public void setRightPadding(double value)
        
        Returns or sets the amount of space (in points) to add to the right of the contents of cell.

        Example:

        Shows how to format cells with a document builder.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        Table table = builder.startTable();
        builder.insertCell();
        builder.write("Row 1, cell 1.");
        
        // Insert a second cell, and then configure cell text padding options.
        // The builder will apply these settings at its current cell, and any new cells creates afterwards.
        builder.insertCell();
        
        CellFormat cellFormat = builder.getCellFormat();
        cellFormat.setWidth(250.0);
        cellFormat.setLeftPadding(30.0);
        cellFormat.setRightPadding(30.0);
        cellFormat.setTopPadding(30.0);
        cellFormat.setBottomPadding(30.0);
        
        builder.write("Row 1, cell 2.");
        builder.endRow();
        builder.endTable();
        
        // The first cell was unaffected by the padding reconfiguration, and still holds the default values.
        Assert.assertEquals(0.0d, table.getFirstRow().getCells().get(0).getCellFormat().getWidth());
        Assert.assertEquals(5.4d, table.getFirstRow().getCells().get(0).getCellFormat().getLeftPadding());
        Assert.assertEquals(5.4d, table.getFirstRow().getCells().get(0).getCellFormat().getRightPadding());
        Assert.assertEquals(0.0d, table.getFirstRow().getCells().get(0).getCellFormat().getTopPadding());
        Assert.assertEquals(0.0d, table.getFirstRow().getCells().get(0).getCellFormat().getBottomPadding());
        
        Assert.assertEquals(250.0d, table.getFirstRow().getCells().get(1).getCellFormat().getWidth());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getLeftPadding());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getRightPadding());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getTopPadding());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getBottomPadding());
        
        // The first cell will still grow in the output document to match the size of its neighboring cell.
        doc.save(getArtifactsDir() + "DocumentBuilder.SetCellFormatting.docx");
      • getShading

        public Shading getShading()
        
        Returns a Shading object that refers to the shading formatting for the cell.

        Example:

        Shows how to modify the format of rows and cells.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        Table table = builder.startTable();
        builder.insertCell();
        builder.write("City");
        builder.insertCell();
        builder.write("Country");
        builder.endRow();
        builder.insertCell();
        builder.write("London");
        builder.insertCell();
        builder.write("U.K.");
        builder.endTable();
        
        // The appearance of rows and individual cells can be edited using the respective formatting objects
        RowFormat rowFormat = table.getFirstRow().getRowFormat();
        rowFormat.setHeight(25.0);
        rowFormat.getBorders().getByBorderType(BorderType.BOTTOM).setColor(Color.RED);
        
        CellFormat cellFormat = table.getLastRow().getFirstCell().getCellFormat();
        cellFormat.setWidth(100.0);
        cellFormat.getShading().setBackgroundPatternColor(Color.ORANGE);
        
        doc.save(getArtifactsDir() + "Table.RowCellFormat.docx");

        Example:

        Shows how to build a table with custom borders.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        builder.startTable();
        
        // Setting table formatting options for a document builder
        // will apply them to every row and cell that we add with it.
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
        
        builder.getCellFormat().clearFormatting();
        builder.getCellFormat().setWidth(150.0);
        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
        builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN);
        builder.getCellFormat().setWrapText(false);
        builder.getCellFormat().setFitText(true);
        
        builder.getRowFormat().clearFormatting();
        builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);
        builder.getRowFormat().setHeight(50.0);
        builder.getRowFormat().getBorders().setLineStyle(LineStyle.ENGRAVE_3_D);
        builder.getRowFormat().getBorders().setColor(Color.ORANGE);
        
        builder.insertCell();
        builder.write("Row 1, Col 1");
        
        builder.insertCell();
        builder.write("Row 1, Col 2");
        builder.endRow();
        
        // Changing the formatting will apply it to the current cell,
        // and any new cells that we create with the builder afterward.
        // This will not affect the cells that we have added previously.
        builder.getCellFormat().getShading().clearFormatting();
        
        builder.insertCell();
        builder.write("Row 2, Col 1");
        
        builder.insertCell();
        builder.write("Row 2, Col 2");
        
        builder.endRow();
        
        // Increase row height to fit the vertical text.
        builder.insertCell();
        builder.getRowFormat().setHeight(150.0);
        builder.getCellFormat().setOrientation(TextOrientation.UPWARD);
        builder.write("Row 3, Col 1");
        
        builder.insertCell();
        builder.getCellFormat().setOrientation(TextOrientation.DOWNWARD);
        builder.write("Row 3, Col 2");
        
        builder.endRow();
        builder.endTable();
        
        doc.save(getArtifactsDir() + "DocumentBuilder.InsertTable.docx");
      • getTopPadding/setTopPadding

        public double getTopPadding() / public void setTopPadding(double value)
        
        Returns or sets the amount of space (in points) to add above the contents of cell.

        Example:

        Shows how to format cells with a document builder.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        Table table = builder.startTable();
        builder.insertCell();
        builder.write("Row 1, cell 1.");
        
        // Insert a second cell, and then configure cell text padding options.
        // The builder will apply these settings at its current cell, and any new cells creates afterwards.
        builder.insertCell();
        
        CellFormat cellFormat = builder.getCellFormat();
        cellFormat.setWidth(250.0);
        cellFormat.setLeftPadding(30.0);
        cellFormat.setRightPadding(30.0);
        cellFormat.setTopPadding(30.0);
        cellFormat.setBottomPadding(30.0);
        
        builder.write("Row 1, cell 2.");
        builder.endRow();
        builder.endTable();
        
        // The first cell was unaffected by the padding reconfiguration, and still holds the default values.
        Assert.assertEquals(0.0d, table.getFirstRow().getCells().get(0).getCellFormat().getWidth());
        Assert.assertEquals(5.4d, table.getFirstRow().getCells().get(0).getCellFormat().getLeftPadding());
        Assert.assertEquals(5.4d, table.getFirstRow().getCells().get(0).getCellFormat().getRightPadding());
        Assert.assertEquals(0.0d, table.getFirstRow().getCells().get(0).getCellFormat().getTopPadding());
        Assert.assertEquals(0.0d, table.getFirstRow().getCells().get(0).getCellFormat().getBottomPadding());
        
        Assert.assertEquals(250.0d, table.getFirstRow().getCells().get(1).getCellFormat().getWidth());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getLeftPadding());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getRightPadding());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getTopPadding());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getBottomPadding());
        
        // The first cell will still grow in the output document to match the size of its neighboring cell.
        doc.save(getArtifactsDir() + "DocumentBuilder.SetCellFormatting.docx");
      • getVerticalAlignment/setVerticalAlignment

        public int getVerticalAlignment() / public void setVerticalAlignment(int value)
        
        Returns or sets the vertical alignment of text in the cell. The value of the property is CellVerticalAlignment integer constant.

        Example:

        Shows how to build a table with custom borders.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        builder.startTable();
        
        // Setting table formatting options for a document builder
        // will apply them to every row and cell that we add with it.
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
        
        builder.getCellFormat().clearFormatting();
        builder.getCellFormat().setWidth(150.0);
        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
        builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN);
        builder.getCellFormat().setWrapText(false);
        builder.getCellFormat().setFitText(true);
        
        builder.getRowFormat().clearFormatting();
        builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);
        builder.getRowFormat().setHeight(50.0);
        builder.getRowFormat().getBorders().setLineStyle(LineStyle.ENGRAVE_3_D);
        builder.getRowFormat().getBorders().setColor(Color.ORANGE);
        
        builder.insertCell();
        builder.write("Row 1, Col 1");
        
        builder.insertCell();
        builder.write("Row 1, Col 2");
        builder.endRow();
        
        // Changing the formatting will apply it to the current cell,
        // and any new cells that we create with the builder afterward.
        // This will not affect the cells that we have added previously.
        builder.getCellFormat().getShading().clearFormatting();
        
        builder.insertCell();
        builder.write("Row 2, Col 1");
        
        builder.insertCell();
        builder.write("Row 2, Col 2");
        
        builder.endRow();
        
        // Increase row height to fit the vertical text.
        builder.insertCell();
        builder.getRowFormat().setHeight(150.0);
        builder.getCellFormat().setOrientation(TextOrientation.UPWARD);
        builder.write("Row 3, Col 1");
        
        builder.insertCell();
        builder.getCellFormat().setOrientation(TextOrientation.DOWNWARD);
        builder.write("Row 3, Col 2");
        
        builder.endRow();
        builder.endTable();
        
        doc.save(getArtifactsDir() + "DocumentBuilder.InsertTable.docx");
      • getVerticalMerge/setVerticalMerge

        public int getVerticalMerge() / public void setVerticalMerge(int value)
        
        Specifies how the cell is merged with other cells vertically. The value of the property is CellMerge integer constant.

        Cells can only be merged vertically if their left and right boundaries are identical.

        When cells are vertically merged, the display areas of the merged cells are consolidated. The consolidated area is used to display the contents of the first vertically merged cell and all other vertically merged cells must be empty.

        Example:

        Shows how to merge table cells vertically.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Insert a cell into the first column of the first row.
        // This cell will be the first in a range of vertically merged cells.
        builder.insertCell();
        builder.getCellFormat().setVerticalMerge(CellMerge.FIRST);
        builder.write("Text in merged cells.");
        
        // Insert a cell into the second column of the first row, then end the row.
        // Also, configure the builder to disable vertical merging in created cells.
        builder.insertCell();
        builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
        builder.write("Text in unmerged cell.");
        builder.endRow();
        
        // Insert a cell into the first column of the second row. 
        // Instead of adding text contents, we will merge this cell with the first cell that we added directly above.
        builder.insertCell();
        builder.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);
        
        // Insert another independent cell in the second column of the second row, and end the table.
        builder.insertCell();
        builder.getCellFormat().setVerticalMerge(CellMerge.NONE);
        builder.write("Text in unmerged cell.");
        builder.endRow();
        builder.endTable();
        
        doc.save(getArtifactsDir() + "CellFormat.VerticalMerge.docx");

        Example:

        Prints the horizontal and vertical merge type of a cell.
        public void checkCellsMerged() throws Exception {
            Document doc = new Document(getMyDir() + "Table with merged cells.docx");
        
            // Retrieve the first table in the document
            Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
        
            for (Row row : table.getRows()) {
                for (Cell cell : row.getCells()) {
                    System.out.println(printCellMergeType(cell));
                }
            }
        
        }
        
        public String printCellMergeType(final Cell cell) {
            boolean isHorizontallyMerged = cell.getCellFormat().getHorizontalMerge() != CellMerge.NONE;
            boolean isVerticallyMerged = cell.getCellFormat().getVerticalMerge() != CellMerge.NONE;
            String cellLocation = MessageFormat.format("R{0}, C{1}", cell.getParentRow().getParentTable().indexOf(cell.getParentRow()) + 1, cell.getParentRow().indexOf(cell) + 1);
        
            if (isHorizontallyMerged && isVerticallyMerged) {
                return MessageFormat.format("The cell at {0} is both horizontally and vertically merged", cellLocation);
            } else if (isHorizontallyMerged) {
                return MessageFormat.format("The cell at {0} is horizontally merged.", cellLocation);
            } else if (isVerticallyMerged) {
                return MessageFormat.format("The cell at {0} is vertically merged", cellLocation);
            } else {
                return MessageFormat.format("The cell at {0} is not merged", cellLocation);
            }
        }
        See Also:
        HorizontalMerge
      • getWidth/setWidth

        public double getWidth() / public void setWidth(double value)
        
        Gets the width of the cell in points.

        The width is calculated by Aspose.Words on document loading and saving. Currently, not every combination of table, cell and document properties is supported. The returned value may not be accurate for some documents. It may not exactly match the cell width as calculated by MS Word when the document is opened in MS Word.

        Setting this property is not recommended. There is no guarantee that the cell will actually have the set width. The width may be adjusted to accommodate cell contents in an auto-fit table layout. Cells in other rows may have conflicting width settings. The table may be resized to fit into the container or to meet table width settings. Consider using PreferredWidth for setting the cell width. Setting this property sets PreferredWidth implicitly since version 15.8.

        Example:

        Shows how to build a table with custom borders.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        builder.startTable();
        
        // Setting table formatting options for a document builder
        // will apply them to every row and cell that we add with it.
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
        
        builder.getCellFormat().clearFormatting();
        builder.getCellFormat().setWidth(150.0);
        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
        builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN);
        builder.getCellFormat().setWrapText(false);
        builder.getCellFormat().setFitText(true);
        
        builder.getRowFormat().clearFormatting();
        builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);
        builder.getRowFormat().setHeight(50.0);
        builder.getRowFormat().getBorders().setLineStyle(LineStyle.ENGRAVE_3_D);
        builder.getRowFormat().getBorders().setColor(Color.ORANGE);
        
        builder.insertCell();
        builder.write("Row 1, Col 1");
        
        builder.insertCell();
        builder.write("Row 1, Col 2");
        builder.endRow();
        
        // Changing the formatting will apply it to the current cell,
        // and any new cells that we create with the builder afterward.
        // This will not affect the cells that we have added previously.
        builder.getCellFormat().getShading().clearFormatting();
        
        builder.insertCell();
        builder.write("Row 2, Col 1");
        
        builder.insertCell();
        builder.write("Row 2, Col 2");
        
        builder.endRow();
        
        // Increase row height to fit the vertical text.
        builder.insertCell();
        builder.getRowFormat().setHeight(150.0);
        builder.getCellFormat().setOrientation(TextOrientation.UPWARD);
        builder.write("Row 3, Col 1");
        
        builder.insertCell();
        builder.getCellFormat().setOrientation(TextOrientation.DOWNWARD);
        builder.write("Row 3, Col 2");
        
        builder.endRow();
        builder.endTable();
        
        doc.save(getArtifactsDir() + "DocumentBuilder.InsertTable.docx");

        Example:

        Shows how to format cells with a document builder.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        Table table = builder.startTable();
        builder.insertCell();
        builder.write("Row 1, cell 1.");
        
        // Insert a second cell, and then configure cell text padding options.
        // The builder will apply these settings at its current cell, and any new cells creates afterwards.
        builder.insertCell();
        
        CellFormat cellFormat = builder.getCellFormat();
        cellFormat.setWidth(250.0);
        cellFormat.setLeftPadding(30.0);
        cellFormat.setRightPadding(30.0);
        cellFormat.setTopPadding(30.0);
        cellFormat.setBottomPadding(30.0);
        
        builder.write("Row 1, cell 2.");
        builder.endRow();
        builder.endTable();
        
        // The first cell was unaffected by the padding reconfiguration, and still holds the default values.
        Assert.assertEquals(0.0d, table.getFirstRow().getCells().get(0).getCellFormat().getWidth());
        Assert.assertEquals(5.4d, table.getFirstRow().getCells().get(0).getCellFormat().getLeftPadding());
        Assert.assertEquals(5.4d, table.getFirstRow().getCells().get(0).getCellFormat().getRightPadding());
        Assert.assertEquals(0.0d, table.getFirstRow().getCells().get(0).getCellFormat().getTopPadding());
        Assert.assertEquals(0.0d, table.getFirstRow().getCells().get(0).getCellFormat().getBottomPadding());
        
        Assert.assertEquals(250.0d, table.getFirstRow().getCells().get(1).getCellFormat().getWidth());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getLeftPadding());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getRightPadding());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getTopPadding());
        Assert.assertEquals(30.0d, table.getFirstRow().getCells().get(1).getCellFormat().getBottomPadding());
        
        // The first cell will still grow in the output document to match the size of its neighboring cell.
        doc.save(getArtifactsDir() + "DocumentBuilder.SetCellFormatting.docx");
        See Also:
        PreferredWidth
      • getWrapText/setWrapText

        public boolean getWrapText() / public void setWrapText(boolean value)
        
        If true, wrap text for the cell.

        Example:

        Shows how to build a table with custom borders.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        builder.startTable();
        
        // Setting table formatting options for a document builder
        // will apply them to every row and cell that we add with it.
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
        
        builder.getCellFormat().clearFormatting();
        builder.getCellFormat().setWidth(150.0);
        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
        builder.getCellFormat().getShading().setBackgroundPatternColor(Color.GREEN);
        builder.getCellFormat().setWrapText(false);
        builder.getCellFormat().setFitText(true);
        
        builder.getRowFormat().clearFormatting();
        builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);
        builder.getRowFormat().setHeight(50.0);
        builder.getRowFormat().getBorders().setLineStyle(LineStyle.ENGRAVE_3_D);
        builder.getRowFormat().getBorders().setColor(Color.ORANGE);
        
        builder.insertCell();
        builder.write("Row 1, Col 1");
        
        builder.insertCell();
        builder.write("Row 1, Col 2");
        builder.endRow();
        
        // Changing the formatting will apply it to the current cell,
        // and any new cells that we create with the builder afterward.
        // This will not affect the cells that we have added previously.
        builder.getCellFormat().getShading().clearFormatting();
        
        builder.insertCell();
        builder.write("Row 2, Col 1");
        
        builder.insertCell();
        builder.write("Row 2, Col 2");
        
        builder.endRow();
        
        // Increase row height to fit the vertical text.
        builder.insertCell();
        builder.getRowFormat().setHeight(150.0);
        builder.getCellFormat().setOrientation(TextOrientation.UPWARD);
        builder.write("Row 3, Col 1");
        
        builder.insertCell();
        builder.getCellFormat().setOrientation(TextOrientation.DOWNWARD);
        builder.write("Row 3, Col 2");
        
        builder.endRow();
        builder.endTable();
        
        doc.save(getArtifactsDir() + "DocumentBuilder.InsertTable.docx");
    • Method Detail

      • clearFormatting

        public void clearFormatting()
        Resets to default cell formatting. Does not change the width of the cell.

        Example:

        Shows how to combine the rows from two tables into one.
        // Load the document
        Document doc = new Document(getMyDir() + "Tables.docx");
        
        // Get the first and second table in the document
        // The rows from the second table will be appended to the end of the first table
        Table firstTable = (Table) doc.getChild(NodeType.TABLE, 0, true);
        Table secondTable = (Table) doc.getChild(NodeType.TABLE, 1, true);
        
        // Append all rows from the current table to the next
        // Due to the design of tables even tables with different cell count and widths can be joined into one table
        while (secondTable.hasChildNodes())
            firstTable.getRows().add(secondTable.getFirstRow());
        
        // Remove the empty table container
        secondTable.remove();
        
        doc.save(getArtifactsDir() + "Table.CombineTables.docx");
      • setPaddings

        public void setPaddings(double leftPadding, double topPadding, double rightPadding, double bottomPadding)
        Sets the amount of space (in points) to add to the left/top/right/bottom of the contents of cell.

        Example:

        Shows how to pad the contents of a cell with whitespace.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Set a padding distance (in points) between the border and the text contents
        // of each table cell we create with the document builder. 
        builder.getCellFormat().setPaddings(5.0, 10.0, 40.0, 50.0);
        
        // Create a table with a cell, and add contents which will be padded by whitespace.
        builder.startTable();
        builder.insertCell();
        builder.write("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
                      "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.");
        
        doc.save(getArtifactsDir() + "CellFormat.Padding.docx");