java.lang.Objectcom.aspose.words.RowFormat
public class RowFormat
Example: Example: Example:
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);
Document doc = new Document();
// We start by creating the table object. Note how we must pass the document object
// to the constructor of each node. This is because every node we create must belong
// to some document
Table table = new Table(doc);
// Add the table to the document
doc.getFirstSection().getBody().appendChild(table);
// Here we could call EnsureMinimum to create the rows and cells for us. This method is used
// to ensure that the specified node is valid, in this case a valid table should have at least one
// row and one cell, therefore this method creates them for us
// Instead we will handle creating the row and table ourselves. This would be the best way to do this
// if we were creating a table inside an algorithm for example
Row row = new Row(doc);
row.getRowFormat().setAllowBreakAcrossPages(true);
table.appendChild(row);
// We can now apply any auto fit settings
table.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS);
// Create a cell and add it to the row
Cell cell = new Cell(doc);
cell.getCellFormat().getShading().setBackgroundPatternColor(Color.BLUE);
cell.getCellFormat().setWidth(80);
// Add a paragraph to the cell as well as a new run with some text
cell.appendChild(new Paragraph(doc));
cell.getFirstParagraph().appendChild(new Run(doc, "Row 1, Cell 1 Text"));
// Add the cell to the row
row.appendChild(cell);
// We would then repeat the process for the other cells and rows in the table
// We can also speed things up by cloning existing cells and rows
row.appendChild(cell.deepClone(false));
row.getLastCell().appendChild(new Paragraph(doc));
row.getLastCell().getFirstParagraph().appendChild(new Run(doc, "Row 1, Cell 2 Text"));
// Remove spacing between cells
table.setAllowCellSpacing(false);
doc.save(getArtifactsDir() + "Table.InsertTableUsingNodes.doc");
DocumentBuilder builder = new DocumentBuilder();
// Start building a table
builder.startTable();
// Set the appropriate paragraph, cell, and row formatting. The formatting properties are preserved
// until they are explicitly modified so there's no need to set them for each row or cell
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.getCellFormat().clearFormatting();
builder.getCellFormat().setWidth(150.0);
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
builder.getCellFormat().getShading().setBackgroundPatternColor(new Color(173, 255, 47)); //"green-yellow"
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(new Color(255, 165, 0)); // "orange"
builder.insertCell();
builder.write("Row 1, Col 1");
builder.insertCell();
builder.write("Row 1, Col 2");
builder.endRow();
// Remove the shading (clear background)
builder.getCellFormat().getShading().clearFormatting();
builder.insertCell();
builder.write("Row 2, Col 1");
builder.insertCell();
builder.write("Row 2, Col 2");
builder.endRow();
builder.insertCell();
// Make the row height bigger so that a vertically oriented text could fit into cells
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();
builder.getDocument().save(getArtifactsDir() + "DocumentBuilder.InsertTable.docx");
Property Getters/Setters Summary | ||
---|---|---|
boolean | getAllowBreakAcrossPages() | |
void | setAllowBreakAcrossPages(boolean value) | |
True if the text in a table row is allowed to split across a page break. | ||
BorderCollection | getBorders() | |
Gets the collection of default cell borders for the row. | ||
boolean | getHeadingFormat() | |
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. | ||
double | getHeight() | |
void | setHeight(double value) | |
Gets or sets the height of the table row in points. | ||
int | getHeightRule() | |
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. |
Method Summary | ||
---|---|---|
void | clearFormatting() | |
Resets to default row formatting. |
Property Getters/Setters Detail |
---|
getAllowBreakAcrossPages/setAllowBreakAcrossPages | |
public boolean getAllowBreakAcrossPages() / public void setAllowBreakAcrossPages(boolean value) |
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() |
Example:
Shows how to build a nice bordered table.DocumentBuilder builder = new DocumentBuilder(); // Start building a table builder.startTable(); // Set the appropriate paragraph, cell, and row formatting. The formatting properties are preserved // until they are explicitly modified so there's no need to set them for each row or cell builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); builder.getCellFormat().clearFormatting(); builder.getCellFormat().setWidth(150.0); builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER); builder.getCellFormat().getShading().setBackgroundPatternColor(new Color(173, 255, 47)); //"green-yellow" 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(new Color(255, 165, 0)); // "orange" builder.insertCell(); builder.write("Row 1, Col 1"); builder.insertCell(); builder.write("Row 1, Col 2"); builder.endRow(); // Remove the shading (clear background) builder.getCellFormat().getShading().clearFormatting(); builder.insertCell(); builder.write("Row 2, Col 1"); builder.insertCell(); builder.write("Row 2, Col 2"); builder.endRow(); builder.insertCell(); // Make the row height bigger so that a vertically oriented text could fit into cells 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(); builder.getDocument().save(getArtifactsDir() + "DocumentBuilder.InsertTable.docx");
getHeadingFormat/setHeadingFormat | |
public boolean getHeadingFormat() / public void setHeadingFormat(boolean value) |
Example:
Shows how to build a table which include heading rows that repeat on subsequent pages.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.startTable(); builder.getRowFormat().setHeadingFormat(true); builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); builder.getCellFormat().setWidth(100.0); builder.insertCell(); builder.writeln("Heading row 1"); builder.endRow(); builder.insertCell(); builder.writeln("Heading row 2"); builder.endRow(); builder.getCellFormat().setWidth(50.0); builder.getParagraphFormat().clearFormatting(); // Insert some content so the table is long enough to continue onto the next page for (int i = 0; i < 50; i++) { builder.insertCell(); builder.getRowFormat().setHeadingFormat(false); builder.write("Column 1 Text"); builder.insertCell(); builder.write("Column 2 Text"); builder.endRow(); } doc.save(getArtifactsDir() + "DocumentBuilder.InsertTableSetHeadingRow.doc");
getHeight/setHeight | |
public double getHeight() / public void setHeight(double value) |
Example:
Shows how to create a table that contains a single cell and apply row formatting.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.startTable(); builder.insertCell(); // Set the row formatting RowFormat rowFormat = builder.getRowFormat(); rowFormat.setHeight(100); rowFormat.setHeightRule(HeightRule.EXACTLY); // These formatting properties are set on the table and are applied to all rows in the table table.setLeftPadding(30); table.setRightPadding(30); table.setTopPadding(30); table.setBottomPadding(30); builder.writeln("Contents of formatted row."); builder.endRow(); builder.endTable();
Example:
Shows how to create a formatted table using DocumentBuilder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.startTable(); // Make the header row builder.insertCell(); // Set the left indent for the table. Table wide formatting must be applied after // at least one row is present in the table table.setLeftIndent(20.0); // Set height and define the height rule for the header row builder.getRowFormat().setHeight(40.0); builder.getRowFormat().setHeightRule(HeightRule.AT_LEAST); // Some special features for the header row builder.getCellFormat().getShading().setBackgroundPatternColor(new Color(198, 217, 241)); builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); builder.getFont().setSize(16); builder.getFont().setName("Arial"); builder.getFont().setBold(true); builder.getCellFormat().setWidth(100.0); builder.write("Header Row,\n Cell 1"); // We don't need to specify the width of this cell because it's inherited from the previous cell builder.insertCell(); builder.write("Header Row,\n Cell 2"); builder.insertCell(); builder.getCellFormat().setWidth(200.0); builder.write("Header Row,\n Cell 3"); builder.endRow(); // Set features for the other rows and cells builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE); builder.getCellFormat().setWidth(100.0); builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER); // Reset height and define a different height rule for table body builder.getRowFormat().setHeight(30.0); builder.getRowFormat().setHeightRule(HeightRule.AUTO); builder.insertCell(); // Reset font formatting builder.getFont().setSize(12); builder.getFont().setBold(false); // Build the other cells builder.write("Row 1, Cell 1 Content"); builder.insertCell(); builder.write("Row 1, Cell 2 Content"); builder.insertCell(); builder.getCellFormat().setWidth(200.0); builder.write("Row 1, Cell 3 Content"); builder.endRow(); builder.insertCell(); builder.getCellFormat().setWidth(100.0); builder.write("Row 2, Cell 1 Content"); builder.insertCell(); builder.write("Row 2, Cell 2 Content"); builder.insertCell(); builder.getCellFormat().setWidth(200.0); builder.write("Row 2, Cell 3 Content."); builder.endRow(); builder.endTable(); doc.save(getArtifactsDir() + "DocumentBuilder.CreateFormattedTable.doc");
getHeightRule/setHeightRule | |
public int getHeightRule() / public void setHeightRule(int value) |
Example:
Shows how to create a table that contains a single cell and apply row formatting.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.startTable(); builder.insertCell(); // Set the row formatting RowFormat rowFormat = builder.getRowFormat(); rowFormat.setHeight(100); rowFormat.setHeightRule(HeightRule.EXACTLY); // These formatting properties are set on the table and are applied to all rows in the table table.setLeftPadding(30); table.setRightPadding(30); table.setTopPadding(30); table.setBottomPadding(30); builder.writeln("Contents of formatted row."); builder.endRow(); builder.endTable();
Example:
Shows how to create a formatted table using DocumentBuilder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.startTable(); // Make the header row builder.insertCell(); // Set the left indent for the table. Table wide formatting must be applied after // at least one row is present in the table table.setLeftIndent(20.0); // Set height and define the height rule for the header row builder.getRowFormat().setHeight(40.0); builder.getRowFormat().setHeightRule(HeightRule.AT_LEAST); // Some special features for the header row builder.getCellFormat().getShading().setBackgroundPatternColor(new Color(198, 217, 241)); builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); builder.getFont().setSize(16); builder.getFont().setName("Arial"); builder.getFont().setBold(true); builder.getCellFormat().setWidth(100.0); builder.write("Header Row,\n Cell 1"); // We don't need to specify the width of this cell because it's inherited from the previous cell builder.insertCell(); builder.write("Header Row,\n Cell 2"); builder.insertCell(); builder.getCellFormat().setWidth(200.0); builder.write("Header Row,\n Cell 3"); builder.endRow(); // Set features for the other rows and cells builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE); builder.getCellFormat().setWidth(100.0); builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER); // Reset height and define a different height rule for table body builder.getRowFormat().setHeight(30.0); builder.getRowFormat().setHeightRule(HeightRule.AUTO); builder.insertCell(); // Reset font formatting builder.getFont().setSize(12); builder.getFont().setBold(false); // Build the other cells builder.write("Row 1, Cell 1 Content"); builder.insertCell(); builder.write("Row 1, Cell 2 Content"); builder.insertCell(); builder.getCellFormat().setWidth(200.0); builder.write("Row 1, Cell 3 Content"); builder.endRow(); builder.insertCell(); builder.getCellFormat().setWidth(100.0); builder.write("Row 2, Cell 1 Content"); builder.insertCell(); builder.write("Row 2, Cell 2 Content"); builder.insertCell(); builder.getCellFormat().setWidth(200.0); builder.write("Row 2, Cell 3 Content."); builder.endRow(); builder.endTable(); doc.save(getArtifactsDir() + "DocumentBuilder.CreateFormattedTable.doc");
Method Detail |
---|
clearFormatting | |
public void clearFormatting() throws java.lang.Exception |
Example:
Shows how to build a nice bordered table.DocumentBuilder builder = new DocumentBuilder(); // Start building a table builder.startTable(); // Set the appropriate paragraph, cell, and row formatting. The formatting properties are preserved // until they are explicitly modified so there's no need to set them for each row or cell builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); builder.getCellFormat().clearFormatting(); builder.getCellFormat().setWidth(150.0); builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER); builder.getCellFormat().getShading().setBackgroundPatternColor(new Color(173, 255, 47)); //"green-yellow" 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(new Color(255, 165, 0)); // "orange" builder.insertCell(); builder.write("Row 1, Col 1"); builder.insertCell(); builder.write("Row 1, Col 2"); builder.endRow(); // Remove the shading (clear background) builder.getCellFormat().getShading().clearFormatting(); builder.insertCell(); builder.write("Row 2, Col 1"); builder.insertCell(); builder.write("Row 2, Col 2"); builder.endRow(); builder.insertCell(); // Make the row height bigger so that a vertically oriented text could fit into cells 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(); builder.getDocument().save(getArtifactsDir() + "DocumentBuilder.InsertTable.docx");