Foxit PDF SDK for Mac

How to create and manage forms with Foxit PDF SDK (Objective-C)

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 FSForm::getFieldCount and FSForm::getField.
  • To retrieve form controls from a PDF page, please use functions FSForm::getControlCount and FSForm::getControl.
  • To import form data from an XML file, please use function FSForm::importFromXML; to export form data to an XML file, please use function FSForm::exportToXML.
  • To retrieve form filler object, please use function FSForm::getFormFiller.

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

Example:

How to load forms in a PDF

#include "FSPDFObjC.h"
...
// Assuming FSPDFDoc doc has been loaded.
BOOL hasform = [doc hasForm];
if(hasform)
  FSForm *form = [[FSForm alloc] initWithDocument:doc];
...

How to count form fields and get properties

#include "FSPDFObjC.h"
...
// Assuming FSPDFDoc doc has been loaded.
BOOL hasform = [doc hasForm];
if(hasform) {
    FSForm *form = [[FSForm alloc] initWithDocument:doc];
    int count = [form getFieldCount:@""];
    for (int i = 0; i < count; i++) {
        FSField* field = [form getField:i filter:@""];
        FSFieldType field_type = [field getType];
        NSString* name = [field getName];
    }
}

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

#include "FSPDFObjC.h"
...
// Assuming FSPDFDoc doc has been loaded.
...
FSForm *form = [[FSForm alloc] initWithDocument:doc];
BOOL is_success = [field exportToXML:@"test.xml"];
...
1.9.4        How to import form data from a XML file
#include "FSPDFObjC.h"
...
FSForm *form = [[FSForm alloc] initWithDocument:doc];
BOOL is_success = [field importFromXML:@"test.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

#include "FSPDFObjC.h"
...
// Add test field
FSControl *control = [form addControl:page field_name:@"Text Field0" field_type:FSFieldTypeTextField rect:[[FSRectF alloc] initWithLeft1:50.0 bottom1:600 right1:90 top1:640]];
[control getField].value = @"3";
// Update text field's appearance.
[[control getWidget] resetAppearanceStream];
...

How to remove a text form field from a PDF

#include "FSPDFObjC.h"
...
// Assuming FSPDFDoc doc has been loaded.
...
FSForm *form = [[FSForm alloc] initWithDocument:doc];
int count = [form getFieldCount:@""];
for (int i = 0; i < count; i++) {
    FSField* field = [form getField:i filter:@""];
    FSFieldType field_type = [field getType];
    if (FSFieldTypeTextField == field_type)
        [field 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: