BASKET
Search
PDFlib

interactive/form_and_layers

Download Java Code        Switch to PHP Code    Show Output PDF

/* $Id: form_and_layers.java,v 1.8 2013/03/26 15:38:01 stm Exp $

 * Forms and Layers:

 * Define two layers for English or German contents and hide or show

 * them via form field buttons

 *

 * Define two layers for displaying some text and a "combobox" form field

 * together with its list items in English or German. Create the two form field

 * buttons "English" and "Deutsch" which show the English or the German layer

 * when the user presses the respective button.

 *

 * Required software: PDFlib/PDFlib+PDI/PPS 9

 * Required data: none

 */

package com.pdflib.cookbook.pdflib.interactive;


import com.pdflib.pdflib;

import com.pdflib.PDFlibException;


public class form_and_layers {

    public static void main(String argv[]) {

        /* This is where the data files are. Adjust as necessary. */

        String searchpath = "../input";

        String outfile = "form_and_layers.pdf";

        String title = "Form and Layers";


        pdflib p = null;

        String optlist;

        int font, en_act, de_act, layerEN, layerDE;

        double width = 100, height = 18, llx = 100, lly = 600;


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


            /* Open the document with the "Layers" navigation tab visible */

            if (p.begin_document(outfile, "openmode=layers") == -1)

                throw new Exception("Error: " + p.get_errmsg());


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

            p.set_info("Title", title + " $Revision: 1.8 $");


            /* Load the font */

            font = p.load_font("Helvetica", "winansi", "");

            if (font == -1) {

                throw new Exception("Error: " + p.get_errmsg());

            }


            /* Start page */

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


            /*

             * ------------------------------------------------------------------

             * Define the layer "English" and place some English text as well as

             * a form field of type "combobox" with some English list items on

             * it

             * ----------------------------------------------------------------

             * --

             */

            layerEN = p.define_layer("English", "");

            p.begin_layer(layerEN);


            /* Output an English combobox title */

            p.setfont(font, 12);

            p.fit_textline(

                "Choose a color from the list or enter an individual "

                    + "color:", llx, lly, "");

            lly -= 30;


            /*

             * Create the "color" form field of type "combobox" with some

             * English list items and a height similar to the height of one list

             * item

             */

            optlist = "font="

                + font

                + " fontsize=14 backgroundcolor={gray 0.9} "

                + "bordercolor={gray 0.7} itemnamelist={0 1 2 3 4} currentvalue=4 "

                + "itemtextlist={yellow green blue red white} editable=true layer="

                + layerEN;


            p.create_field(llx, lly, llx + width, lly + height, "color",

                "combobox", optlist);


            /*

             * -----------------------------------------------------------------

             * Define the layer "German" and place some German text as well as a

             * form field of type "combobox" with some German list items on it

             * -----------------------------------------------------------------

             */

            layerDE = p.define_layer("German", "initialviewstate=false "

                + "initialprintstate=false");

            p.begin_layer(layerDE);


            lly = 600;

            p.fit_textline(

                "W\u00E4hlen Sie eine Farbe aus der Liste oder geben Sie "

                    + "eine eigene Farbe ein:", llx, lly, "");

            lly -= 30;


            /*

             * Create the "farbe" form field of type "combobox" with some German

             * list items and a height similar to the height of one list item

             */


            optlist = "font="

                + font

                + " fontsize=14 backgroundcolor={gray 0.9} "

                + "bordercolor={gray 0.7} itemnamelist={0 1 2 3 4} currentvalue=4 "

                + "itemtextlist={gelb gr\u00FCn blau rot wei\u00DF} editable=true layer="

                + layerDE;


            p.create_field(llx, lly, llx + width, lly + height, "farbe",

                "combobox", optlist);


            /*

             * At most one of the "English" and "German" layers should be

             * visible

             */

            p.set_layer_dependency("Radiobtn", "group={" + layerEN + " "

                + layerDE + "}");


            /*

             * Create a "SetOCGState" action which shows the English layer.

             * Since at most one layer may be visible we don't need to

             * explicitly hide the German layer.

             */

            en_act = p.create_action("SetOCGState", "layerstate={on " + layerEN

                + "}");


            /*

             * Create a "SetOCGState" action which shows the German layer. Since

             * at most one layer may be visible we don't need to explicitly hide

             * the English layer.

             */

            de_act = p.create_action("SetOCGState", "layerstate={on " + layerDE

                + "}");


            /*

             * Create the "english" and "german" form fields of type

             * "pushbutton". Using the action defined above switch to the other

             * layer when the user releases the mouse button inside the field's

             * area (action={up <actionhandle>}).

             */

            lly = 680;

            optlist =

                "bordercolor={rgb 0.25 0 0.95} backgroundcolor={rgb 0.95 0.95 1} "

                + "fillcolor={rgb 0.25 0 0.95} font=" + font + " fontsize=14";


            p.create_field(llx, lly, llx + width, lly + height, "english",

                "pushbutton", optlist + " caption={English} action={up "

                    + en_act + "}");


            llx += 150;


            p.create_field(llx, lly, llx + width, lly + height, "german",

                "pushbutton", optlist + " caption={Deutsch} action={up "

                    + de_act + "}");


            /* Complete all layers */

            p.end_layer();


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

            }

        }

    }

}