PDFlib Cookbook

cookbook

pdfua/table_pdfua1

Demonstrate automatic table tagging.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Demonstrate automatic table tagging
 *
 * required software: PDFlib/PDFlib+PDI/PPS 10
 * required data: image file (dummy text created within the program)
 */
package com.pdflib.cookbook.pdflib.pdfua;

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

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

        String imagefile = "nesrin.jpg";
        
        String title = "table_pdfua1";

        int row, col, font, image, tf = -1, tbl = -1;
        final int rowmax = 5, colmax = 5;
        int exitcode = 0;

        pdflib p = null;
        final double llx = 50, lly = 50, urx = 550, ury = 700;
        String result;
        String optlist;

        /* Dummy text for filling a cell with multi-line Textflow */
        final String tf_text =
            "Sample text created with the Textflow feature in order to " +
            "create multiline content within a table cell.";

        try {
            p = new pdflib();

            /*
             * Set the search path for fonts and images etc.
             */
            p.set_option(
                "errorpolicy=exception SearchPath={" + searchpath + "}");

            if (p.begin_document(title + ".pdf",
                    "pdfua=PDF/UA-1 lang=en tag={tagname=Document}") == -1)
                throw new Exception("Error: " + p.get_errmsg());

            p.set_info("Creator", "PDFlib Cookbook");
            p.set_info("Title", title);
            
            /* Automatically create spaces between chunks of text */
            p.set_option("autospace=true");

            /* -------------------- Add table cells -------------------- */

            /* ---------- Row 1: table header */
            row = 1;
            col = 1;
            font = p.load_font("NotoSerif-Regular", "unicode", "");
            if (font == -1)
                throw new Exception("Error loading font: " + p.get_errmsg());

            optlist = "fittextline={position=center fontsize=14 font=" + font + "}";

            tbl = p.add_table_cell(tbl, col, row, "Header " + col, optlist);
            col++;
            tbl = p.add_table_cell(tbl, col, row, "Header " + col, optlist);
            col++;
            tbl = p.add_table_cell(tbl, col, row, "Header " + col, optlist);
            col++;
            tbl = p.add_table_cell(tbl, col, row, "Header " + col, optlist);
            col++;
            tbl = p.add_table_cell(tbl, col, row, "Header " + col, optlist);

            /* ---------- Row 2: various kinds of content */
            /* ----- Simple text cell */
            row++;
            col = 1;

            optlist = "fittextline={font=" + font + " fontsize=10 orientate=west}";

            tbl = p.add_table_cell(tbl, col, row, "vertical line", optlist);

            /* ----- Colorized background */
            col++;

            optlist = "fittextline={font=" + font + " fontsize=10} "
                    + "matchbox={fillcolor={rgb 0.9 0.5 0}}";

            tbl = p.add_table_cell(tbl, col, row, "some color", optlist);

            /* ----- Multi-line text with Textflow */
            col++;

            optlist = "fontname=NotoSerif-Regular charref fontsize=8";

            tf = p.add_textflow(tf, tf_text, optlist);

            optlist = "margin=2 textflow=" + tf;

            tbl = p.add_table_cell(tbl, col, row, "", optlist);

            /* ----- Rotated image */
            col++;

            image = p.load_image("auto", imagefile, "");
            if (image == -1)
                throw new Exception("Error loading image: " + p.get_errmsg());

            optlist = "image=" + image +
                " fitimage={orientate=west" +
                " tag={tagname=Figure Alt={Nesrin at Zabrisky point}}}";

            tbl = p.add_table_cell(tbl, col, row, "", optlist);

            /* ----- Diagonal stamp */
            col++;

            optlist = "fittextline={font=" + font + " fontsize=10 stamp=ll2ur "
                    + "tag={tagname=Artifact} }";

            tbl = p.add_table_cell(tbl, col, row, "Artifact", optlist);

            /* ---------- Fill row 3 and above with their numbers */
            for (row++; row <= rowmax; row++) {
                for (col = 1; col <= colmax; col++) {
                    String num;

                    num = "Col " + col + "/Row " + row;
                    optlist = "colwidth=20% fittextline={font=" + font
                            + " fontsize=10}";
                    tbl = p.add_table_cell(tbl, col, row, num, optlist);
                }
            }

            /* ---------- Place the table on one or more pages ---------- */

            /*
             * Loop until all of the table is placed; create new pages as long
             * as more table instances need to be placed.
             */
            do {
                p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
                
                p.create_bookmark("Tagged table demo", "");
                
                p.fit_textline("Tagged table demo", 50, 750,
                    "fontname=NotoSerif-Regular fontsize=16 tag={tagname=H1}");
                
                /*
                 * Shade every other row; draw lines for all table cells. Add
                 * "showcells showborder" to visualize cell borders
                 */
                optlist = "header=1 fill={{area=rowodd fillcolor={gray 0.9}}} "
                    + "stroke={{line=other}} "
                    + "tag={tagname=Table Summary={Sample Tagged table}} "
                    + "caption={fitbox={0r 100% 0r 20r} "
                    +     "matchbox={fillcolor=Lavender borderwidth=1.0} "
                    +     "tag={tagname=P} "
                    +     "fittextline={font=" + font + " fontsize=14} "
                    +     "text={Table Caption}}";

                /* Place the table instance */
                result = p.fit_table(tbl, llx, lly, urx, ury, optlist);

                if (result.equals("_error"))
                    throw new Exception("Couldn't place table : "
                            + p.get_errmsg());

                p.end_page_ext("");
            }
            while (result.equals("_boxfull"));

            /* Check the result; "_stop" means all is ok. */
            if (!result.equals("_stop")) {
                if (result.equals("_error")) {
                    throw new Exception("Error when placing table: "
                            + p.get_errmsg());
                }
                else {
                    /*
                     * Any other return value is a user exit caused by the
                     * "return" option; this requires dedicated code to deal
                     * with.
                     */
                    throw new Exception("User return found in Textflow");
                }
            }

            /* This will also delete Textflow handles used in the table */
            p.delete_table(tbl, "");

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