• 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 or applet 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 (new URL(“http://www.mysite.com/content.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());
 
  // field value and options
  if (field instanceof TextField) {
    System.out.println("Value " + ((TextField) field).getValue());
    System.out.println("Default Value " + ((TextField) field).getDefaultValue());
  }
 
  else if (field instanceof RadioButtonGroupField) {
    System.out.println("Value " + ((RadioButtonGroupField) field).getValue());
    System.out.println("Default Value " + ((RadioButtonGroupField) field).getDefaultValue());
  }
 
  else if (field instanceof CheckBoxField) {
    System.out.println("Value " + ((CheckBoxField) field).getValue());
    System.out.println("Default Value " + ((CheckBoxField) field).getDefaultValue());
  }
 
  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());
  }
 
  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());
  }
 
  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 as FDF (exportFDFFile is a Java File Object)
pdfFields.exportAsFDF(exportFDFFile.getAbsolutePath());

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 (outputPDFFile is a Java File Object)
pdfFields.saveDocument(outputPDFFile.getAbsoluteFileName());

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 FDF (importFDFFile is a Java File Object)
pdfFields.importFDF(importFDFFile.getAbsolutePath());
 
// save pdf document (outputPDFFile is a Java File Object)
pdfFields.saveDocument(outputPDFFile.getAbsoluteFileName());

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 (outputPDFFile is a Java File Object)
pdfFields.saveDocument(outputPDFFile.getAbsoluteFileName());

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

Only the jPDFFields.jar file is always required for deployment, the remaining jar files are used to work with specific features in some PDF documents:

jPDFFields.jar – This is the main jar file for the component, it is always required.

bcpkix-jdk14-154.jar and bcprov-jdk14-154.jar – These files contain the Bouncycastle security libraries, they are only used to verify digital signatures, when present.

cmaps.jar – This jar file contains CMaps, used to read and display character encodings used with CJK (Chinese, Japanese, Korean) content.

rhino.jar – This jar file contains the Rhino JavaScript engine, it is necessary to run JavaScript for form field calculation, validation and formatting.

Javadoc API

Source Code Samples