The basic algorithm to create a table using DocumentBuilder is simple:
- Start the table using StartTable.
- Insert a cell using InsertCell. This automatically starts a new row. If needed, use the CellFormat property to specify cell formatting.
- Insert cell contents using the DocumentBuilder methods.
- Repeat steps 2 and 3 until the row is complete.
- Call EndRow to end the current row. If needed, use RowFormat property to specify row formatting.
- Repeat steps 2 - 5 until the table is complete.
- Call EndTable to finish the table building.
The appropriate DocumentBuilder table creation methods are described below.
Starting a Table
Calling StartTable is the first step in building a table. It can be also called inside a cell, in which case it starts a nested table. The next method to call is InsertCell.
Inserting a Cell
After you call InsertCell, a new cell is created and any content you add using other methods of the DocumentBuilder class will be added to the current cell. To start a new cell in the same row, call InsertCell again.
Use the CellFormat property to specify cell formatting. It returns a CellFormat object that represents all formatting for a table cell.
Ending a Row
Call EndRow to finish the current row. If you call InsertCell immediately after that, then the table continues on a new row.
Use the RowFormat property to specify row formatting. It returns a RowFormat that represents all formatting for a table row.
Ending a Table
Call EndTable to finish the current table. This method should be called only once after EndRow was called. When called, EndTable moves the cursor out of the current cell to a position just after the table.
The following example demonstrates how to build a formatted table that contains 2 rows and 2 columns.
Example DocumentBuilderBuildTable
Shows how to build a formatted table that contains 2 rows and 2 columns.
[C#]
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.StartTable();
// Insert a cell
builder.InsertCell();
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.Writeln("This is row 1 cell 1");
// Insert a cell
builder.InsertCell();
builder.Writeln("This is row 1 cell 2");
builder.EndRow();
// Apply new row formatting
builder.RowFormat.Height = 100;
builder.RowFormat.HeightRule = HeightRule.Exactly;
// Insert a cell
builder.InsertCell();
builder.CellFormat.Orientation = TextOrientation.Upward;
builder.Writeln("This is row 2 cell 1");
// Insert a cell
builder.InsertCell();
builder.CellFormat.Orientation = TextOrientation.Downward;
builder.Writeln("This is row 2 cell 2");
builder.EndRow();
builder.EndTable();
[Visual Basic]
Dim doc As Document = New Document()
Dim builder As DocumentBuilder = New DocumentBuilder(doc)
builder.StartTable()
' Insert a cell
builder.InsertCell()
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center
builder.Writeln("This is row 1 cell 1")
' Insert a cell
builder.InsertCell()
builder.Writeln("This is row 1 cell 2")
builder.EndRow()
' Apply new row formatting
builder.RowFormat.Height = 100
builder.RowFormat.HeightRule = HeightRule.Exactly
' Insert a cell
builder.InsertCell()
builder.CellFormat.Orientation = TextOrientation.Upward
builder.Writeln("This is row 2 cell 1")
' Insert a cell
builder.InsertCell()
builder.CellFormat.Orientation = TextOrientation.Downward
builder.Writeln("This is row 2 cell 2")
builder.EndRow()
builder.EndTable()
[Java]
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startTable();
// Insert a cell
builder.insertCell();
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
builder.writeln("This is row 1 cell 1");
// Insert a cell
builder.insertCell();
builder.writeln("This is row 1 cell 2");
builder.endRow();
// Apply new row formatting
builder.getRowFormat().setHeight(100);
builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);
// Insert a cell
builder.insertCell();
builder.getCellFormat().setOrientation(TextOrientation.UPWARD);
builder.writeln("This is row 2 cell 1");
// Insert a cell
builder.insertCell();
builder.getCellFormat().setOrientation(TextOrientation.DOWNWARD);
builder.writeln("This is row 2 cell 2");
builder.endRow();
builder.endTable();