/* $Id: fit_formfield_into_cell.java,v 1.2 2008/04/22 19:22:19 tm Exp $
 * Fit form field into cell:
 * Fit a form field into a table cell, e.g. to create a pushbutton in the
 * cell.
 *
 * Use add_table_cell() with the "matchbox" option to define a named matchbox
 * for the table cell to contain the form field. After the call of fit_table()
 * retrieve the coordinates of the table cell using info_matchbox() and the
 * matchbox name. Then supply the received 4 coordinates to create_field() for
 * creating a pushbutton which fits exactly into the cell.
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 7
 * 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";
    String outfile = "fit_formfield_into_cell.pdf";
    String title = "Fit Form Field into Cell";

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

    final int margin = 4;

    /* URL to be referenced by the link */
    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
     */
    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_parameter("SearchPath", searchpath);

        /* This means we must check return values of load_font() etc. */
        p.set_parameter("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 + " $Revision: 1.2 $");

        /* Load the font */
        font = p.load_font("Helvetica", "winansi", "");
        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} font=" + font +
            " 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 = "font=" + font + " " + "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());


        /* -----------------------------------------------------------------
         * Adding an empty cell in the second column of the second  row with
         * a named matchbox defined.
         * -----------------------------------------------------------------
         */
        optlist = "colwidth=" + c2 + " margin=" + margin +
            " matchbox={name=download}";

        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 */
        	}
        }

        /* Retrieve the x and y coordinates of the lower left corner of the
         * matchbox named "download" as well as its width and height
         */
        double x = p.info_matchbox("download", 1, "x1");
        double y = p.info_matchbox("download", 1, "y1");
        double w = p.info_matchbox("download", 1, "width");
        double h = p.info_matchbox("download", 1, "height");

        /* Create a "URI" action for opening the URL */
        optlist = "url={" + url + "}";
        action = p.create_action("URI", optlist);

        /* Create a pushbutton with the "URI" action and coordinates similar
         * to the coordinates of the matchbox "download" retrieved above
         */
        optlist = "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";

        p.create_field(x, y, x+w, y+h, "download", "pushbutton", optlist);

        p.end_page_ext("");

        p.end_document("");

        } catch (PDFlibException e) {
            System.err.print("PDFlib exception occurred:\n");
            System.err.print("[" + e.get_errnum() + "] " + e.get_apiname() +
                ": " + e.get_errmsg() + "\n");
        } catch (Exception e) {
            System.err.println(e.getMessage());
        } finally {
            if (p != null) {
                p.delete();
            }
        }
    }
}
