PDFlib Cookbook

cookbook

table/fit_formfield_into_cell

Fit a form field into a table cell, e.g. to create a pushbutton in the cell.

Download Java Code  Switch to PHP Code  Show Output 

/*
 * Fit form field into cell
 *
 * Use add_table_cell() with the "fieldtype", "fieldname" and "fitfield" options
 * to define a table cell containing a push button form field.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 10
 * Required data: none
 */
package com.pdflib.cookbook.pdflib.table;

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

public class fit_formfield_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_formfield_into_cell.pdf";
        final String title = "Fit Form Field into Cell";
        int exitcode = 0;

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

        final int margin = 4;

        /* URL to be referenced by the link */
        final String url = "http://www.pdflib.com/download/tet";

        /*
         * Height of a table row which is the sum of a font size of 6 and the
         * upper and lower cell margin of 4 each
         */
        final int rowheight = 14;

        /* Width of the three table columns */
        final int c1 = 180, c2 = 120, c3 = 60;

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

        try {
            p = new pdflib();

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

            /* This means we must check return values of load_font() etc. */
            p.set_option("errorpolicy=return");

            if (p.begin_document(outfile, "") == -1)
                throw new Exception("Error: " + p.get_errmsg());

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

            /* Load the font */
            final int font = p.load_font("NotoSerif-Regular", "winansi", 
                "simplefont nosubsetting");
            if (font == -1)
                throw new Exception("Error: " + p.get_errmsg());

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

            /*
             * Add a heading line spanning three columns 
             */
            optlist = 
                "fittextline={position={center top} fontname=NotoSerif-Regular "
                + " fontsize={capheight=6}} rowheight=" + rowheight
                + " margin=" + margin + " colspan=3 " + "colwidth=" + c1;

            tbl = p.add_table_cell(tbl, 1, 1, "Download the latest version",
                optlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /*
             * Adding a Textflow cell in the first column of the second row
             */
            String license = "Try before you buy! We offer free downloads of "
                + "all our software packages which can be used "
                + "without a license key for testing and developing.";

            /*
             * The Textflow is centered vertically, with a margin from all
             * borders.
             */
            optlist = "fontname=NotoSerif-Regular "
                + "fontsize={capheight=6} leading=110%";

            tf = p.add_textflow(tf, license, optlist);
            if (tf == -1)
                throw new Exception("Error: " + p.get_errmsg());

            optlist = "textflow=" + tf
                + " fittextflow={firstlinedist=capheight} " + "margin="
                + margin + " colwidth=" + c1;

            tbl = p.add_table_cell(tbl, 1, 2, "", optlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Create a "URI" action for opening the URL */
            optlist = "url={" + url + "}";
            final int action = p.create_action("URI", optlist);
            
            /*
             * Adding a cell in the second column of the second row with
             * the form field containing the push button. Tie the action 
             * created above to the event that the mouse button is released
             * inside the push button.
             */
            optlist = "colwidth=" + c2 + " "
                    + "fieldtype=pushbutton fieldname={download} "
                    + "fitfield={action={up="+ action + "} linewidth=1 "
                        + "bordercolor={rgb 0.25 0 0.95} "
                        + "backgroundcolor={rgb 0.95 0.95 1} "
                        + "caption={Download now} "
                        + "fillcolor={rgb 0.25 0 0.95} "
                        + "font=" + font + " fontsize=14}";
            
            tbl = p.add_table_cell(tbl, 2, 2, "", optlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /*
             * Adding a text line cell in the third column of the second row
             */
            optlist = "fittextline={position={right center} font=" + font
                + " fontsize={capheight=6}} rowheight=" + rowheight
                + " margin=" + margin + " colwidth=" + c3;

            tbl = p.add_table_cell(tbl, 3, 2, "7.5 MB", optlist);
            if (tbl == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /*
             * ------------- Fit the table -------------
             * 
             * Using "header=1" the table header will include the first line.
             * 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);
        }
    }
}