PDFlib Cookbook

cookbook

table/fit_annotation_into_cell new

Fit link annotation into a table cell.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Fit annotation into cell
 *
 * Use add_table_cell() with the "annotationtype" and "fitannotation"
 * options to define an image with a Link annotation in a table cell.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.table;

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

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

        final String outfile = "fit_annotation_into_cell.pdf";
        final String title = "Fit Annotation into Cell";
        int exitcode = 0;

        pdflib p = null;
        int tbl = -1;
        String optlist, result;

        final int margin = 4;

        class planedata {
            planedata(String text,
                String image,
                String url) {
                    this.text = text;
                    this.image = image;
                    this.url = url;
                }

            String text;
            String image;
            String url;
        }

        final planedata data[] = {
            new planedata(
                "<fillcolor=red>Long Distance Glider<fillcolor=black>\n" +
                "With this paper rocket you can send all your " +
                "messages even when sitting in a hall or in the cinema pretty near " +
                "the back.\n",
                "plane1.png",
                "https://www.pdflib.com"),
            new planedata(
                "<fillcolor=red>Giant Wing<fillcolor=black>\n" +
                "An unbelievable sailplane! It is amazingly robust and " +
                "can even do aerobatics. But it is best suited to gliding.\n" +
                "This paper arrow can be thrown with big swing. " +
                "We launched it from the roof of a hotel. It stayed in the air a " +
                "long time and covered a considerable distance.\n",
                "plane2.png",
                "https://www.pdflib.com"),

            new planedata(
                "<fillcolor=red>Super Dart<fillcolor=black>\n" +
                "The super dart can fly giant loops with a radius of 4 " +
                "or 5 meters and cover very long distances. Its heavy cone point is " +
                "slightly bowed upwards to get the lift required for loops.\n",
                "plane3.png",
                "https://www.pdflib.com")
        };

        /* Coordinates of the lower left corner of the table fitbox */
        final double llx = 30, lly = 100, urx = 565, ury = 800;

        try {
            p = new pdflib();
            
            p.set_option("searchpath={" + searchpath + "}");

            /* This means an exception occurs in load_font() etc. */
            p.set_option("errorpolicy=exception");

            p.begin_document(outfile, "");

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

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

            /*
             * Add a header row spanning three columns 
             */
            optlist = "fittextline={position={center top} fontname=NotoSerif-Bold "
                + " encoding=unicode embedding fontsize=20} rowheight=30 " 
                + " margin=" + margin + " colspan=2 ";

            tbl = p.add_table_cell(tbl, 1, 1,
                "Overview of our planes (click on plane for more information)",
                optlist);

            for (int i = 0; i < data.length; i += 1) {
                /* Left cell contains a Textflow */
                int tf = p.create_textflow(data[i].text, "fontname=NotoSerif-Regular" 
                    + " encoding=unicode embedding fontsize=12 leading=125%");
                
                tbl = p.add_table_cell(tbl, 1, i+2, "", "textflow=" + tf 
                    + " margin=2 colwidth=300 rowheight=12");

                /* Right cell contains an image with a Link annotation which
                 * is populated with a URI action. The table cell is used
                 * as annotation rectangle.
                 */
                int image = p.load_image("auto", data[i].image, "");
                int action = p.create_action("URI", "url={" + data[i].url +"}");
                
                tbl = p.add_table_cell(tbl, 2, i+2, "", "image=" + image 
                    + " fitimage={position center fitmethod=meet} colwidth=150 " 
                    + " annotationtype=Link fitannotation={action={activate " + action + "} linewidth=0}");
            }

            /*
             * ------------- Fit the table -------------
             * With "header=1" the table header consists of the first row.
             * Using "line=horother linewidth=0.3" the ruling is specified with
             * a line width of 0.3 for all horizontal lines.
             */
            optlist = "header=1 stroke={ {line=horother linewidth=0.3}}";

            result = p.fit_table(tbl, llx, lly, urx, ury, optlist);

            /* Check the result; "_stop" means all is ok */
            if (!result.equals("_stop")) {
                if (result.equals("_error"))
                    throw new Exception("Error: " + p.get_errmsg());
                else {
                    /* Other return values require dedicated code to deal with */
                }
            }

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