PDFlib Cookbook

cookbook

pdfa/starter_pdfa2b

Download Java Code      Switch to PHP Code      Show Output PDF

/* $Id: starter_pdfa2b.java,v 1.5 2017/01/26 09:00:25 stm Exp $
 *
 * PDF/A-2b starter:
 * Create PDF/A-2b conforming output with layers, transparency and
 * PDF/A attachments.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9 
 * Required data: font file, image file, ICC output intent profile
 *                (see www.pdflib.com for ICC profiles)
 */
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", "nesrin.pdf" };
        String optlist;

	int exitcode = 0;
        int font, image, gstate;
        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", "starter_pdfa2b");

            p.load_iccprofile("sRGB", "usage=outputintent");

            /* 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(595, 842, "");

            /* Font embedding is required for PDF/A */
            font = p.load_font("LuciduxSans-Oblique", "winansi", "embedding");

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

            optlist = "font=" + font + " fontsize=24";
            
            p.begin_layer(layer_english);
            textflow = p.create_textflow(
                    "PDF/A-2b starter sample with layers, transparency " +
                    "and attachments", 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 " +
                    "und Anlagen", 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, " +
                    "de la transparence et des annexes", optlist);
            p.fit_textflow(textflow, 50, 650, 550, 700, "");
            p.delete_textflow(textflow);

            p.begin_layer(layer_image);

            p.setfont(font, 48);

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

            /* Set transparency in the graphics state */
            gstate = p.create_gstate("opacityfill=0.5");
            p.set_gstate(gstate);

            /* Place a transparent diagonal stamp across the image area, in
             * different colors
             */
            optlist = "boxsize={" + width + " " + height + "} stamp=ll2ur";
           
            p.begin_layer(layer_english);
            p.setcolor("fill", "Lab", 100, 28, 75, 0);
            p.fit_textline("Transparent text", 0, 0, optlist);

            p.begin_layer(layer_german);
            p.setcolor("fill", "Lab", 33.725, 5, -52, 0);
            p.fit_textline("Transparenter Text", 0, 0, optlist);

            p.begin_layer(layer_french);
            p.setcolor("fill", "Lab", 0, 0, 0, 0);
            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.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);
        }
    }
}