com.aspose.words

Class RowFormat

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

Represents all formatting for a table row.

Example:

Shows how to modify formatting of a table row.
Document doc = new Document(getMyDir() + "Tables.docx");
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);

doc.save(getArtifactsDir() + "Table.RowFormat.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
booleangetAllowBreakAcrossPages()
void
           True if the text in a table row is allowed to split across a page break.
BorderCollectiongetBorders()
Gets the collection of default cell borders for the row.
booleangetHeadingFormat()
void
setHeadingFormat(booleanvalue)
           True if the row is repeated as a table heading on every page when the table spans more than one page.
doublegetHeight()
void
setHeight(doublevalue)
           Gets or sets the height of the table row in points.
intgetHeightRule()
void
setHeightRule(intvalue)
           Gets or sets the rule for determining the height of the table row. The value of the property is HeightRule integer constant.
 
Method Summary
voidclearFormatting()
Resets to default row formatting.
 

    • Property Getters/Setters Detail

      • getAllowBreakAcrossPages/setAllowBreakAcrossPages

        public boolean getAllowBreakAcrossPages() / public void setAllowBreakAcrossPages(boolean value)
        
        True if the text in a table row is allowed to split across a page break.

        Example:

        Shows how to disable rows breaking across pages for every row in a table.
        // Disable breaking across pages for all rows in the table
        Document doc = new Document(getMyDir() + "Table spanning two pages.docx");
        
        // Retrieve the first table in the document
        Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
        
        for (Row row : table) {
            row.getRowFormat().setAllowBreakAcrossPages(false);
        }
        
        doc.save(getArtifactsDir() + "Table.DisableBreakAcrossPages.docx");
      • getBorders

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

        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");
      • getHeadingFormat/setHeadingFormat

        public boolean getHeadingFormat() / public void setHeadingFormat(boolean value)
        
        True if the row is repeated as a table heading on every page when the table spans more than one page.

        Example:

        Shows how to build a table with rows that repeat on every page.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        Table table = builder.startTable();
        
        // Any rows inserted while the HeadingFormat flag is set to true
        // will show up at the top of the table on every page that it spans.
        builder.getRowFormat().setHeadingFormat(true);
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
        builder.getCellFormat().setWidth(100.0);
        builder.insertCell();
        builder.write("Heading row 1");
        builder.endRow();
        builder.insertCell();
        builder.write("Heading row 2");
        builder.endRow();
        
        builder.getCellFormat().setWidth(50.0);
        builder.getParagraphFormat().clearFormatting();
        builder.getRowFormat().setHeadingFormat(false);
        
        // Add enough rows for the table to span two pages.
        for (int i = 0; i < 50; i++)
        {
            builder.insertCell();
            builder.write(MessageFormat.format("Row {0}, column 1.", table.getRows().toArray().length));
            builder.insertCell();
            builder.write(MessageFormat.format("Row {0}, column 2.", table.getRows().toArray().length));
            builder.endRow();
        }
        
        doc.save(getArtifactsDir() + "DocumentBuilder.InsertTableSetHeadingRow.docx");
      • getHeight/setHeight

        public double getHeight() / public void setHeight(double value)
        
        Gets or sets the height of the table row in points.

        Example:

        Shows how to format rows 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.");
        
        // Start a second row, and then configure its height. The builder will apply these settings to
        // its current row, as well as any new rows it creates afterwards.
        builder.endRow();
        
        RowFormat rowFormat = builder.getRowFormat();
        rowFormat.setHeight(100.0);
        rowFormat.setHeightRule(HeightRule.EXACTLY);
        
        builder.insertCell();
        builder.write("Row 2, cell 1.");
        builder.endTable();
        
        // The first row was unaffected by the padding reconfiguration and still holds the default values.
        Assert.assertEquals(0.0d, table.getRows().get(0).getRowFormat().getHeight());
        Assert.assertEquals(HeightRule.AUTO, table.getRows().get(0).getRowFormat().getHeightRule());
        
        Assert.assertEquals(100.0d, table.getRows().get(1).getRowFormat().getHeight());
        Assert.assertEquals(HeightRule.EXACTLY, table.getRows().get(1).getRowFormat().getHeightRule());
        
        doc.save(getArtifactsDir() + "DocumentBuilder.SetRowFormatting.docx");

        Example:

        Shows how to create a formatted table using DocumentBuilder.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        Table table = builder.startTable();
        builder.insertCell();
        table.setLeftIndent(20.0);
        
        // Set some formatting options for text and table appearance.
        builder.getRowFormat().setHeight(40.0);
        builder.getRowFormat().setHeightRule(HeightRule.AT_LEAST);
        builder.getCellFormat().getShading().setBackgroundPatternColor(new Color((198), (217), (241)));
        
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
        builder.getFont().setSize(16.0);
        builder.getFont().setName("Arial");
        builder.getFont().setBold(true);
        
        // Configuring the formatting options in a document builder will apply them
        // to the current cell/row its cursor is in,
        // as well as any new cells and rows created using that builder.
        builder.write("Header Row,\n Cell 1");
        builder.insertCell();
        builder.write("Header Row,\n Cell 2");
        builder.insertCell();
        builder.write("Header Row,\n Cell 3");
        builder.endRow();
        
        // Reconfigure the builder's formatting objects for new rows and cells that we are about to make.
        // The builder will not apply these to the first row already created so that it will stand out as a header row.
        builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);
        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
        builder.getRowFormat().setHeight(30.0);
        builder.getRowFormat().setHeightRule(HeightRule.AUTO);
        builder.insertCell();
        builder.getFont().setSize(12.0);
        builder.getFont().setBold(false);
        
        builder.write("Row 1, Cell 1.");
        builder.insertCell();
        builder.write("Row 1, Cell 2.");
        builder.insertCell();
        builder.write("Row 1, Cell 3.");
        builder.endRow();
        builder.insertCell();
        builder.write("Row 2, Cell 1.");
        builder.insertCell();
        builder.write("Row 2, Cell 2.");
        builder.insertCell();
        builder.write("Row 2, Cell 3.");
        builder.endRow();
        builder.endTable();
        
        doc.save(getArtifactsDir() + "DocumentBuilder.CreateFormattedTable.docx");
      • getHeightRule/setHeightRule

        public int getHeightRule() / public void setHeightRule(int value)
        
        Gets or sets the rule for determining the height of the table row. The value of the property is HeightRule integer constant.

        Example:

        Shows how to format rows 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.");
        
        // Start a second row, and then configure its height. The builder will apply these settings to
        // its current row, as well as any new rows it creates afterwards.
        builder.endRow();
        
        RowFormat rowFormat = builder.getRowFormat();
        rowFormat.setHeight(100.0);
        rowFormat.setHeightRule(HeightRule.EXACTLY);
        
        builder.insertCell();
        builder.write("Row 2, cell 1.");
        builder.endTable();
        
        // The first row was unaffected by the padding reconfiguration and still holds the default values.
        Assert.assertEquals(0.0d, table.getRows().get(0).getRowFormat().getHeight());
        Assert.assertEquals(HeightRule.AUTO, table.getRows().get(0).getRowFormat().getHeightRule());
        
        Assert.assertEquals(100.0d, table.getRows().get(1).getRowFormat().getHeight());
        Assert.assertEquals(HeightRule.EXACTLY, table.getRows().get(1).getRowFormat().getHeightRule());
        
        doc.save(getArtifactsDir() + "DocumentBuilder.SetRowFormatting.docx");

        Example:

        Shows how to create a formatted table using DocumentBuilder.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        Table table = builder.startTable();
        builder.insertCell();
        table.setLeftIndent(20.0);
        
        // Set some formatting options for text and table appearance.
        builder.getRowFormat().setHeight(40.0);
        builder.getRowFormat().setHeightRule(HeightRule.AT_LEAST);
        builder.getCellFormat().getShading().setBackgroundPatternColor(new Color((198), (217), (241)));
        
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
        builder.getFont().setSize(16.0);
        builder.getFont().setName("Arial");
        builder.getFont().setBold(true);
        
        // Configuring the formatting options in a document builder will apply them
        // to the current cell/row its cursor is in,
        // as well as any new cells and rows created using that builder.
        builder.write("Header Row,\n Cell 1");
        builder.insertCell();
        builder.write("Header Row,\n Cell 2");
        builder.insertCell();
        builder.write("Header Row,\n Cell 3");
        builder.endRow();
        
        // Reconfigure the builder's formatting objects for new rows and cells that we are about to make.
        // The builder will not apply these to the first row already created so that it will stand out as a header row.
        builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);
        builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
        builder.getRowFormat().setHeight(30.0);
        builder.getRowFormat().setHeightRule(HeightRule.AUTO);
        builder.insertCell();
        builder.getFont().setSize(12.0);
        builder.getFont().setBold(false);
        
        builder.write("Row 1, Cell 1.");
        builder.insertCell();
        builder.write("Row 1, Cell 2.");
        builder.insertCell();
        builder.write("Row 1, Cell 3.");
        builder.endRow();
        builder.insertCell();
        builder.write("Row 2, Cell 1.");
        builder.insertCell();
        builder.write("Row 2, Cell 2.");
        builder.insertCell();
        builder.write("Row 2, Cell 3.");
        builder.endRow();
        builder.endTable();
        
        doc.save(getArtifactsDir() + "DocumentBuilder.CreateFormattedTable.docx");
    • Method Detail

      • clearFormatting

        public void clearFormatting()
                            throws java.lang.Exception
        Resets to default row formatting.

        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");