PDFlib Cookbook

cookbook

pdfua/parallel_columns_pdfua1

Demonstrate how to tag multiple columns in parallel with activate_item().

Download Java Code  Switch to PHP Code  Show Output 

/*
 * parallel_columns_pdfua1
 *
 * This topic demonstrates how to tag multiple columns in parallel with
 * activate_item(). In order to work around Acrobat problems no direct
 * content should be created after activate_item(), but only new structure
 * elements.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: font
 */
package com.pdflib.cookbook.pdflib.pdfua;

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

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

        pdflib p = null;
        int font, x_left = 50, x_right = 350, y1 = 600, y2 = 580;
        int id_div1, id_div2;
        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,
                "pdfua=PDF/UA-1 tag={tagname=Document} lang=en") == -1)
                throw new Exception("Error: " + p.get_errmsg());

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

            font = p.load_font("NotoSerif-Regular", "unicode", "");
            if (font == -1)
                throw new Exception("Error: " + p.get_errmsg());

            p.create_bookmark(title, "");

            /* Automatically create spaces between chunks of text */
            p.set_option("autospace=true");

            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            id_div1 = p.begin_item("Div", "Title={Left column}");
            p.setfont(font, 12);

            /* Start with a H1 for the left column */
            p.fit_textline("Start of left column...", x_left, y1,
                "tag={tagname=H1} fontsize=18");

            /*
             * Since we keep the first Div open we must explicitly designate the
             * root element as parent for the right column.
             */
            id_div2 = p.begin_item("Div", "parent=0 Title={Right column}");
            p.fit_textline("Start of right column...", x_right, y1,
                "tag={tagname=H1} fontsize=18");

            /* Return to the left column and add another structure element */
            p.activate_item(id_div1);
            p.fit_textline("continued", x_left, y2, "tag={tagname=P}");
            p.end_item(id_div1);

            /* Now return to the right column again and add an element */
            p.activate_item(id_div2);
            p.fit_textline("also continued", x_right, y2, "tag={tagname=P}");
            p.end_item(id_div2);

            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);
            exitcode = 1;
        }
        finally {
            if (p != null) {
                p.delete();
            }
            System.exit(exitcode);
        }
    }
}