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

    New in Version 6.0

    Starting in version 6.0, a new table class called Table2 will replace the prior Table object. The Table2 class can be used to achieve all the functionality that the previous Table object contained as well as many added new features (including individual cell spacing and individual cell borders) and overall efficiency improvements. For the remainder of this help topic and the rest of the help topics in this library any time a table object is mentioned it will be referring to a table created using the Table2 class.

    General Explanation

    In general, a table object will be used to format content within a PDF document and allows for control of the flow of data between sections or pages within that PDF. 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 Adding Page Elements to Cells 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 a cell takes precedence over the formatting specified within a row which in turn takes precedence over the formatting specified within a table. For example, if the entire table contains a certain format, then all the formatting information can be set on the table and every cell in every row will contain the appropriate formatting. If a row contains some formatting different than the rest of the table then the text format can be set on the row and every cell in that row will contain the particular formatting. Finally, if a cell contains a different formatting than the rest of the row then the formatting can be specified on the individual cell. Understanding this hierarchy will help to easily format any cell within the table as needed. Note that the Column class contains no formatting information.

    Simple Example

    To create a table that will appear on the PDF, first instantiate a Table class. Always specify the Height and Width as well as the X and Y coordinates where the table will be placed on the page (the X and Y coordinates is where the top left corner of the Table will be positioned on the page). The height and width specified will be the amount of vertical or horizontal space allotted for the table on a particular page. There is no need to try and guess the height and width of the final table during the instantiation of the Table object (since the table can be based on dynamic data and height or width may not always be known). While these height and width values are required by the Table's constructor, they can be reset once the Table is filled in. One strategy may be to set the height and width values to the biggest possible values that the page can accomidate and then go back and re-set them if the table's content is smaller. In addition to the X, Y, Height and Width, any formatting that has to be the default for the entire table should be specified here.

    Next, define the width of the columns for the table and remember, once column width is set, it is read-only and can not be changed.

    Once the columns are defined, the table can be populated with data. This is done firstly, by adding a row to the table and then adding cells to that row. It is possible to add fewer cells to the row than the existing number of columns in the table, but it is not possible to add more cells than the number of 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.

    Add rows to the table and a particular height for the row can also be set. This height will be the minimum height and 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]
        //Create a document object.
        Document document = new Document();
           
        //Create a page.
        Page page = new Page();
       
        //Create Table2 object.     
        Table2 table2 = new Table2(0, 0, 600, 600);
        
        // Add columns to the table
        table2.getColumns().add(150);
        table2.getColumns().add(90);
        table2.getColumns().add(90);
        table2.getColumns().add(90);
        
        // Add rows to the table and add cells to the rows
        Row2 row1 = table2.getRows().add(40, Font.getHelveticaBold(), 16,
            Grayscale.getBlack(), Grayscale.getGray());    
       
        row1.getCellDefault().setAlign(TextAlign.CENTER);
        row1.getCellDefault().setVAlign(VAlign.CENTER);
        row1.getCells().add("Header 1");
        row1.getCells().add("Header 2");
        row1.getCells().add("Header 3");
        row1.getCells().add("Header 4");
        
        Row2 row2 = table2.getRows().add(30);
        Cell2 cell1 = row2.getCells().add("Rowheader 1", Font.getHelveticaBold(), 16,
            Grayscale.getBlack(), Grayscale.getGray(), 1);
        cell1.setAlign(TextAlign.CENTER);
        cell1.setVAlign(VAlign.CENTER);
        row2.getCells().add("Item 1");
        row2.getCells().add("Item 2");
        row2.getCells().add("Item 3");
                
        Row2 row3 = table2.getRows().add(30);
        Cell2 cell2 = row3.getCells().add("Rowheader 2", Font.getHelveticaBold(), 16,
            Grayscale.getBlack(), Grayscale.getGray(), 1);
        cell2.setAlign(TextAlign.CENTER);
        cell2.setVAlign(VAlign.CENTER);
        row3.getCells().add("Item 4");
        row3.getCells().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.getCells().add("Item 6");
                
        Row2 row4 = table2.getRows().add(30);
        Cell2 cell3 = row4.getCells().add("Rowheader 3", Font.getHelveticaBold(), 16,
            Grayscale.getBlack(), Grayscale.getGray(), 1);
        cell3.setAlign(TextAlign.CENTER);
        cell3.setVAlign(VAlign.CENTER);
        row4.getCells().add("Item 7");
        row4.getCells().add("Item 8");
        row4.getCells().add("Item 9");
        
        // Add the table to the page
        page.getElements().add(table2);
       
        //add page to the document.
        document.getPages().add(page);
       
        //Save document to file
        document.draw("File Path");
    

    In This Section

    Adding Page Elements to Cells
    Explains how to add page elements to a table's cell.
    Table Continuation
    Explains how span a table across multiple pages or columns. 
    See Also