Tables
A Table is a grid of cells built row by row. Tables break across pages automatically, and because a TableCell is a full content container, cells can hold anything a document can hold.
Basic table
Create the table with AddTable, add rows with AddTableRow, and add cells with AddTableCell. SetDefaultColumnWidth and SetDefaultRowHeight provide the grid defaults.
Borders are set at two levels. Table.SetBorder draws only the outer frame around the whole table. It doesn't draw the lines between the cells. To get a full grid, call TableCell.SetBorder on each cell as well. Both calls take a thickness in points plus an RGB color, and apply it to all four edges.
Table table = doc.AddTable();
table.SetDefaultColumnWidth(90);
table.SetDefaultRowHeight(18);
table.SetBorder(0.75, 120, 120, 120);
for (int r = 0; r < 3; ++r)
{
TableRow row = table.AddTableRow();
for (int c = 0; c < 3; ++c)
{
TableCell cell = row.AddTableCell();
cell.SetBorder(0.75, 120, 120, 120); // draws the internal grid lines
cell.AddParagraph("R" + r + " C" + c);
}
}Table table = doc.AddTable();
table.SetDefaultColumnWidth(90);
table.SetDefaultRowHeight(18);
table.SetBorder(0.75, 120, 120, 120);
for (int r = 0; r < 3; ++r)
{
TableRow row = table.AddTableRow();
for (int c = 0; c < 3; ++c)
{
TableCell cell = row.AddTableCell();
cell.SetBorder(0.75, 120, 120, 120); // draws the internal grid lines
cell.AddParagraph("R" + IntToString(r) + " C" + IntToString(c));
}
}
The number of columns is determined by the number of cells you add to each row; rows can differ.
Style cells
Cells carry their own background color, border, alignment, and size. A typical pattern is a colored header row plus alternating row shading.

Horizontal placement of text has two levels. Paragraph justification places the text within the cell's content area, while the cell's horizontal alignment positions the content block within the cell. A small start indent keeps text off the cell border.
Alignment values
Vertical cell alignment
e_alignment_top, e_alignment_center, e_alignment_bottom
Horizontal cell alignment
e_alignment_left, e_alignment_middle, e_alignment_right
Merge cells
MergeCellsRight absorbs the next cells in the row, and MergeCellsDown absorbs the next cells in the column. Cells are addressed with GetTableCell, which takes the column first and then the row.

Merging is how invoice-style totals blocks are built: add a full-width row, then merge away the leading empty cells so the label sits next to the amount. After merging, a row reports fewer columns.
Nest content in cells
TableCell is a content container, so paragraphs, lists, and tables are all available inside a cell.

Sizing values
Table
SetDefaultColumnWidth, SetDefaultRowHeight
Row
SetRowHeight
Cell
SetWidth, SetHeight
More specific settings win: a cell width overrides the table default, and a row height overrides the table default. Row height behaves as a minimum. A row grows when its content needs more space.
Border values
Table
SetBorder
The outer frame of the table only
Cell
SetBorder
All four edges of that one cell
Because the table-level border covers only the outside, the internal grid lines come from the cells. Applying a border to every cell also draws the outer edges, so a cell-level border alone is enough for a uniform grid. Adjacent cell borders share a line, so neighboring cells with different border colors give the shared edge to whichever cell is drawn last.
Further Reading
Paragraphs and text for tab stops, a lighter alternative for simple column layouts.
Traversing the content tree for restyling table cells after they have been generated.
Last updated
Was this helpful?