BASKET
Search
PDFlib

interactive/form_textfield_height

Download Java Code       Switch to PHP Code     Show Output PDF

/* $Id: form_textfield_height.java,v 1.8 2013/01/15 10:12:00 stm Exp $

 * Form text field height:

 * Determine the height of a form field of type "textfield" with respect to

 * the font size and vice versa

 *

 * Create a text field with a defined field height and no font size specified.

 * Create a text field with a defined field height and calculate the

 * appropriate font size from the field height. Create a text field with a

 * defined font size and calculate the appropriate field height from the font

 * size.

 *

 * 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_textfield_height

{

    public static void main (String argv[])

    {

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

    String searchpath = "../input";

    String outfile = "form_textfield_height.pdf";

    String title = "Form Textfield Height";

   

    pdflib p = null;


    String optlist;

    int font;

    double fontsize;

    double ascender, descender;

    double width=160, height=30;

    double llx = 10, lly = 700;

    String text = "Enter text here";


    try {

        p = new pdflib();


        /* This means we must check return values of load_font() etc. */

        p.set_option("errorpolicy=return");


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


        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.8 $");


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

        if (font == -1)

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


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

       

       

        /* ------------------------------------------

         * Field height given. No font size specified

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

         *

         * Create a form field of type "textfield" called "date".

         * Provide it with a light blue background and a black border.

         * Allow a maximum of 25 characters to be entered (maxchar=20).

         * If we don't supply any further options the font size will be

         * automatically adjusted to ensure the text being completely fit into

         * the text field. This means that the text get smaller when entering

         * more text.

         */   

        optlist = "backgroundcolor={rgb 0.95 0.95 1} bordercolor={gray 0} " +

            "maxchar=25 currentvalue={" + text + "} font=" + font;

       

        p.create_field(llx, lly, llx + width, lly + height, "date", "textfield",

            optlist);

        lly-=40;

       

        p.fit_textline("Field height given, no font size specified; a " +

            "maximum of 25 characters is allowed.", llx, lly, "font=" + font +

            " fontsize=12");

        p.fit_textline("Acrobat automatically decreases the font size when " +

            "more text is entered.", llx, lly-=20, "font=" + font +

            " fontsize=12");

       

        lly-=100;

       

       

        /* ---------------------------------------------------

         * Field height given. Calculate appropriate font size

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

         */

       

        /* Create another text field "date2" of a given size. Adjust the font

         * size to match the field height: Retrieve the ascender and the

         * descender (the latter is usually negative) of a hypothetical font

         * size equal to the field height and use the font size resulting from

         * the sum of ascender and (positive) descender as a starting point.

         * Then, descrease the font size by e.g. 20% to leave some empty space

         * from the field borders.

         */

        ascender = p.info_font(font, "ascender", "fontsize=" + height);

        descender = p.info_font(font, "descender", "fontsize=" + height);

       

        fontsize = (ascender - descender) * 0.8;

       

        optlist = "backgroundcolor={rgb 0.95 0.95 1}  bordercolor={gray 0} " +

        "currentvalue={" + text + "} font=" + font + " fontsize=" + fontsize;

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

            "textfield", optlist);

        lly-=40;

        p.fit_textline("Field height of 30 is given. Acrobat uses an " +

            "appropriate font size.", llx, lly, "font=" + font +

            " fontsize=12");

       

        lly-=100;

       

       

        /* --------------------------------------------------------

         * Font size given. Calculate appropriate field height size

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

         */

       

        /* Create another text field "date3". In this case, the font size is

         * given as a fixed value of 24 and the field height should be chosen

         * appropriately: Retrieve the ascender and descender (which is usually

         * negative) of the font size 24. The resulting value will be a good

         * starting point for determining the field height, for example.

         *

         * Since the behaviour of Acrobat regarding the chosen font and

         * baseline is not obvious, the field height will be too small in many

         * cases. To avoid that we add a margin of an appropriate percentage of

         * the field height, e.g. 50%.

         */

        ascender = p.info_font(font, "ascender", "fontsize=24");

        descender = p.info_font(font, "descender", "fontsize=24");

       

        height = (ascender - descender) * 1.5;

       

        optlist = "backgroundcolor={rgb 0.95 0.95 1}  bordercolor={gray 0} " +

        "currentvalue={" + text + "} font=" + font + " fontsize=24";

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

            "textfield", optlist);

        lly-=40;

       

        p.fit_textline("Font size of 24 is given. We calculate the field " +

                "height based on the font's ascender and descender.", llx, lly,

            "font=" + font + " fontsize=12");

         

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

            }

        }

    }

}