PDFlib Cookbook

cookbook

pdfua/tag_parallel_columns

Download Java Code     Switch to PHP Code     Show Output PDF

/* $Id: tag_parallel_columns.java,v 1.3 2014/01/30 14:05:28 rjs Exp $
 * tag_parallel_columns
 *
 * 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 9
 * Required data: font
 */
package com.pdflib.cookbook.pdflib.pdfua;

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

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

        pdflib p = null;
        int font, x_left = 50, x_right = 350, y1 = 600, y2 = 580;
        int id_sect1, id_sect2;
	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 + " $Revision: 1.3 $");

            font = p.load_font("DejaVuSerif", "unicode", "embedding");
            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_sect1 = p.begin_item("Sect", "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 P open we must explicitly designate the
             * root element as parent for the right column.
             */
            id_sect2 = p.begin_item("Sect", "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_sect1);
            p.fit_textline("continued", x_left, y2, "tag={tagname=P}");
            p.end_item(id_sect1);

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

            p.end_page_ext("");

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