Foxit PDF SDK for Linux Foxit PDF SDK for Mac Foxit PDF SDK for Windows

How to create and manage forms with Foxit PDF SDK (Java)

PDF currently supports two different forms for gathering information interactively from the user – AcroForms and XFA forms. AcroForms are the original PDF-based fillable forms, based on PDF architecture. Foxit PDF SDK provides APIs to view and edit form field programmatically. Form fields are commonly used in PDF documents to gather data. The Form class offers functions to retrieve form fields or form controls, import/export form data and other features, for example:

  • To retrieve form fields, please use functions getControlCount and Form.getControl.
  • To retrieve form controls from a PDF page, please use functions Form::GetControlCount and Form::GetControl.
  • To import form data from an XML file, please use function importFromXML; to export form data to an XML file, please use function Form.exportToXML.
  • To retrieve form filler object, please use function getFormFiller.

To import form data from a FDF/XFDF file or export such data to a FDF/XFDF file, please refer to functions pdf.PDFDoc.importFromFDF and pdf.PDFDoc.exportToFDF.

Example:

How to load forms in a PDF

import com.foxit.sdk.pdf.interform.Form;
...
// Assuming PDFDoc doc has been loaded.
...
Boolean hasForm = doc.hasForm();
if(hasForm)
Form form = new Form(doc);
...

How to count form fields and get properties

import com.foxit.sdk.pdf.interform.Form;
import com.foxit.sdk.pdf.interform.Control;
import com.foxit.sdk.pdf.interform.Field;
...
// Assuming PDFDoc doc has been loaded.
...
Form form = new Form(doc);
String filter = "";
int nControlCount = form.getFieldCount(filter);
for (int i=0; i

How to export form data from a PDF to a XML file

import com.foxit.sdk.pdf.interform.Form;
...
// Assuming PDFDoc doc has been loaded.
...
Form form = new Form(doc);
form.exportToXML("form.xml");
...

How to import form data from an XML file

import com.foxit.sdk.pdf.interform.Form;
...
Form form = new Form(doc);
form.importFromXML("form.xml");
...

Form Design

Fillable PDF forms (AcroForm) are especially convenient for preparation of various applications, such as taxes and other government forms. Form design provides APIs to add or remove form fields (Acroform) to or from a PDF file. Designing a form from scratch allows developers to create the exact content and layout of the form they want.

Example:

How to add a text form field to a PDF

import com.foxit.sdk.pdf.interform.Form;
import com.foxit.sdk.pdf.interform.Control;
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.common.fxcrt.RectF;
...
// Assuming PDFDoc doc has been loaded.
...
Form form = new Form(doc);
Control control = form.addControl(page, "Text Field0", Field.e_TypeTextField, new RectF(50, 600, 90, 640));
control.getField().setValue("3");
// Update text field's appearance.
control.getWidget().resetAppearanceStream();
...

How to remove a text form field from a PDF

import com.foxit.sdk.pdf.interform.Form;
import com.foxit.sdk.pdf.interform.Control;
import com.foxit.sdk.pdf.interform.Field;
...
// Assuming PDFDoc doc has been loaded.
...
Form form = new Form(doc);
int nControlCount = form.getFieldCount("Text Field0");
if (nControlCount > 0)
{
Field field = form.getField(0, "Text Field0");
form.removeField(field);
}
...

Updated on April 29, 2019

Was this article helpful?
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: