pdfua/form_fields_pdfua1
Create accessible form field according to PDF/UA.
Download Java Code Switch to PHP Code Show Output
/*
* Create accessible form fields according to PDF/UA
* Each call to create_field() generates the required OBJR structure element
* automatically. The corresponding "Form" element and auxiliary structure
* elements must be created by the user.
* The nesting of Part, Div, Caption, P and Form follows the recommendations
* in the document "Tagged PDF Best Practice Guide".
*
* Required software: PDFlib/PDFlib+PDI/PPS 10
* Required data: font file
*/
package com.pdflib.cookbook.pdflib.pdfua;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class form_fields_pdfua1 {
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 = "Tagged form fields";
try {
int x1 = 50, x2 = 150, y = 750;
int fieldheight=24, fieldwidth=200, fieldsmall=12;
int printaction, id_Part, id_Div, id_Div2;
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_pdfua1.pdf",
"pdfua=PDF/UA-1 lang=en tag={tagname=Document Title={" + title + "}}") ;
p.set_info("Creator", "PDFlib Cookbook");
p.set_info("Title", title);
/* Automatically create spaces between chunks of text */
p.set_option("autospace=true");
p.begin_page_ext(0, 0,
"width=a4.width height=a4.height taborder=structure");
id_Part = p.begin_item("Part", "");
font = p.load_font("NotoSerif-Regular", "winansi",
"simplefont nosubsetting");
p.setfont(font, 24.0);
p.fit_textline("Kraxi paper planes order form",
x1, y, "tag={tagname=H1}");
p.create_bookmark("Kraxi paper planes order form", "");
p.setfont(font, 12.0);
/* =================== Text field ======================== */
id_Div = p.begin_item("Div", "");
y -= 3 * fieldheight;
labeltext = "Enter name:";
p.fit_textline(labeltext, x1, y + 0.5* fieldheight, "tag={tagname=Caption tag={tagname=P}}");
optlist = "tag={tagname=P tag={tagname=Form}} tooltip={" + labeltext + "} " +
"bordercolor={gray 0} font=" + font;
p.create_field(x2, y, x2+fieldwidth, y+fieldheight,
"name", "textfield", optlist);
p.end_item(id_Div);
/* =================== Combo box ======================== */
id_Div = p.begin_item("Div", "");
y -= 3 * fieldheight;
labeltext = "Select size:";
p.fit_textline(labeltext, x1, y + 0.5* fieldheight, "tag={tagname=Caption tag={tagname=P}}");
/* 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 = "tag={tagname=P tag={tagname=Form}} tooltip={" + labeltext + "} " +
"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);
p.end_item(id_Div);
/* =================== List box ======================== */
id_Div = p.begin_item("Div", "");
y -= 4 * fieldheight;
labeltext = "Select model:";
p.fit_textline(labeltext, x1, y, "tag={tagname=Caption tag={tagname=P}}");
/* 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 first item (currentvalue=0).
*/
optlist = "tag={tagname=P tag={tagname=Form}} tooltip={" + labeltext + "} " +
"font=" + font + " fontsize=12 backgroundcolor={gray 0.9} " +
"bordercolor={gray 0.7} itemnamelist={0 1 2 3} currentvalue=0 "+
"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);
p.end_item(id_Div);
/* =================== Check boxes ======================== */
id_Div = p.begin_item("Div", "");
y -= 2 * fieldheight;
labeltext = "Select extras:";
p.fit_textline(labeltext, x1, y, "tag={tagname=Caption tag={tagname=P}}");
/* The nested Div element encloses the check boxes */
id_Div2 = p.begin_item("Div", "");
/* 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, "tag={tagname=Caption tag={tagname=P}}");
p.create_field(x2, y, x2 + fieldsmall, y + fieldsmall,
labeltext, "checkbox",
optlist + " tag={tagname=P tag={tagname=Form}} tooltip={" + labeltext + "}");
y -= fieldheight;
labeltext = "rainbow colors";
p.fit_textline(labeltext, x2 + 2 * fieldsmall, y, "tag={tagname=Caption tag={tagname=P}}");
p.create_field(x2, y, x2 + fieldsmall, y + fieldsmall,
labeltext, "checkbox",
optlist + " tag={tagname=P tag={tagname=Form}} tooltip={" + labeltext + "}");
p.end_item(id_Div2);
p.end_item(id_Div);
/* =================== radio buttons ======================== */
id_Div = p.begin_item("Div", "");
y -= 3 * fieldheight;
labeltext = "Select color:";
p.fit_textline(labeltext, x1, y, "tag={tagname=Caption tag={tagname=P}}");
/* The nested Div element encloses the buttons */
id_Div2 = p.begin_item("Div", "");
/* First create a form field group called "colors" */
p.create_fieldgroup("colors",
"fieldtype=radiobutton tooltip={" + labeltext + "}");
/* 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}).
* The tooltip is always shared by the group.
*/
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, "tag={tagname=Caption tag={tagname=P}}");
p.create_field(x2, y, x2 + fieldsmall, y + fieldsmall,
"colors."+labeltext, "radiobutton",
optlist + " currentvalue={On} tag={tagname=P tag={tagname=Form}}");
y -= fieldheight;
labeltext = "yellow";
p.fit_textline(labeltext, x2 + 2 * fieldsmall, y, "tag={tagname=Caption tag={tagname=P}}");
p.create_field(x2, y, x2 + fieldsmall, y + fieldsmall,
"colors."+labeltext, "radiobutton",
optlist + " tag={tagname=P tag={tagname=Form}}");
y -= fieldheight;
labeltext = "blue";
p.fit_textline(labeltext, x2 + 2 * fieldsmall, y, "tag={tagname=Caption tag={tagname=P}}");
p.create_field(x2, y, x2 + fieldsmall, y + fieldsmall,
"colors."+labeltext, "radiobutton",
optlist + " tag={tagname=P tag={tagname=Form}}");
p.end_item(id_Div2);
p.end_item(id_Div);
/* =================== Push button ======================== */
/* Create a print field of type "pushbutton". */
/* Create an action for executing the Acrobat command File/Print */
printaction = p.create_action("Named", "menuname=Print");
y -= 3*fieldheight;
optlist = "bordercolor={rgb 0.25 0 0.95} " +
"backgroundcolor={rgb 0.95 0.95 1} " +
"fillcolor={rgb 0.25 0 0.95} font=" + font + " fontsize=14";
labeltext = "Print form";
/* For the Div-->Caption-->P-->Form nesting we use quadruple abbreviated tagging */
p.create_field(x2, y, x2 + 0.5*fieldwidth, y + fieldheight,
"print", "pushbutton",
optlist + " caption={" + labeltext + "} " +
"action={up " + printaction + "} " +
"tag={tagname=Div tag={tagname=Caption tag={tagname=P tag={tagname=Form}}}} tooltip={" + labeltext + "}");
p.end_item(id_Part);
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);
}
}
}