PDFlib Cookbook

cookbook

table/nested_tables

Place a sub-table in one table cell.

Download Java Code  Switch to PHP Code  Show Output 

/* 
 * Nested tables:
 * Place a sub-table in one table cell.
 *
 * Create a template which contains a table consisting of three rows and two 
 * columns. Create a table consisting of three rows and three columns. Add a
 * cell in the second column and the second row of the table so that the cell
 * contains the template, e.g. the contained table, as a sub-table.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.table;

import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

public class nested_tables {
    public static void main(String argv[]) {
        /* This is where the data files are. Adjust as necessary. */
        String searchpath = "../input";
        String outfile = "nested_tables.pdf";
        String title = "Nested Tables";

        pdflib p = null;

        /*
         * Option list for the template, i.e. the sub-table
         * Note "position={left bottom}" for being able to calculate the
         * size of the table for subsequent specification of the template size.
         */
        final String taboptlist1 = "stroke={{line=other linewidth=0.1 }} "
            + "fill={{area=table fillcolor={rgb 1 0.9 0.9}}} "
            + "position={left bottom}";

        /*
         * Option list for the outer table
         */
        final String taboptlist2 = "stroke={{line=other linewidth=0.1 } "
            + "{line=frame linewidth=1.0 }} "
            + "fill={{area=table fillcolor={rgb 0.9 0.9 1}}} ";

        String addoptlist1, addoptlist2, fitoptlist1, textoptlist;
        final double fontsize = 14.0;
        final double margin = 5.0;
        final double tabwidth1, tabheight1;
        final double pagewidth = 595;
        final double pageheight = 500;
        double x = 20, y = 470, yoff = 10;
        int exitcode = 0;

        try {
            p = new pdflib();

            p.set_option("searchpath={" + searchpath + "}");

            /* This means we must check return values of load_font() etc. */
            p.set_option("errorpolicy=return");

            if (p.begin_document(outfile, "") == -1)
                throw new Exception("Error: " + p.get_errmsg());

            p.set_info("Creator", "PDFlib Cookbook");
            p.set_info("Title", title);

            /*
             * Create a template which contains a table consisting of three
             * rows and two columns. Pass (0, 0) for the size of the template,
             * as we will calculate the size of the table and provide the
             * size to the template with end_template_ext().
             */
            int templ = p.begin_template_ext(0, 0, "");

            /*
             * Add some cells for the template sub-table and fit them into the
             * template
             */
            addoptlist1 = 
                "fittextline={fontname=NotoSerif-Regular "
                + "fontsize=" + fontsize + "} margin=" + margin;

            int tbl = p.add_table_cell(-1, 1, 1, "tab 1, cell A", addoptlist1);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());

            tbl = p.add_table_cell(tbl, 2, 1, "tab 1, cell B", addoptlist1);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());

            tbl = p.add_table_cell(tbl, 1, 2, "tab 1, cell C", addoptlist1);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());

            tbl = p.add_table_cell(tbl, 2, 2, "tab 1, cell D", addoptlist1);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());

            tbl = p.add_table_cell(tbl, 1, 3, "tab 1, cell E", addoptlist1);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());

            tbl = p.add_table_cell(tbl, 2, 3, "tab 1, cell F", addoptlist1);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());

            p.fit_table(tbl, 0, 0, 1000, 1000, taboptlist1);

            /* Retrieve the width and height of the template sub-table */
            tabwidth1 = p.info_table(tbl, "width");
            tabheight1 = p.info_table(tbl, "height");

            /* Finish the template while specifying the size of it */
            p.end_template_ext(tabwidth1, tabheight1);

            /* Start page */
            p.begin_page_ext(pagewidth, pageheight, "");

            /*
             * Place the sub-table. To illustrate the extent of the sub-table
             * first output it individually. "rewind=1" is only required here
             * for illustration purposes. It resets the table to the state
             * before last fit_table() call which has been used to place the
             * table into the template. Otherwise we would not be able to place
             * the table a second time.
             */
            
            /* Output some descriptive text */
            textoptlist = "fontname=NotoSerif-Regular fontsize=" + fontsize;
            p.fit_textline("Inner table 1 (placed in a template):", x, y,
                textoptlist);

            y -= tabheight1 + yoff;

            fitoptlist1 = taboptlist1 + " rewind=1";
            p.fit_table(tbl, x + tabwidth1, y, x + 2 * tabwidth1, y
                + tabheight1, fitoptlist1);

            p.delete_table(tbl, "");

            /*
             * Create the outer table consisting of three rows and three columns
             */

            /* Output some descriptive text */
            y -= yoff * 4;
            p.fit_textline("Outer table 2:", x, y, textoptlist);

            /*
             * Add some cells to the outer table. The column width is set to the
             * table width of the sub-table which has been retrieved above. The
             * row height is set to the height of the sub-table which has been
             * retrieved above.
             */
            addoptlist2 = 
                "fittextline={fontname=NotoSerif-Regular " 
                + "fontsize=" + fontsize + "} margin=" + margin + " colwidth=" 
                + tabwidth1 + " rowheight=" + tabheight1;

            tbl = p.add_table_cell(-1, 1, 1, "tab 2, cell 1", addoptlist2);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());

            tbl = p.add_table_cell(tbl, 2, 1, "tab 2, cell 2", addoptlist2);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());

            tbl = p.add_table_cell(tbl, 3, 1, "tab 2, cell 3", addoptlist2);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());

            tbl = p.add_table_cell(tbl, 1, 2, "tab 2, cell 4", addoptlist2);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());

            tbl = p.add_table_cell(tbl, 3, 2, "tab 2, cell 5", addoptlist2);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());

            tbl = p.add_table_cell(tbl, 1, 3, "tab 2, cell 6", addoptlist2);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());

            tbl = p.add_table_cell(tbl, 2, 3, "tab 2, cell 7", addoptlist2);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());

            tbl = p.add_table_cell(tbl, 3, 3, "tab 2, cell 8", addoptlist2);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /*
             * Add another cell containing the template sub-table as well as
             * some gray text. The "image" option supplies the template. 
             */
            addoptlist2 = "image=" + templ
                + " fittextline={fontname=NotoSerif-Regular " 
                + " position={center 70} fontsize=26 fillcolor={gray 0.8}}";

            tbl = p.add_table_cell(tbl, 2, 2, "tab 2, cell 9", addoptlist2);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Place the outer table */
            y -= yoff;
            p.fit_table(tbl, x, 20, pagewidth - x, y, taboptlist2);

            p.end_page_ext("");
            p.end_document("");
        }
        catch (PDFlibException e) {
            System.err.println("PDFlib exception occurred:");
            System.err.println("[" + e.get_errnum() + "] " + e.get_apiname() +
                ": " + e.get_errmsg());
            exitcode = 1;
        }
        catch (Exception e) {
            System.err.println(e.toString());
            exitcode = 1;
        }
        finally {
            if (p != null) {
                p.delete();
            }
            System.exit(exitcode);
        }
    }
}