PDFlib Cookbook

cookbook

pdfua/tagged_form_fields

Download Java Code      Switch to PHP Code      Show Output PDF

/* $Id: tagged_form_fields.java,v 1.4 2014/01/30 14:05:28 rjs Exp $
 * 
 * Create accessible form field according to PDF/UA
 * Each call to create_field() generates the required OBJR structure element
 * automatically; the corresponding "Form" element must be created by the
 * user.
 * 
 * Required software: PDFlib/PDFlib+PDI/PPS 9
 * Required data: font file
 */

package com.pdflib.cookbook.pdflib.pdfua;

import com.pdflib.pdflib;
import com.pdflib.PDFlibException;

public class tagged_form_fields {
	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_P;

			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("tagged_form_fields.pdf",
					"pdfua=PDF/UA-1 lang=en " +
							"tag={tagname=Document Title={" + title + "}}") ;

			p.set_info("Creator", "PDFlib Cookbook");
			p.set_info("Title", title + " $Revision: 1.4 $");

			/* 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");

			font = p.load_font("GenR102", "pdfdoc", "embedding 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);

			id_P = p.begin_item("P", "");


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

			optlist = "tag={tagname=Form} tooltip={First name} " +
					"bordercolor={gray 0} font=" + font;

			p.create_field(x2, y, x2+fieldwidth, y+fieldheight,
					"firstname", "textfield", optlist);


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

			p.fit_textline("Select size:", 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=4).
			 * Allow the user to change an item (editable=true)
			 */

			optlist = "tag={tagname=Form} tooltip={Select size} " + 
					"font=" + font + " fontsize=12 backgroundcolor={gray 0.9} " +
					"bordercolor={gray 0.7} itemnamelist={0 1 2 3 4} " +
					"currentvalue=4 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;

			p.fit_textline("Select model:", 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 first item (currentvalue=0).
			 */
			optlist = "tag={tagname=Form} tooltip={Select model} " + 
					"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,
					"plane", "listbox", optlist);


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

			p.fit_textline("Select extras:", 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}";

			p.create_field(x2, y, x2 + fieldsmall, y + fieldsmall,
					"glossy", "checkbox",
					optlist + " tag={tagname=Form} tooltip={glossy paper}");

			p.fit_textline("Glossy paper", x2 + 2 * fieldsmall, y, "");


			y -= fieldheight;
			p.create_field(x2, y, x2 + fieldsmall, y + fieldsmall,
					"rainbow", "checkbox",
					optlist + " tag={tagname=Form} tooltip={rainbow colors}");

			p.fit_textline("Rainbow colors", x2 + 2 * fieldsmall, y, "");


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

			p.fit_textline("Select color:", x1, y, "");

			/* First, create a form field group called "colors" */
			p.create_fieldgroup("colors",
					"fieldtype=radiobutton " +
					"tooltip={Select a color for the paper plane}");

			/* 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}";

			p.create_field(x2, y, x2 + fieldsmall, y + fieldsmall,
					"colors.standard", "radiobutton",
					optlist + " currentvalue={On} tag={tagname=Form}");

			p.fit_textline("Standard", x2 + 2 * fieldsmall, y, "");
			y -= fieldheight;

			p.create_field(x2, y, x2 + fieldsmall, y + fieldsmall,
					"colors.yellow", "radiobutton",
					optlist + " tag={tagname=Form}");

			p.fit_textline("Yellow", x2 + 2 * fieldsmall, y, "");
			y -= fieldheight;

			p.create_field(x2, y, x2 + fieldsmall, y + fieldsmall,
					"colors.blue", "radiobutton",
					optlist + " tag={tagname=Form}");

			p.fit_textline("Blue", x2 + 2 * fieldsmall, y, "");


			/* =================== 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";

			p.create_field(x2, y, x2 + 0.5*fieldwidth, y + fieldheight,
					"print", "pushbutton",
					optlist + " caption={Print form} action={up " + printaction + 
					"} tag={tagname=Form} tooltip={Print the document}");

			p.end_item(id_P);
			p.end_page_ext("");

			p.end_document("");
		}
		catch (PDFlibException e) {
			System.err.print("PDFlib exception occurred:\n");
			System.err.print("[" + e.get_errnum() + "] " + e.get_apiname()
					+ ": " + e.get_errmsg() + "\n");
			exitcode = 1;
		}
		catch (Exception e) {
			System.err.println(e.getMessage());
			exitcode = 1;
		}
		finally {
			if (p != null) {
				p.delete();
			}
			System.exit(exitcode);
		}
	}
}