The main Tables topic demonstrates how to create a simple table using strings of text for each cell. The Table2 object also allows adding any of our page elements (including another Table2) into a cell in a table.
In the example below, it is shown how it is possible to add any of our page elements to the table. In this example, instantiate a table object, define the columns and then start adding the rows and cells. The first two rows in the example below are used for headings. The next two rows of the table add page elements into a cell. Row 3 adds an FormattedTextArea page element that implements IArea, while Row 4 adds two Line page elements that do not implement IArea. Let's take a look at the differences.
[Java]
// Create a PDF Document
Document document = new Document();
// Create a Page and add it to the document
Page page = new Page();
document.getPages().add(page);
Table2 table = new Table2(0, 0, 400, 400);
table.getCellDefault().getBorder().setColor(RgbColor.getBlue());
table.getCellDefault().getBorder().setLineStyle(LineStyle.getSolid());
table.getCellDefault().getPadding().setValue(3.0f);
// Add columns to the table
table.getColumns().add(90);
table.getColumns().add(150);
table.getColumns().add(110);
// The first row is used as a table heading
Row2 row1 = table.getRows().add(20, Font.getHelveticaBold(), 14, RgbColor.getBlack(),RgbColor.getGray());
row1.getCellDefault().setAlign(TextAlign.CENTER);
row1.getCellDefault().setVAlign(VAlign.CENTER);
row1.getCells().add("Page Element", 3);
// The second row is the column headings
Row2 row2 = table.getRows().add(Font.getHelveticaBoldOblique(), 12);
row2.getCellDefault().setAlign( TextAlign.CENTER);
row2.getCells().add("Name");
row2.getCells().add("Element");
row2.getCells().add("Implements IArea");
// Add IArea page elements directly to the cell
Row2 row3 = table.getRows().add(30);
row3.getCells().add("FormattedTextArea");
row3.getCellDefault().getBorder().setColor(RgbColor.getGreen());
String formattedText = "<font face='Times'>font face, </font><font color='FF0000'>color, </font><b>bold.</b>";
row3.getCells().add(new FormattedTextArea(formattedText, 0, 0, 140, 50,FontFamily.getHelvetica(), 12, false));
Cell2 cell1 = row3.getCells().add("YES");
cell1.setAlign(TextAlign.CENTER);
cell1.setVAlign(VAlign.CENTER);
// Add page elemnts (Lines) to a cell using the AreaGroup
Row2 row4 = table.getRows().add(30);
row4.getCells().add("Line");
AreaGroup lineGroup = new AreaGroup(150, 150);
lineGroup.add(new Line(25, 25, 125, 125, 5));
lineGroup.add(new Line(25, 125, 125, 25, 5));
row4.getCells().add(lineGroup);
Cell2 cell2 = row4.getCells().add("NO");
cell2.setAlign(TextAlign.CENTER);
cell2.setVAlign(VAlign.CENTER);
// Add the table to the page
page.getElements().add(table);
// Save the PDF
document.draw("C:/MyDocument.pdf");