In the Tables topic, it is shown how to create a simple table using strings of text for each cell. The Table object also allows adding any of our page elements (including another table) 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.
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 that has to be added, to the cell does implement IArea, then add that element directly to a cell in the row. This is displayed by the Row 3 below adding the FormattedTextArea.Adding Non-IArea Elements
If the page element that has to be added to the table does not implement IArea then before adding that page element to the table, first add it to an AreaGroup. The area group is designed for is to give you the ability to set a height and width on page elements that do not have height and width 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 Links, Bookmarks, Notes and Form Fields
Adding page elements that create PDF page annotation to a cell is not currently supported. This includes Links, Bookmarks, Notes and Form Field elements. This will be supported in a later version of the product.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 elements to that group and then add the group to the cell.[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 );
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, RgbColor.getBlack(),
RgbColor.getGray());
row1.setAlign(CellAlign.CENTER);
row1.setVAlign(CellVAlign.CENTER);
row1.getCellList().add("Page Element", 3);
// The second row is the column headings
Row row2 = table.getRows().add(Font.getHelveticaBoldOblique(), 12);
row2.aetAlign(CellAlign.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("FormattedTextArea");
String formattedText = "<font face='Times'>font face, </font><font " +
"color='FF0000'>color, </font><b>bold.</b>";
row3.getCellList().add(new FormattedTextArea(formattedText, 0, 0, 140, 50,
FontFamily.getHelvetica(), 12, false));
Cell cell1 = row3.getCellList().add("YES");
cell1.setAlign(CellAlign.CENTER);
cell1.setVAlign(CellVAlign.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(CellAlign.CENTER);
cell2.setVAlign(CellVAlign.CENTER);
// Add the table to the page
page.getElements().add(table);
// Save the PDF
document.draw("[PhysicalPath]/MyDocument.pdf");
| See Also |
com.cete.dynamicpdf.pageelements.Cell | com.cete.dynamicpdf.pageelements.Cell.getElement() | Table | IArea Interface | AreaGroup | PageElements | Programming With Generator for Java

