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.

public Table2 GetOverflowRows()
public Table2 GetOverflowRows(float x, float y)
public Table2 GetOverflowRows(float x, float y, float width, float height)

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(pdfFilePath);        
' Create a PDF Document 
Dim MyDocument As Document = New Document

Dim MyTable As Table2 = New Table2(0, 0, 200, 700)

' Add columns to the table
MyTable.Columns.Add(100)
MyTable.Columns.Add(100)

Dim rowH As Row2 = MyTable.Rows.Add(20)
rowH.Cells.Add("Row Header");
rowH.Cells.Add("Item Header");

MyTable.RepeatColumnHeaderCount = 1;

' This loop populates the table
Dim I As Integer
For I = 1 To 400
    Dim MyRow As Row2 = MyTable.Rows.Add(20)
    MyRow.Cells.Add("Row #" & I)
    MyRow.Cells.Add("Item")
Next

' This loop outputs the table to as many pages as is required
Do
    Dim MyPage As Page = New Page
    MyDocument.Pages.Add(MyPage)
    MyPage.Elements.Add(MyTable)
    MyTable = MyTable.GetOverflowRows()
Loop While Not (MyTable Is Nothing)

' Save the PDF 
MyDocument.Draw(pdfFilePath)      

Refer to the Table2.GetOverflowRows API documentation for an example.

GetOverflowColumns

A table allows its columns to flow through pages without knowing the number of columns. The Table2 class has three GetOverflowColumns methods.

public Table2 GetOverflowColumns()
public Table2 GetOverflowGetOverflowColumns(float x, float y)
public Table2 GetOverflowColumns(float x, float y, float width, float height)

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(pdfFilePath);         
' Create a PDF Document 
Dim MyDocument As Document = New Document

Dim MyTable As Table2 = New Table2(0, 0, 500, 50)
' Create some number of columns
Dim Cols As Integer = 50
Dim Int As Integer

// create the headers for the two rows
MyTable.Columns.Add(150);
MyTable.Columns.Add(150);
Dim MyRow1 As Row2 = MyTable.Rows.Add(20)
Dim MyRow2 As Row2 = MyTable.Rows.Add(20)
MyRow1.Cells.Add("Column Header");
MyRow2.Cells.Add("Row Header");
table.RepeatRowHeaderCount = 2;

For Int = 1 To Cols
    MyTable.Columns.Add(100)
Next
' Create rows to fill in

Dim J As Integer

For J = 1 To Cols
    MyRow1.Cells.Add("Column #" & J)
    MyRow2.Cells.Add("Item")
Next

' This loop outputs the table to as many pages as is required
Do
Dim MyPage As Page = New Page
    MyDocument.Pages.Add(MyPage)
    MyPage.Elements.Add(MyTable)
    MyTable = MyTable.GetOverflowColumns()
Loop While Not (MyTable Is Nothing)

' Save the PDF 
MyDocument.Draw(pdfFilePath)      

Refer to the GetOverflowColumns API documentation for an example.

In this topic