pdfa/form_fields_pdfa2b
Form fields within PDF/A-2b.
Download Java Code Switch to PHP Code Show Output
/*
* Create form fields for PDF/A-2b
*
* Required software: PDFlib/PDFlib+PDI/PPS 10
* Required data: font file
*/
package com.pdflib.cookbook.pdflib.pdfa;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class form_fields_pdfa2b {
public static void main(String argv[]) {
/* This is where the data files are. Adjust as necessary. */
String searchpath = "../input";
pdflib p = null;
int font;
String optlist;
int exitcode = 0;
String title = "Form fields in PDF/A-2b";
try {
int x1 = 50, x2 = 150, y = 750;
int fieldheight=24, fieldwidth=200, fieldsmall=12;
String labeltext;
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("form_fields_pdfa2b.pdf", "pdfa=PDF/A-2b") ;
if (p.load_iccprofile("sRGB", "usage=outputintent") == -1){
System.err.println("Error: " + p.get_errmsg() );
System.err.println("See www.pdflib.com for output intent ICC profiles.");
p.delete();
System.exit(2);
}
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 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 group. 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("");
}
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);
}
}
}