Table Continuation
When dynamically adding data to a table, the generated content can be controlled by controlling where a table stops and starts on any particular page and the placement of any overflow rows or overflow columns.
GetOverflowRows
The Table2 class has three GetOverflowRows methods. The Table2 class also has the HasOverflowRows method, which determines if a table has overflow rows.
The Table2 class also has the following relevant property that allows repeating column headers when rows span multiple pages.
Property | Description |
---|---|
RepeatColumnHeaderCount | Gets or sets the number of initial rows in the table that should also be drawn as the first rows on all subsequent row overflow tables. This is set to 0 by default. |
Example
The following example illustrates dynamically adding rows to a table where the number of rows is greater than the size of a page. It also adds a header row that is repeated for each overflow table.
// Create a PDF Document
Document document = new Document();
Table2 table = new Table2(0, 0, 200, 700);
// Add columns to the table
table.Columns.Add(100);
table.Columns.Add(100);
// create a header row for overflow
Row2 rowH = table.Rows.Add(20);
rowH.Cells.Add("Row Header");
rowH.Cells.Add("Item Header");
table.RepeatColumnHeaderCount = 1;
// This loop populates the table
for (int i = 1; i <= 400; i++)
{
Row2 row = table.Rows.Add(20);
row.Cells.Add("Row #" + i);
row.Cells.Add("Item");
}
// This loop outputs the table to as many pages as is required
do
{
Page page = new Page();
document.Pages.Add(page);
page.Elements.Add(table);
table = table.GetOverflowRows();
} while (table != null);
// Save the PDF
document.Draw(outputPath);
// Create a PDF Document
Document document = new Document();
Table2 table = new Table2(0, 0, 200, 700);
// Add columns to the table
table.Columns.Add(100);
table.Columns.Add(100);
// create a header row for overflow
Row2 rowH = table.Rows.Add(20);
rowH.Cells.Add("Row Header");
rowH.Cells.Add("Item Header");
table.RepeatColumnHeaderCount = 1;
// This loop populates the table
for (int i = 1; i <= 400; i++)
{
Row2 row = table.Rows.Add(20);
row.Cells.Add("Row #" + i);
row.Cells.Add("Item");
}
// This loop outputs the table to as many pages as is required
do
{
Page page = new Page();
document.Pages.Add(page);
page.Elements.Add(table);
table = table.GetOverflowRows();
} while (table != null);
// Save the PDF
document.Draw(outputPath);
Figure 1. A table that continues past multiple pages.
GetOverflowColumns
A table allows its columns to flow through pages without knowing the number of columns. The Table2 class has three GetOverflowColumns methods.
The Table2 class also has the HasOverflowColumns method that determines if a table has any overflow columns.
The Table2 class also has the following relevant property that allows repeating row headers when columns span multiple pages.
Property | Description |
---|---|
RepeatRowHeaderCount | Gets or sets the number of columns that will be repeated as the row header. |
Example
The following example illustrates dynamically adding columns to a table where the number of columns exceeds a page's width. It also adds header columns that are repeated for each overflow table.
// Create a PDF Document
Document document = new Document();
Table2 table = new Table2(0, 0, 500, 50);
// create the headers for the two rows
table.Columns.Add(150);
table.Columns.Add(150);
Row2 row1 = table.Rows.Add(20);
Row2 row2 = table.Rows.Add(20);
row1.Cells.Add("Column Header");
row2.Cells.Add("Row Header");
table.RepeatRowHeaderCount = 2;
// create columns
int cols = 50;
for (int i = 0; i <= cols; i++)
{
table.Columns.Add(100);
for (int j = 0; j <= cols; j++)
{
row1.Cells.Add("Column #" + j);
row2.Cells.Add("Item");
}
}
// This loop outputs the table to as many pages as is required
do
{
Page page = new Page();
document.Pages.Add(page);
page.Elements.Add(table);
table = table.GetOverflowColumns();
} while (table != null);
// Save the PDF
document.Draw(outputPath);
// Create a PDF Document
Document document = new Document();
Table2 table = new Table2(0, 0, 500, 50);
// create the headers for the two rows
table.Columns.Add(150);
table.Columns.Add(150);
Row2 row1 = table.Rows.Add(20);
Row2 row2 = table.Rows.Add(20);
row1.Cells.Add("Column Header");
row2.Cells.Add("Row Header");
table.RepeatRowHeaderCount = 2;
// create columns
int cols = 50;
for (int i = 0; i <= cols; i++)
{
table.Columns.Add(100);
for (int j = 0; j <= cols; j++)
{
row1.Cells.Add("Column #" + j);
row2.Cells.Add("Item");
}
}
// This loop outputs the table to as many pages as is required
do
{
Page page = new Page();
document.Pages.Add(page);
page.Elements.Add(table);
table = table.GetOverflowColumns();
} while (table != null);
// Save the PDF
document.Draw(outputPath);
Figure 2. A table who's columns continue past multiple pages.