Tables are very powerful because they give you the ability to control how the data within that table flows from page to page. You 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.
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 is in your 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 the table is filled in with all the data, it is this next do while loop that takes care of allowing the rows of data to flow from page to page in the document. You will 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 to understanding the table is knowing that regardless of how many rows you add 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 value 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.
[Java]
Table2 table = new Table2(0, 0, 200, 700);
table.getColumns().add(100);
table.getColumns().add(100);
// This loop populates the table
for (int i = 1; i <= 400; i++) {
Row2 row = table.getRows().add(20);
row.getCells().add("Row #" + i);
row.getCells().add("Item");
}
// This loop outputs the table to as many pages as is required
do {
Page page = new Page();
document.getPages().add(page);
page.getElements().add(table);
table = table.getOverflowRows();
} while (table != null);
For this example, just as you would for any other, you 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 you can go ahead and fill in the rows. For simplicity we only created two rows but your 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. You will 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.
[Java]
Table2 table = new Table2(0, 0, 500, 50);
// Create some number of columns
int cols = 50;
for (int i = 0; i <= cols; i++)
table.getColumns().add(100);
// Create your rows to fill in
Row2 row1 = table.getRows().add(20);
Row2 row2 = table.getRows().add(20);
for (int j = 0; j <= cols; j++) {
row1.getCells().add("Column #" + j);
row2.getCells().add("Item");
}
// This loop outputs the table to as many pages as is required
do {
Page page = new Page();
document.getPages().add(page);
page.getElements().add(table);
table = table.getOverflowColumns();
} while (table != null);
Combining the information presented in the above two examples, you 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.