PDFlib Cookbook

cookbook

blocks/fill_converted_formfields

Output an imported PDF page with its Blocks filled with different personalized data.

Download Java Code  Switch to PHP Code  Show Output  Show Input (form_with_blocks.pdf) 

/*
 * Fill converted form fields:
 * Output an imported PDF page with its blocks being filled with different 
 * personalized data
 * 
 * Import a PDF page whose form fields have been converted to blocks.
 * Fill some blocks representing text fields, checkboxes and radio buttons
 * appropriately.   
 * 
 * Required software: PPS 10
 * Required data: PDF document containing blocks
 */
package com.pdflib.cookbook.pdflib.blocks;

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

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

        /* By default annotations are also imported. In some cases this
         * requires the Noto fonts for creating annotation appearance streams.
         * We therefore set the searchpath to also point to the font directory.
         */
        String fontpath = "../resource/font";
        String outfile = "fill_converted_formfields.pdf";
        String title = "Fill Converted Form Fields";

        pdflib p = null;
        double width, height;
        String infile = "form_with_blocks.pdf";
        int inpage, indoc;
        String text_optlist;
        int exitcode = 0;

        /*
         * The following blocks are contained in the imported page:
         * "plane model" representing a text field "quantity" representing a
         * text field "color" and "color_1" representing two radio buttons
         * "perforation" and "glossy" representing two checkboxes
         */
        try {
            p = new pdflib();

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

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

            /* Open a PDF containing blocks */
            indoc = p.open_pdi_document(infile, "");
            if (indoc == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Open the first page */
            inpage = p.open_pdi_page(indoc, 1, "");
            if (inpage == -1)
                throw new Exception("Error: " + p.get_errmsg());

            /* Get the width and height of the imported page */
            width = p.pcos_get_number(indoc, "pages[0]/width");
            height = p.pcos_get_number(indoc, "pages[0]/height");

            /* Start the output page with the size given by the imported page */
            p.begin_page_ext(width, height, "");

            /* Place the imported page on the output page */
            p.fit_pdi_page(inpage, 0, 0, "");

            /* Fill the "plane model" block with some text */
            if (p.fill_textblock(inpage, "plane model", "Long Distance Glider", "") == -1)
                System.err.println("Warning: " + p.get_errmsg());

            /* Fill the "quantity" text block with a quantity of 1 */
            if (p.fill_textblock(inpage, "quantity", "1", "") == -1)
                System.err.println("Warning: " + p.get_errmsg());

            /*
             * Fill the "color" block (representing a radiobutton) with a
             * circle. The circle is supplied as "l". The encoding "builtin"
             * will retrieve the circle symbol from the encoding which is
             * integrated in the ZapfDingbats font. The following symbols are
             * represented by the respective characters in the ZapfDingbats
             * font: Check=4, Circle=1, Cross=8, Diamond=u, Square=n, Star=H
             */
            text_optlist = 
                "fontname=ZapfDingbats encoding=builtin position=center";
            if (p.fill_textblock(inpage, "color", "l", text_optlist) == -1)
                System.err.println("Warning: " + p.get_errmsg());

            /*
             * Fill the "color_1" block (for a radiobutton) with a space.
             * Otherwise, the border of the block representing the empty check
             * box would not be shown in the output document.
             */
            text_optlist = 
                "fontname=ZapfDingbats encoding=builtin position=center";
            if (p.fill_textblock(inpage, "color_1", " ", text_optlist) == -1)
                System.err.println("Warning: " + p.get_errmsg());

            /*
             * Fill the "perforation" block (for a checkbox) with a check mark
             * encoded in Unicode.
             */
            text_optlist = "fontname=ZapfDingbats encoding=unicode fontsize=12 position=center";
            if (p.fill_textblock(inpage, "perforation", "\u2714", text_optlist)
                                                                    == -1)
                System.err.println("Warning: " + p.get_errmsg());

            /*
             * Fill the "glossy" block (for a checkbox) with a space. Otherwise,
             * the border of the block representing the empty check box would
             * not be shown in the output document.
             */
            text_optlist = "fontname=ZapfDingbats encoding=unicode fontsize=12 position=center";
            if (p.fill_textblock(inpage, "glossy", " ", text_optlist) == -1)
                System.err.println("Warning: " + p.get_errmsg());

            p.end_page_ext("");

            p.close_pdi_page(inpage);

            p.end_document("");
            p.close_pdi_document(indoc);
        }
        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.toString());
            exitcode = 1;
        }
        finally {
            if (p != null) {
                p.delete();
            }
            System.exit(exitcode);
        }
    }
}