PDFlib Cookbook

cookbook

pdfua/table_of_contents_pdfua1

Demonstrate PDF/UA list tagging.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Demonstrate tagging for a table of contents containing text, page
 * number, dot leaders and Link annotations
 *
 * 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 table_of_contents_pdfua1 {
    public static void main(String argv[]) {
        pdflib p = null;

        String title = "table_of_contents_pdfua1";
        int exitcode = 0;

        /* Dummy entries for the table of contents */
        final String toc_items[][] = {
            { "1.", "Long Distance Glider",   "1" },
            { "2.", "Giant Wing",             "1" },
            { "3.", "Cone Head Rocket",       "1" },
            { "4.", "Super Dart",             "1" }
        };

        try {
            int id_toc;
            double x0 = 50, x1 = 70, x2=450, y, leading, fontsize=14;

            /* This is where the data files are. Adjust as necessary. */
            String searchpath = "../input";

            p = new pdflib();

            p.set_option("errorpolicy=exception searchpath={" + searchpath  + "}");

            p.begin_document(title + ".pdf",
                "pdfua=PDF/UA-1 lang=en tag={tagname=Document}");

            p.set_info("Creator", "PDFlib Cookbook");
            p.set_info("Title", 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");

            p.create_bookmark("Tagged table of contents demo", "");

            /* Emit heading */
            y = 700;
            leading = 1.5 * fontsize;
            
            /* Heading */
            p.fit_textline("Table of contents", x0, y,
                "tag={tagname=H1} " +
                "fontname=NotoSerif-Regular fontsize=24 encoding=unicode embedding " +
                "fillcolor=blue");
            
            y -= leading;
            
            /* Start table of contents (TOC) structure element */
            id_toc = p.begin_item("TOC", "");

            /* Emit several table of contents items (TOCI) with substructure */
            for (int i = 0; i < toc_items.length; i++)
            {
                int id_toci;
                int action;
                
                y -= leading;

                /* Create TOCI element with nested P, Reference and Link elements in a single call */
                id_toci = p.begin_item("TOCI",
                    "tag={tagname=P tag={tagname=Reference inline=false tag={tagname=Link}}}");
                
                // Emit the chapter number and tag it as Lbl (requires PDFlib 9.2.0p3 or above)
                //p.fit_textline(toc_items[i][0], x0, y,
                //    "fontname=NotoSerif-Regular fontsize=14 encoding=unicode embedding " +
                //    "boxsize={20 30} " +
                //    "matchbox={name={toc_entry" + i + "} boxheight={fontsize descender}} " +
                //    "tag={tagname=Lbl}");

                /* Emit the text and define a named matchbox which will be used
                 * to create the Link annotation. The dot leaders between text
                 * and page number are automatically tagged as Artifact.
                 */
                p.fit_textline(toc_items[i][1], x1, y,
                    "fontname=NotoSerif-Regular fontsize=14 encoding=unicode embedding " +
                    "boxsize={" + (x2-x1) + " 30} leader={alignment=right text={.}} " +
                    "matchbox={name={toc_entry" + i + "} boxheight={fontsize descender}}");

                /* Emit page number using a matchbox with the same name */ 
                p.fit_textline(toc_items[i][2], x2, y,
                    "fontname=NotoSerif-Regular fontsize=14 encoding=unicode embedding " +
                     "boxsize={20 30} position={right bottom} fillcolor=blue " +
                     "matchbox={name={toc_entry" + i + "} boxheight={fontsize descender}}");

                /* Create GoTo action (to simplify this topic we pretend to
                 * jump to page 1) */
                action = p.create_action("GoTo", "destination={page=1}");
                
                /* Create Link annotation on named matchbox "toc_entry<i>".
                 * Each matchbox consists of two rectangles (text and number).
                 * This automatically creates suitable OBJR (object reference)
                 * elements for each annotation.
                 */
                 p.create_annotation(0, 0, 0, 0, "Link",
                     "usematchbox={toc_entry" + i + "} linewidth=0 " +
                     "contents={Link to chapter start} " +
                     "action={activate=" + Integer.toString(action) +  " }");

                 /* This closes Link, Reference, P and TOCI */
                p.end_item(id_toci);
            }

            p.end_item(id_toc);

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