• jPDFFields

    jPDFFields

    jPDFFields

    Work with interactive PDF forms to import, export, set, and get field values.

    TRY LIVE DEMO

jPDFFields Developer Guide

jPDFFields Developer Guide

Contents

javalogo

Introduction
Getting Started
Extracting Data from Fields Programmatically
Exporting to FDF or XFDF
Filling Fields with Data Programmatically
Importing Data from FDF or XFDF
Flattening Form Fields
Getting Basic Document Information

Distribution and JAR files
Javadoc API

Source Code Samples

Introduction

jPDFFields is a Java library that integrates seamlessly into your application to work with Adobe PDF interactive forms also called acroforms. jPDFFields provides the following functions to work with forms:

  • Load PDF interactive forms from files, network drives, URLs or input streams
  • Get the list of all fields contained in the interactive form and work with them programmatically
  • Extract data from form fields programmatically
  • Export data from form fields in FDF or XFDF formats (files or output streams)
  • Fill in form fields programmatically with data
  • Import data from FDF or XFDF (files or input streams) into form fields
  • Save the updated PDF form

Like all of our libraries, jPDFFields is built on top of Qoppa’s proprietary format and doesn’t require any third party programs or drivers.

Getting Started

The starting point for using jPDFFields is the com.qoppa.pdfFields.PDFFields. This class is used to load documents into an application, work with form fields and then save the document. The class provides three constructors to load PDF files from the file system, a URL or an InputStream. All constructors take an additional parameter, an object that implements IPasswordHandler, that will be queried if the PDF file has requires a password to open. For PDF files that are not encrypted, this second parameter can be null:

PDFFields pdfFields = new PDFFields ("C:/form01.pdf", null);

Extracting Data from Fields Programmatically

Once a PDFFields object has been created, the host application simply needs to call the getFields method to get the list of fields in the loaded PDF document. It is then possible to work with each field individually based on its field type to extract information and data:

Vector fields = pdfFields.getFieldList();
 
for (int count = 0; count < fields.size(); ++count) {
  FormField field = (FormField) fields.get(count);
  // field type description
  System.out.println("Field Type " + field.getFieldTypeDesc());
 
  // field name
  System.out.println("Field Name " + field.getFieldName());
 
  // text field
  if (field instanceof TextField) {
    System.out.println("Value " + ((TextField) field).getValue());
    System.out.println("Default Value " + ((TextField) field).getDefaultValue());
  }
  // radio button field
  else if (field instanceof RadioButtonGroupField) {
    System.out.println("Value " + ((RadioButtonGroupField) field).getValue());
    System.out.println("Default Value " + ((RadioButtonGroupField) field).getDefaultValue());
  }
  // check box field
  else if (field instanceof CheckBoxField) {
    System.out.println("Value " + ((CheckBoxField) field).getValue());
    System.out.println("Default Value " + ((CheckBoxField) field).getDefaultValue());
  }
  // combo box field
  else if (field instanceof ComboField) {
    System.out.println("Value " +((ComboField) field).getValue());
    System.out.println("Options " +((ComboField) field).getExportOptions());
    System.out.println("Default Value " + ((ComboField) field).getDefaultValue());
  }
  // list field
  else if (field instanceof ListField) {
    System.out.println("Value " + ((ListField) field).getValues());
    System.out.println("Options " + ((ListField) field).getExportOptions());
    System.out.println("Default Values " +((ListField) field).getDefaultValue());
  }
  // signature field
  else if (field instanceof SignatureField) {
    System.out.println("Signature Name " + ((SignatureField) field).getSignName());
    System.out.println("Signature Date " + ((SignatureField) field).getSignDateTime());
  }
}

Exporting to FDF or XFDF

To export fields data to an FDF or XFDF file (or output stream), simply call the exportAsFDF or exportAsXFDF method in the PDFFields class. Both methods are available with a File argument or an Java OutputStream argument.

// export fields data to FDF file
pdfFields.exportAsFDF("C:/form01.fdf");

Filling Fields with Data Programmatically

To fill in the fields with data, you can use the getField method to get a field by field name and then use the setValue method to set the data. To set values in combo boxes and list fields, use values from the field’s export options and not the display options. To save the pdf document, you need to call the save method.

((TextField) pdfFields.getField("LastName")).setValue("Smith");
 
((TextField) pdfFields.getField("FirstName")).setValue("Maggie");
 
// Married check box's value is "M" (box is checked)
((CheckBoxField) pdfFields.getField("Married")).setValue("M");
 
// Gender radio button's value is "Female"
 
((RadioButtonGroupField) pdfFields.getField("Gender")).setValue("Female");
 
((ComboField) pdfFields.getField( "Country")).setValue("US");
 
// List fields' values are Kroger, Publix
((ListField) pdfFields.getField( "Supermarkets")).setValue(new Vector(new String[]{ "Kroger", "Publix"}));
 
// save pdf document 
pdfFields.saveDocument("C:/form01.pdf");

Importing Data from FDF or XFDF

To import data into fields from an FDF or XFDF file (or output stream), simply call the importFDF or importXFDF method in the PDFFields class. Both methods are available with a File argument or an Java InputStream argument. If you need to save the pdf document, you need to call the save method explicitly.

// import fields data from FDF file
pdfFields.importFDF("C:/form01.fdf");
 
// save pdf document 
pdfFields.saveDocument("C:/form01.pdf");

Flattening Fields

To flatten form fields in a PDF form, simply call the flattenFields method in the PDFFields class. This will paint the content of the fields directly into the page and remove the fields themselves from the PDF document. This method takes a boolean as a parameter to indicate whether push buttons should be painted or not. Most of the time, it doesn’t make sense to paint a push button once its flattened since it has no action anymore.

// flatten form fields - push buttons will be omitted when painting
pdfFields.flattenFields(false);
 
// save pdf document
pdfFields.saveDocument("C:/form01.pdf");

Getting / Setting Information about the PDF Document

To get basic information about the loaded PDF document, you need to get the DocumentInfo class accessible through PDFFields.getDocumentInfo. From this class, you can get and set information about the document such as title, author, subject, keywords, etc…

System.out.println(pdfFields.getDocumentInfo().getTitle());
System.out.println(pdfFields.getDocumentInfo().getAuthor());
System.out.println(pdfFields.getDocumentInfo().getKeywords());

JavaScript Support

Read the entry in our knowlege base regarding JavaScript Support.

Distribution and JAR Files

Required and optional jar files for jPDFFields can be found on the jPDFFields Download page.

Javadoc API

Source Code Samples