PDFlib Cookbook

cookbook

pdfa/form_fields_pdfa2b updated

Form fields within PDF/A-2b.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Create form fields for PDF/A-2b
 * 
 * Required software: PDFlib/PDFlib+PDI/PPS 9.3
 * Required data: font file
 */

/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";

$exitcode = 0;

$title = "Form fields in PDF/A-2b";

try {
    $x1 = 50; $x2 = 150; $y = 750;
    $fieldheight=24; $fieldwidth=200; $fieldsmall=12;

    $p = new pdflib();

    /*
     * errorpolicy=exception means that program will stop
     * if one of the API function runs into a problem.
     */
    $p->set_option("errorpolicy=exception searchpath={" . $searchpath . "}");

    $p->begin_document("", "pdfa=PDF/A-2b") ;

    if ($p->load_iccprofile("sRGB", "usage=outputintent") == 0){
        throw new Exception("Error: " . $p->get_errmsg() ."<br/>\n" . 
            "See www.pdflib.com for output intent ICC profiles.");
    }

    $p->set_info("Creator", "PDFlib Cookbook");
    $p->set_info("Title", $title);

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

    $font = $p->load_font("NotoSerif-Regular", "winansi", "simplefont embedding nosubsetting");

    $p->setfont($font, 24.0);
    $p->fit_textline("Kraxi paper planes order form", $x1, $y, "");
    $p->create_bookmark("Kraxi paper planes order form", "");

    $p->setfont($font, 12.0);


    /* =================== Text field ======================== */
    $y -= 3 * $fieldheight;
    $labeltext = "Enter name:";
    $p->fit_textline($labeltext, $x1, $y + 0.5* $fieldheight, "");

    $optlist = "currentvalue= {John Doe} bordercolor={gray 0.7} backgroundcolor={gray 0.9} font=" . $font;

    $p->create_field($x2, $y, $x2+$fieldwidth, $y+$fieldheight,
        "name", "textfield", $optlist);


    /* =================== Combo box ======================== */
    $y -= 3 * $fieldheight;

    $labeltext = "Select size:";
    $p->fit_textline($labeltext, $x1, $y + 0.5* $fieldheight, "");

    /* Create a form field of type "combobox".
     * Set the values for the combobox items (itemnamelist={0 1 2 3 4}).
     * Set the labels for the combobox items (itemtextlist={...}).
     * Set the focus on the last item (currentvalue=XXL).
     * Allow the user to change an item (editable=true)
     */

    $optlist = 
        "font=" . $font . " fontsize=12 backgroundcolor={gray 0.9} " .
        "bordercolor={gray 0.7} itemnamelist={0 1 2 3 4} " .
        "currentvalue=XXL itemtextlist={S M L XL XXL} editable=true";

    $p->create_field($x2, $y, $x2 + $fieldwidth, $y + $fieldheight,
        "size", "combobox", $optlist);


    /* =================== List box ======================== */		    
    $y -= 4 * $fieldheight;

    $labeltext = "Select model:";
    $p->fit_textline($labeltext, $x1, $y, "");

    /* Create a form fiels of type "listbox".
     * Set the values for the list items (itemnamelist={0 1 2 3}).
     * Set the labels for the list items (itemtextlist={...}).
     * Set the focus on the second item (currentvalue=1).
     */
    $optlist =
        "font=" . $font . " fontsize=12 backgroundcolor={gray 0.9} " .
        "bordercolor={gray 0.7} itemnamelist={0 1 2 3} currentvalue=1 " .
        "itemtextlist={{Long Distance Glider} {Giant Wing} " .
        "{Cone Head Rocket} {Super Dart}}";
    $y -= 2.5 * $fieldheight;

    $p->create_field($x2, $y, $x2 + $fieldwidth, $y + 3*$fieldheight,
        "model", "listbox", $optlist);


    /* =================== Check boxes ======================== */
    $y -= 2 * $fieldheight;

    $labeltext = "Select extras:";
    $p->fit_textline($labeltext, $x1, $y, "");

    /* Create several form fields of type "checkbox" */
    $optlist = "buttonstyle=cross bordercolor={gray 0} " .
        "backgroundcolor={rgb 0.95 0.95 1} fillcolor={rgb 0.25 0 0.95}";

    /* Emit the Caption text before the field because logically it comes first. */
    $labeltext = "glossy paper";
    $p->fit_textline($labeltext, $x2 + 2 * $fieldsmall, $y, "");
    $p->create_field($x2, $y, $x2 + $fieldsmall, $y + $fieldsmall,
        $labeltext, "checkbox",
        $optlist . " currentvalue={On}");

    $y -= $fieldheight;
    $labeltext = "rainbow colors";
    $p->fit_textline($labeltext, $x2 + 2 * $fieldsmall, $y, "");
    $p->create_field($x2, $y, $x2 + $fieldsmall, $y + $fieldsmall,
        $labeltext, "checkbox",
        $optlist);


    /* =================== radio buttons ======================== */
    $y -= 3 * $fieldheight;

    $labeltext = "Select color:";
    $p->fit_textline($labeltext, $x1, $y, "");

    /* First create a form field group called "colors" */
    $p->create_fieldgroup("colors", "fieldtype=radiobutton");

    /* Create several form fields of type "radiobutton". 
     * All fields belong to the "colors" field grou$p-> Indicate this
     * relationship by providing each radiobutton field name with the
     * prefix "colors.".
     * Activate the first radio button (currentvalue={On}).
     */
    $optlist = "buttonstyle=circle bordercolor={gray 0.8}";

    /* Emit the Caption text before the field because logically it comes first. */
    $labeltext = "standard";
    $p->fit_textline($labeltext, $x2 + 2 * $fieldsmall, $y, "");
    $p->create_field($x2, $y, $x2 + $fieldsmall, $y + $fieldsmall,
        "colors." . $labeltext, "radiobutton",
        $optlist . " currentvalue={On} ");

    $y -= $fieldheight;

    $labeltext = "yellow";
    $p->fit_textline($labeltext, $x2 + 2 * $fieldsmall, $y, "");
    $p->create_field($x2, $y, $x2 + $fieldsmall, $y + $fieldsmall,
        "colors." . $labeltext, "radiobutton",
        $optlist);

    $y -= $fieldheight;

    $labeltext = "blue";
    $p->fit_textline($labeltext, $x2 + 2 * $fieldsmall, $y, "");			
    $p->create_field($x2, $y, $x2 + $fieldsmall, $y + $fieldsmall,
        "colors." . $labeltext, "radiobutton",
        $optlist);

    $p->end_page_ext("");
    $p->end_document("");

    $buf = $p->get_buffer();
    $len = strlen($buf);

    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=form_fields_pdfa2b.pdf");
    print $buf;

} catch (PDFlibException $e) {
    echo("PDFlib exception occurred:\n" .
        "[" . $e->get_errnum() . "] " . $e->get_apiname() .
        ": " . $e->get_errmsg() . "\n");
    exit(1);
} catch (Throwable $e) {
    echo("PHP exception occurred: " . $e->getMessage() . "\n");
    exit(1);
}
$p = 0;
?>