ceTe Software Help Library for Java August - 2020
DynamicPDF Generator for Java / Programming with Generator for Java / Page Elements / Tables / Adding Page Elements to Cells
In This Topic
    Adding Page Elements to Cells
    In This Topic

    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.

    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 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 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  annotations 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);
            
            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");
    
    See Also