PDFlib Cookbook

cookbook

pdfua/tag_out_of_order

Download Java Code    Switch to PHP Code      Show Output PDF

/* $Id: tag_out_of_order.java,v 1.5 2014/01/30 14:05:28 rjs Exp $
 * tag_out_of_order
 *
 * This topic demonstrates how to create Tagged PDF out of order, using
 * the following features:
 * - suspend/resume_page() for adding content to a previous page
 * - query the current structure element id and index with get_option()
 * - the "parent" and "index" tagging options for adding structure elements
 *   at a previous location in the structure tree
 *
 * 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_out_of_order {
    public static void main(String argv[]) {
        /* This is where the data files are. Adjust as necessary. */
        String searchpath = "../input";
        String outfile = "tag_out_of_order.pdf";
        String title = "Create Tagged PDF content out of order";

        pdflib p = null;
        int font, x = 50, y1 = 600, y2 = 550;
        int id_parent;
        int index1, index2, index3;
	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());

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

            p.set_info("Creator", "PDFlib Cookbook");
            p.set_info("Title", title + " $Revision: 1.5 $");

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

            p.create_bookmark(title, "");

            // ***************************************************
            // First pass: create partial content on each page.
            // ***************************************************

            id_parent = (int) p.get_option("activeitemid", "");

            // Create page 1
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            p.setfont(font, 24);

            p.fit_textline("Heading of Chapter 1", x, y1, "tag={tagname=H1}");

            /* Fetch the number of kids of the parent */
            index1 = (int) p.get_option("activeitemkidcount", "");

            // Suspend page 1 to resume it later
            p.suspend_page("");

            // Create page 2
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            p.setfont(font, 24);
            p.fit_textline("Heading of Chapter 2", x, y1, "tag={tagname=H1}");
            index2 = (int) p.get_option("activeitemkidcount", "");

            // Suspend page 2 to resume it later
            p.suspend_page("");

            // Create page 3
            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            p.setfont(font, 24);
            p.fit_textline("Heading of Chapter 3", x, y1, "tag={tagname=H1}");
            index3 = (int) p.get_option("activeitemkidcount", "");

            // Suspend page 3 to resume it later
            p.suspend_page("");

            // ***************************************************
            // Second pass: add more content to each page.
            // ***************************************************

            // Revisit page 1
            p.resume_page("pagenumber=1");

            // insert more content after the H1 element
            p.fit_textline("Contents of chapter 1...", x, y2, "fontsize=12 "
                + "tag={tagname=P parent=" + id_parent + " index=" + index1
                + "}");

            // Since we inserted an element we must increase subsequent indices
            index2++;
            index3++;

            p.end_page_ext("");

            // Revisit page 2
            p.resume_page("pagenumber=2");

            // insert more content after the H1 element
            p.fit_textline("Contents of chapter 2...", x, y2, "fontsize=12 "
                + "tag={tagname=P parent=" + id_parent + " index=" + index2
                + "}");

            // Since we inserted an element we must increase the subsequent
            // index
            index3++;

            p.end_page_ext("");

            // Revisit page 3
            p.resume_page("pagenumber=3");

            // insert more content after the H1 element
            p.fit_textline("Contents of chapter 3...", x, y2, "fontsize=12 "
                + "tag={tagname=P parent=" + id_parent + " index=" + index3
                + "}");

            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);
        }
    }
}