PDFlib Cookbook

cookbook

pdfua/image_with_link_pdfua1

Create PDF/UA-1 document where an image is used as background for a link.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Image with Link in PDF/UA-1
 * Create PDF/UA-1 document where an image is used as background for a link
 * 
 * The background image for a link must be tagged as Artifact since Figure
 * is not allowed as child of Link.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: font file, image
 */

package com.pdflib.cookbook.pdflib.pdfua;

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

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

        pdflib p = null;
        int id_P, id_Link, image, font, action;
        String optlist;
        String title = "image_with_link_pdfua1";
        int exitcode = 0;

        try {
            p = new pdflib();
            /*
             * errorpolicy=exception means that the program will stop
             * if one of the API functions runs into a problem.
             */
            p.set_option("errorpolicy=exception searchPath={" + searchpath + "}");

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

            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 taborder=structure");

            p.create_bookmark("PDF/UA-1 demo", "");

            font = p.load_font("NotoSerif-Regular", "unicode", "embedding");
            p.setfont(font, 24.0);
            
            /* Create a P tag as container of text and link */
            id_P = p.begin_item("P", "");
            
            p.fit_textline("The image serves as background for a link:", 
                50, 625, "fontsize=12");

            /* ============== Link with background image =================== */
            
            /* Create the Link element. The alternative text describes both
             * the background image and the link
             */
            id_Link = p.begin_item("Link", "Alt={Visit Kraxi on the Web}");

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

            /* The background image must be tagged as Artifact; the matchbox
             * provides the geometry for create_annotation() below.
             */
            p.fit_image(image, 50, 400,
                "tag={tagname=Artifact} scale=0.5 matchbox={name={kraxi}}");
            p.close_image(image);

            /* Create URI action */
            action = p.create_action("URI", "url={http://www.kraxi.com}");

            /* Create Link annotation on named matchbox "kraxi".
            * This automatically creates an OBJR (object reference) element
            * inside the Link element.
            */
            optlist = "linewidth=0 usematchbox={kraxi} " +
                "contents={Link to Kraxi Inc. Web site} " +
                "action={activate=" + Integer.toString(action) + " } ";
            p.create_annotation(0, 0, 0, 0, "Link", optlist);

            /* Close the Link and P structure elements */
            p.end_item(id_Link);
            p.end_item(id_P);

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