form_fields/form_synchronized_fields
Create multiple form fields of type 'textfield' with synchronized values
Download Java Code Switch to PHP Code Show Output
/* Synchronized form fields
*
* Create pairs of synchronized form fields, i.e. both fields display the same
* values.
*
* The fields in each pair are part of a group with suitable type.
* By using the group name as prefix for the field names the fields
* are part of the corresponding group.
*
* Required software: PDFlib/PDFlib+PDI/PPS 10
* Required data: none
*/
package com.pdflib.cookbook.pdflib.form_fields;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class form_synchronized_fields
{
public static void main (String argv[])
{
/* This is where the data files are. Adjust as necessary */
String searchpath = "../input";
String outfile = "form_synchronized_fields.pdf";
String title = "Form synchronized fields";
pdflib p = null;
int exitcode = 0;
String optlist;
int font;
double 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, "") == -1)
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 nosubsetting");
if (font == -1)
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("");
} 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);
exitcode = 1;
} finally {
if (p != null) {
p.delete();
}
System.exit(exitcode);
}
}
}