PDFlib Cookbook

cookbook

pdfua/tagged_list

Download Java Code    Switch to PHP Code      Show Output PDF

/* $Id: tagged_list.java,v 1.3 2014/01/30 14:05:28 rjs Exp $
 *
 * Demonstrate list tagging
 *
 * 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 tagged_list {
    public static void main(String argv[]) {
        pdflib p = null;

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

        try {
            int font;
            int id_list, id_caption, id_listitem;
            double x1 = 50, x2 = 65, y = 700, leading = 36;

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

            p = new pdflib();

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

            if (p.begin_document(title + ".pdf",
                "pdfua=PDF/UA-1 lang=en tag={tagname=Document}") == -1)
                throw new Exception("Error: " + p.get_errmsg());

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

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

            p.set_option("charref=true");

            p.begin_page_ext(595, 842, "");

            font = p.load_font("DejaVuSerif", "unicode", "embedding");

            p.setfont(font, 16);

            p.create_bookmark("Tagged list demo", "");

            p.fit_textline("Tagged list demo", x1, 750,
                "tag={tagname=H1} fontsize=24");

            id_list = p.begin_item("L", "ListNumbering=Disc");

            id_caption = p.begin_item("Caption", "");
            p.fit_textline("The following kinds of fruit are available:", x1,
                y, "tag={tagname=P}");
            y -= leading;
            p.end_item(id_caption);

            id_listitem = p.begin_item("LI", "");
            p.fit_textline("•", x1, y, "tag={tagname=Lbl}");
            p.fit_textline("Apples", x2, y, "tag={tagname=LBody}");
            y -= leading;
            p.end_item(id_listitem);

            id_listitem = p.begin_item("LI", "");
            p.fit_textline("•", x1, y, "tag={tagname=Lbl}");
            p.fit_textline("Oranges", x2, y, "tag={tagname=LBody}");
            y -= leading;
            p.end_item(id_listitem);

            id_listitem = p.begin_item("LI", "");
            p.fit_textline("•", x1, y, "tag={tagname=Lbl}");
            p.fit_textline("Bananas", x2, y, "tag={tagname=LBody}");
            y -= leading;
            p.end_item(id_listitem);

            p.end_item(id_list);

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