PDFlib Cookbook

cookbook

form_fields/form_synchronized_fields

Create multiple form fields of type 'textfield' with synchronized values

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * Synchronized fields
 * Create multiple form fields of type "textfield" with synchronized values
 * 
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: none
 */
/* This is where the data files are. Adjust as necessary */
$searchpath = dirname(__FILE__,3)."/input";
$outfile = "";
$title = "Form synchronized fields";

$width=150; $height=20; $llx1 = 100; $lly = 600; $llx2=$llx1+200;

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, "") == 0)
        throw new Exception("Error: " . $p->get_errmsg());

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

    $font = $p->load_font("NotoSerif-Regular", "winansi", "simplefont embedding nosubsetting");
    if ($font == 0)
        throw new Exception("Error: " . $p->get_errmsg());
    
    $p->begin_page_ext(0, 0, " width=a4.width height=a4.height");
    
    $p->fit_textline("The fields below contain synchronized values:", $llx1, $lly, "fontsize=14 font=" . $font);


    /* ------------- Synchronized textfields ------------- */
    /* Create field group as container of synchronized fields. */
    $p->create_fieldgroup("name", "fieldtype=textfield currentvalue={John Doe} fontsize=14 font=" . $font);
    
    $lly -= 2*$height;
    $optlist = "backgroundcolor={gray 0.9} bordercolor={gray 0.7}";
    
    $p->create_field($llx1, $lly, $llx1 + $width, $lly + $height, "name.1", "textfield", $optlist);
    $p->create_field($llx2, $lly, $llx2 + $width, $lly + $height, "name.2", "textfield", $optlist);
    
    /* ------------- Synchronized checkboxes ------------- */
    /* Create field group as container of synchronized fields. */
    $p->create_fieldgroup("paper", "fieldtype=checkbox currentvalue=On buttonstyle=cross fontsize=14");

    $lly -= 2*$height;
    $optlist = "backgroundcolor={gray 0.9} bordercolor={gray 0.7}";
    
    $p->create_field($llx1, $lly, $llx1 + $height, $lly + $height, "paper.1", "checkbox", $optlist);
    $p->create_field($llx2, $lly, $llx2 + $height, $lly + $height, "paper.2", "checkbox", $optlist);


    /* ------------- Synchronized comboboxes ------------- */
    /* Create field group as container of synchronized fields. */
    $p->create_fieldgroup("size", "fieldtype=combobox fontsize=14 font=" . $font . " itemnamelist={0 1 2 3 4} currentvalue=XXL itemtextlist={S M L XL XXL} editable");

    $lly -= 2*$height;
    $optlist = "backgroundcolor={gray 0.9} bordercolor={gray 0.7}";
    
    $p->create_field($llx1, $lly, $llx1 + $width, $lly + $height, "size.1", "combobox", $optlist);
    $p->create_field($llx2, $lly, $llx2 + $width, $lly + $height, "size.2", "combobox", $optlist);


    /* ------------- Synchronized listboxes ------------- */
    /* Create field group as container of synchronized fields. */
    $p->create_fieldgroup("plane", "fieldtype=listbox fontsize=14 font=" . $font .
        " itemtextlist={{Long Distance Glider} {Giant Wing} {Cone Head Rocket} {Super Dart}}" .
        "  currentvalue=2");
    
    $lly -= 6*$height;
    $optlist = "backgroundcolor={gray 0.9} bordercolor={gray 0.7}";
    
    $p->create_field($llx1, $lly, $llx1 + $width, $lly + 4*$height, "plane.1", "listbox", $optlist);
    $p->create_field($llx2, $lly, $llx2 + $width, $lly + 4*$height, "plane.2", "listbox", $optlist);

    
    /* ------------- Synchronized pushbuttons ------------- */
    /* Create field group as container of synchronized fields. */
    $p->create_fieldgroup("press", "fieldtype=pushbutton fontsize=14 font=" . $font . " caption={click me}");

    $lly -= 2*$height;
    $optlist = "backgroundcolor={gray 0.9} bordercolor={gray 0.7}";
    
    $p->create_field($llx1, $lly, $llx1 + $width/2, $lly + $height, "press.1", "pushbutton", $optlist);
    $p->create_field($llx2, $lly, $llx2 + $width/2, $lly + $height, "press.2", "pushbutton", $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_synchronized_fields.pdf");
    print $buf;
}
catch (PDFlibException $e) {
    echo("PDFlib exception occurred in form_synchronized_fields sample:\n" .
        "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
        $e->get_errmsg() . "\n");
    exit(1);
}
catch (Throwable $e) {
    echo($e);
    exit(1);
}

$p = 0;
?>