PDFlib Cookbook

cookbook

pdfua/tag_out_of_order_pdfua1

Demonstrate how to create Tagged PDF out of order.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * tag_out_of_order_pdfua1
 *
 * 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 10
 * Required data: font
 */
package com.pdflib.cookbook.pdflib.pdfua;

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

public class tag_out_of_order_pdfua1 {
    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_pdfua1.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);

            font = p.load_font("NotoSerif-Regular", "unicode", "");
            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.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);
        }
    }
}