DynamicPDF™ Generator for Java offers a Table page element to help format content within PDF pages. The table gives the ability to add text as well as any page elements to each cell. Table also gives the ability to control the flow of data between sections and pages. This topic explains how to use the table page element and provides examples on how to use the features listed above.
General Explanation
A table consists of one or more columns and one or more rows. Each row contains one or more cells. A cell can contain plain text or any page element (we will look at adding other page elements to the table in the Adding Page Elements to Cells topic). Formatting of the text (background color, font, font size, text color and alignment) in each cell is handled in a hierarchical manner. Specifically, the formatting specified within cell takes precedence over the formatting specified within a row which in turn takes precedence over formatting specified within a table. For example, if the entire table contains a certain format, then all the formatting information can be set on the table and every cell in every row will contain the appropriate formatting. If a row contains some formatting different than the rest of the table then the text format can be set on the row and every cell in that row will contain the particular formatting. Finally, if a cell contains a different formatting than the rest of the row then the formatting can be specified on the individual cell. Understanding this hierarchy will help to easily format any cell within the table as needed. Note that the Column class contains no formatting information.
Simple Example
To create a table that will appear on the PDF, first instantiate a Table class. Always specify the Height and Width as well as the X and Y coordinates of the starting point (top left corner) where the table will be placed on the page. This height and width should be the amount of vertical or horizontal space needed for the table on a particular page. Do not try and guess the height and width of the final table here. Also, any formatting that has to be the default for the entire table should be specified here.
First, define the width of the columns for the table and remember, once column width is set, it is read-only and can not be changed.
Once the columns are defined, the table can be populated with data. This is done firstly, by adding a row to the table and then adding cells to that row. It is possible to add fewer cells to the row than the existing number of columns in the table, but it is not possible to add more cells than the number of columns. Cells can also span multiple columns. The key here is that the sum of all the column spans for all the cells in a row must be less than or equal to the total number of columns in that table.
Add rows to the table and a particular height for the row can also be set. This height will be the minimum height and if needed, the height will automatically expand to fit the largest element (either text or any page element that implements IArea) in that row. Take a look at the third row in the example below.
[Java]
Table table = new Table(0, 0, 600, 600);
// Add columns to the table
table.getColumns().add(150);
table.getColumns().add(90);
table.getColumns().add(90);
table.getColumns().add(90);
// Add rows to the table and add cells to the rows
Row row1 = table.getRows().add(40, Font.getHelveticaBold(), 16, Color.getBlack(),
Color.getGray());
row1.setAlign(TextAlign.CENTER);
row1.setVAlign(VAlign.CENTER);
row1.getCellList().add("Header 1");
row1.getCellList().add("Header 2");
row1.getCellList().add("Header 3");
row1.getCellList().add("Header 4");
Row row2 = table.getRows().add(30);
Cell cell1 = row2.getCellList().add("Rowheader 1", Font.getHelveticaBold(), 16,
Color.getBlack(), Color.getGray(), 1);
cell1.setAlign(TextAlign.CENTER);
cell1.setVAlign(VAlign.CENTER);
row2.getCellList().add("Item 1");
row2.getCellList().add("Item 2");
row2.getCellList().add("Item 3");
Row row3 = table.getRows().add(30);
Cell cell2 = row3.getCellList().add("Rowheader 2", Font.getHelveticaBold(), 16,
Color.getBlack(), Color.getGray(), 1);
cell2.setAlign(TextAlign.CENTER);
cell2.setVAlign(VAlign.CENTER);
row3.getCellList().add("Item 4");
row3.getCellList().add("Item 5, this item is much longer than the rest so that " +
"you can see that each row will automatically expand to fit to the " +
"height of the largest element in that row.");
row3.getCellList().add("Item 6");
Row row4 = table.getRows().add(30);
Cell cell3 = row4.getCellList().add("Rowheader 3", Font.getHelveticaBold(), 16,
Color.getBlack(), Color.getGray(), 1);
cell3.setAlign(TextAlign.CENTER);
cell3.setVAlign(VAlign.CENTER);
row4.getCellList().add("Item 7");
row4.getCellList().add("Item 8");
row4.getCellList().add("Item 9");
// Add the table to the page
page.getElements().add(table);
In This Section
- Adding Page Elements to Cells
- Explains how to add page elements to a tables cell.
- Table Continuation
- Explains how span a table across multiple pages or columns.
| See Also |
com.cete.dynamicpdf.pageelements.Table | Programming With Generator for Java

