Table Continuation

Tables are powerful because they allow controling how the data within that table flows from page to page. One can control where a table stops and starts on any particular page as well as control the placement of any overflow rows or overflow columns a table may have. And all this control is given without ever needing to know the amount of data that the table will contain.

Overflowing Rows

The next example demonstrates how easy it is for rows of data to flow from page to page without ever needing to know exactly how much total data will be in the table. For this example, just like any other, we instantiate a table object and define the columns for that table. Next, the for loop will populate the table. We have picked an arbitrary number of rows, in this case 400, to fill the table with but this example will work for any number larger or smaller.

Once a table is filled in with all the data, it is the do-while loop that takes care of allowing the rows of data to flow from page to page in the document. First time through the loop a page is created and added to the document, and then the table is added to the page. One key to understanding the table is knowing that regardless of how many rows are added to the table (in this case 400 rows) only the number of rows that will fit in the defined height (in this case 700 points) of the table are going to appear on that page.

The very last line of the do-while loop will now set the table variable equal to the remaining rows of the table that did not fit on that first page. This is done using the table's GetOverflowRows method. If all the rows happen to fit on that first page then the GetOverflowRows method will return a null and will then break out of the loop when the while condition is checked. Otherwise the loop will continue to create a new Page, add the page to the document, add the table to the page and then set the table equal to the remaining rows until all the rows are displayed.

// 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);

// 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)

' 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)      

Overflowing Columns

In the above example we saw how the rows of a table can flow from page to page as needed to fit the entire table. Very similarly, a table will also allow its columns to flow through pages without ever needing to know the exact number of Columns. This next example demonstrates exactly that.

For this example, create a table and then define the columns for that table. Since we are demonstrating that the number of Columns does not need to be known ahead of time we placed the line for adding the columns in a for loop. We set the total number of columns to 50 but this example will work for any value larger or smaller.

Once all the columns are defined, go ahead and fill in the rows. For simplicity we only created two rows but the table could contain any number of rows as well.

Once the table is filled in with all the data, it is this next do while loop that takes care of allowing the columns of data to flow from page to page. Notice that the first time through the loop, a page is created and added to the document, and then the table is added to the page. One key concept to understand here is that the Table class, regardless of how much content is in the table, displays only the number of columns that will fit within the particular width defined in the constructor (500 points).

The very last line of the do while loop will now set the table variable equal to the remaining columns of the table that did not fit on the page. This is done by calling the table's GetOverflowColumns method. If all the columns happen to fit on that first page then the GetOverflowColumns method will return a null and will then break out of the loop. Otherwise the loop will continue to create a new Page, add the page to the document, add the table to the page and then set the table equal to the remaining columns until all the columns are displayed.

// Create a PDF Document 
Document document = new Document();

Table2 table = new Table2(0, 0, 500, 50);

// Create some number of columns
int cols = 50;
for (int i = 0; i <= cols; i++)
    table.Columns.Add(100);
// Create rows to fill in
Row2 row1 = table.Rows.Add(20);
Row2 row2 = table.Rows.Add(20);

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
For Int = 1 To Cols
    MyTable.Columns.Add(100)
Next
' Create rows to fill in
Dim MyRow1 As Row2 = MyTable.Rows.Add(20)
Dim MyRow2 As Row2 = MyTable.Rows.Add(20)
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)      

Overflowing Columns and Rows

Combining the information presented in the above two examples one can have complete control over the overflow rows of a table as well as the overflow columns. The GetOverFlowRows method and the GetOverflowColumns method can be used in nested loops to handle the overflow of even the largest table.

In this topic