In the previous topic, you saw how to create a simple table using strings of text for each cell. The Table object also allows you to add any of our 37 page elements (including another table) into a cell in a table.
In the below example you will see how it is possible to add any of our page elements to the table. In this example you will 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 HtmlTextArea page element that implements IArea, while Row 4 adds two Line page elementsthat do not implement IArea. Let's take a look at the differences.
Adding IArea Elements
Some page elements implement IArea (Rectangle, Image, Label, etc.), while others do not (Line, Circle, etc.). The implementation of IArea simply means that the particular page element can have a defined height and width. If the particular page element you want to add, to the cell does implement IArea, then you may add that element directly to a cell in the row. This is displayed by the Row 3 below adding the HtmlTextArea.Adding Non-IArea Elements
If the page element that you would like to add to the table does not implement IArea then before adding that page element to the table you must first add it to an AreaGroup. One thing the area group is designed for is to give you the ability to set a height and width on page elements that do not already have one defined. This is all displayed in row 4 with the addition of two line page elements to an area group and then adding the area group to a cell in the row.Adding Multiple Page Elements
More than one page element can also be added to a cell. For this an AreaGroup would be used again. First define an AreaGroup, add all your elements to that group and then add the group to the cell.[Java]
Table table = new Table(0, 0, 400, 400);
// 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
Row row1 = table.getRows().add(20, Font.getHelveticaBold(), 14, Color.getBlack(),
Color.getGray());
row1.setAlign(TextAlign.CENTER);
row1.setVAlign(VAlign.CENTER);
row1.getCellList().add("Page Element", 3);
// The second row is the column headings
Row row2 = table.getRows().add(Font.getHelveticaBoldOblique(), 12);
row2.aetAlign(TextAlign.CENTER);
row2.getCellList().add("Name");
row2.getCellList().add("Element");
row2.getCellList().add("Implements IArea");
// Add IArea page elements directly to the cell
Row row3 = table.getRows().add(30);
row3.getCellList().add("HtmlTextArea");
String htmlText = "<font face='Times'>font face, </font><font " +
"color='FF0000'>color, </font><b>bold.</b>";
row3.getCellList().add(new HtmlTextArea(htmlText, 0, 0, 140, 50,
FontFamily.getHelvetica(), 12, false));
Cell cell1 = row3.getCellList().add("YES");
cell1.setAlign(TextAlign.CENTER);
cell1.setVAlign(VAlign.CENTER);
// Add page elemnts (Lines) to a cell using the AreaGroup
Row row4 = table.getRows().add(30);
row4.getCellList().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.getCellList().add(lineGroup);
Cell cell2 = row4.getCellList().add("NO");
cell2.setAlign(TextAlign.CENTER);
cell2.setVAlign(VAlign.CENTER);
// Add the table to the page
page.getElements().add(table);
See Also
com.cete.dynamicpdf.pageelements.Cell | com.cete.dynamicpdf.pageelements.Cell.getElement() | com.cete.dynamicpdf.pageelements.Table | Programming With Generator for Java
