Generator > Programming > Tables

DynamicPDF™ Generator for Java offers a Table page element to help you format content within your PDF pages. The table gives you the ability to add text as well as any page elements to each cell. Table also gives you 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 next 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 you want the entire table to contain a certain format, then you could set all the formatting information on the table and every cell in every row would contain the appropriate formatting. If you want a row to contain some formatting different than the rest of the table then you can specify the text format on the row and every cell in that row will contain the particular formatting. Finally, if you want a cell to contain a different formatting than the rest of the row then you can specify the formatting on the individual cell. Note that the Column class contains no formatting information. Understanding this hierarchy will help you easily format any cell within the table as needed.

Simple Example

To create a table that will appear on the PDF you should first instantiate a Table class. You will need to specify the starting x and y coordinate to place the table on the page as well as a height and width. This height and width should be the amount of vertical on horizontal space you have for the table on a particular page. You do not need to try and guess the height and width of your final table here. Also, any formatting that you want to be the default for the entire table should be specified here.

The first thing you should do to the table is define the width of your columns for the table. Remember that once set, column widths are read-only and can not be changed.

Once the columns are defined, then you can populate the table with data. This is done first by adding a row to the table and then adding cells to that row. You can add fewer cells to the row than the existing number of columns in the table but you can never add more cells than 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.

As you are adding your rows to the table you can set a particular height for that row. This height will be the minimum height for the row. 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