PDFlib Cookbook

cookbook

pdfa/starter_pdfa2b

Create PDF/A-2b conforming output with layers, transparency and PDF/A attachments.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * PDF/A-2b starter:
 * Create PDF/A-2b conforming output with layers, transparency, annotation and
 * PDF/A attachment.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10 
 * Required data: font file, image file
 */
package com.pdflib.cookbook.pdflib.pdfa;

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

class starter_pdfa2b {
    public static void main(String argv[]) {
        /* This is where the data files are. Adjust as necessary. */
        final String searchpath = "../input";

        pdflib p = null;
        final String imagefile = "zebra.tif";
        final String attachments[] = { "lionel.pdf" };
        String optlist;;
        String title = "starter_pdfa2b";

        int exitcode = 0;
        int font, image;
        int layer_english, layer_german, layer_french, layer_image;
        int textflow;
        double width, height;
        int i;

        try {
            p = new pdflib();

            /*
             * Set errorpoliy to return, this means we must check return
             * values of load_font() etc.
             * Set the search path for fonts and images etc.
             */
            p.set_option("errorpolicy=return SearchPath={" + searchpath + "}");

            /* Initially display the layer panel and show the full page */
            if (p.begin_document("starter_pdfa2b.pdf",
                    "openmode=layers viewerpreferences={fitwindow=true} " +
                    "pdfa=PDF/A-2b") == -1) {
                throw new Exception("Error: " + p.get_errmsg());
            }

            p.set_info("Creator", "PDFlib starter sample");
            p.set_info("Title", title);

            /* Define the layers, with only English and image layers switched on. */
            layer_english = p.define_layer("English text", "");
            layer_german = p.define_layer("German text", "defaultstate=false");
            layer_french = p.define_layer("French text", "defaultstate=false");
            layer_image = p.define_layer("Images", "");

            /* Define a radio button relationship for the language layers, so
             * only one language can be switched on at a time.
             */
            optlist = "group={" + layer_english + " " + layer_german + " "
                    + layer_french + "}";
            p.set_layer_dependency("Radiobtn", optlist);

            p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

            font = p.load_font("NotoSerif-Regular", "unicode", "");

            if (font == -1) {
                throw new Exception("Error: " + p.get_errmsg());
            }

            /* Create Stamp annotation */
            optlist = "iconname=approved contents={PDF/A} font=" + font;
            p.create_annotation( 50, 700, 300, 840, "Stamp", optlist);

            optlist = "font=" + font + " fontsize=24";
            
            p.begin_layer(layer_english);
            textflow = p.create_textflow(
                    "PDF/A-2b starter sample with layers, transparency, " +
                    "annotation and attachment", optlist);
            p.fit_textflow(textflow, 50, 650, 550, 700, "");
            p.delete_textflow(textflow);

            p.begin_layer(layer_german);
            textflow = p.create_textflow(
                    "PDF/A-2b Starter-Beispiel mit Ebenen, Transparenz, " +
                    "Anmerkung und Anlage", optlist);
            p.fit_textflow(textflow, 50, 650, 550, 700, "");
            p.delete_textflow(textflow);
            
            p.begin_layer(layer_french);
            textflow = p.create_textflow(
                    "PDF/A-2b starter exemple avec des calques, " +
                    "transparence, commentaire et attachement", optlist);
            p.fit_textflow(textflow, 50, 650, 550, 700, "");
            p.delete_textflow(textflow);

            p.begin_layer(layer_image);

            image = p.load_image("auto", imagefile, "");

            if (image == -1) {
                throw new Exception("Error: " + p.get_errmsg());
            }
            
            width = p.info_image(image, "width", "");
            height = p.info_image(image, "height", "");
            
            /* Place the image on the page and close it */
            p.fit_image(image, (double) 0.0, (double) 0.0, "");
            p.close_image(image);

            /* Place a transparent diagonal stamp across the image area, in
             * different colors
             */
            optlist = 
                "boxsize={" + width + " " + height + "} stamp=ll2ur font=" + 
                font + " fillcolor={lab 100 0 0} gstate={opacityfill=0.5}";
           
            p.begin_layer(layer_english);
            p.fit_textline("Transparent text", 0, 0, optlist);

            p.begin_layer(layer_german);
            p.fit_textline("Transparenter Text", 0, 0, optlist);

            p.begin_layer(layer_french);
            p.fit_textline("Texte transparent", 0, 0, optlist);

            /* Close all layers */
            p.end_layer();

            p.end_page_ext("");

            /* Construct option list with attachment handles. The attachments must
             * be PDF/A-1 or PDF/A-2 files.
             */
            optlist = "attachments={";
            for (i = 0; i < attachments.length; i += 1) {
                final int attachment_handle =
                        p.load_asset("Attachment", attachments[i],
                                    "description={This is a PDF/A attachment}");

                if (attachment_handle == -1) {
                    throw new Exception("Error loading attachment: " + p.get_errmsg());
                }

                optlist += " " + attachment_handle;
            }
            optlist += "}";
            
            p.end_document(optlist);
        }
        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);
        }
    }
}