Forms
Foxit PDF SDK for Android provides AcroForm read, fill, edit, and data import/export through the Core SDK. Forms consist of fields and controls, supporting text fields, checkboxes, radio buttons, combo boxes, list boxes, buttons, and signature fields.
Built-in form filling in UI Extensions
If you use the full reader (UI Extensions), form filling is built in (FormFillerModule); users can tap and fill fields in the document. The form navigation module (FormNavigationModule) supports quick movement between fields. No extra code is required.
Core Classes
| Class | Description |
|---|---|
Form | Form object; manages fields and controls; supports data import/export |
Field | Form field; defines type, name, value, and properties |
Control | Form control; visual representation on the page (Widget annotation) |
Filler | Form filler; handles form interaction logic |
Field Types
| Constant | Value | Description |
|---|---|---|
Field.e_TypePushButton | 1 | Push button |
Field.e_TypeCheckBox | 2 | Checkbox |
Field.e_TypeRadioButton | 3 | Radio button |
Field.e_TypeComboBox | 4 | Combo box |
Field.e_TypeListBox | 5 | List box |
Field.e_TypeTextField | 6 | Text field |
Field.e_TypeSignature | 7 | Signature field |
Common Field Flags
| Constant | Description |
|---|---|
e_FlagReadOnly | Read-only |
e_FlagRequired | Required |
e_FlagNoExport | Do not export |
e_FlagTextMultiline | Multiline text |
e_FlagTextPassword | Password input |
e_FlagTextComb | Comb spacing |
e_FlagComboEdit | Editable combo box |
e_FlagChoiceMultiSelect | Multi-select list |
Example: Get Form Fields and Controls
java
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.pdf.interform.Form;
import com.foxit.sdk.pdf.interform.Field;
import com.foxit.sdk.pdf.interform.Control;
PDFDoc doc = new PDFDoc("path/to/Sample.pdf");
doc.load(null);
if (!doc.hasForm()) return;
Form form = new Form(doc);
// Iterate all fields (null filter = no filter)
int fieldCount = form.getFieldCount(null);
for (int i = 0; i < fieldCount; i++) {
Field field = form.getField(i, null);
String name = field.getName();
int type = field.getType();
String value = field.getValue();
}
// Get all controls on a page
PDFPage page = doc.getPage(0);
int controlCount = form.getControlCount(page);
for (int i = 0; i < controlCount; i++) {
Control control = form.getControl(page, i);
Field field = control.getField();
}Filter fields by type
getFieldCount(String filter) and getField(int index, String filter) support type name filters such as "PushButton", "CheckBox", "RadioButton", "ComboBox", "ListBox", "TextField", "Signature". Pass null for all fields.
Example: Fill Form Fields
java
Form form = new Form(doc);
// Fill text fields
int count = form.getFieldCount("TextField");
for (int i = 0; i < count; i++) {
Field field = form.getField(i, "TextField");
if ("name".equals(field.getName())) {
field.setValue("张三");
}
}
// Set checkbox checked state
int cbCount = form.getFieldCount("CheckBox");
for (int i = 0; i < cbCount; i++) {
Field field = form.getField(i, "CheckBox");
Control control = field.getControl(0);
if (control != null && !control.isEmpty()) {
control.setChecked(true);
}
}
doc.saveAs("path/to/filled.pdf", PDFDoc.e_SaveFlagNormal);Data Import and Export
XML Format
java
Form form = new Form(doc);
// Export all fields to XML
form.exportToXML("/sdcard/form_data.xml");
// Import from XML
form.importFromXML("/sdcard/form_data.xml");FDF / XFDF Format
java
import com.foxit.sdk.fdf.FDFDoc;
import com.foxit.sdk.common.Range;
// Export form data to FDF
FDFDoc fdfDoc = new FDFDoc(FDFDoc.e_FDF);
doc.exportToFDF(fdfDoc, PDFDoc.e_AnnotForm, new Range());
fdfDoc.saveAs("/sdcard/form_data.fdf");
// Import form data from FDF
FDFDoc importFdf = new FDFDoc("/sdcard/form_data.fdf");
doc.importFromFDF(importFdf, PDFDoc.e_AnnotForm, new Range());Form also supports CSV, HTML, and plain text export. See the API Reference for details.
API Reference
See the API Reference for full Form, Field, and Control documentation.