Foxit PDF SDK for Windows

How to create and manage forms with Foxit PDF SDK (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 Form::GetFieldCount and Form::GetField.
  • 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 Form::ImportFromXML; to export form data to an XML file, please use function Form::ExportToXML.
  • To retrieve form filler object, please use function Form::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

#include "include/common/fs_common.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/fs_pdfform.h"
using namespace foxit;
using namespace pdf;
using namespace interform;
...
// Assuming PDFDoc doc has been loaded.
bool hasForm = doc.HasForm();
if(hasForm)
 Form form(doc);
...

How to count form fields and get properties

#include "include/common/fs_common.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/interform/fs_pdfform.h"
using namespace foxit;
using namespace pdf;
using namespace interform;
...
// Assuming PDFDoc doc has been loaded.
Form form(doc);
int countFields = form. GetFieldCount(NULL);
for (int i = 0; i < nFieldCount; i++)
{
 Field field = form.GetField(i, filter);
 Field::Type type = field.GetType();
 WString org_alternateName = field.GetAlternateName();
 field.SetAlternateName(L"signature");
}

How to export form data from a PDF to XML file

#include "include/common/fs_common.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/interform/fs_pdfform.h"
using namespace foxit;
using namespace pdf;
using namespace interform;
... 
// Assuming PDFDoc doc has been loaded.
Form form(doc);
...
form.ExportToXML(XMLFilePath);
...

How to import form data from a XML file

#include "include/common/fs_common.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/interform/fs_pdfform.h"
using namespace foxit;
using namespace pdf;
using namespace interform;
... 
// Assuming PDFDoc doc has been loaded.
Form form(doc);
...
form.ImportFromXML(XMLFilePath);
...

Form Filler

Form filler is the most commonly used feature for users. Form filler allows applications to fill forms dynamically. The key point for applications to fill forms is to construct callback functions for PDF SDK to call. To fill a form, please construct a Filler object with current Form object or retrieve the Filler object by the function Form::GetFormFiller if such an object has been constructed. (There should be only one form filler object for an interactive form). For how to fill a form with form filler, you can refer to the view demo “view_demo” in the “examples\simple_demo” folder of the download package for Windows.

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 to.

Example:

How to add a text form field to a PDF

#include "include/common/fs_common.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/fs_pdfpage.h"
#include "include/pdf/interform/fs_pdfform.h"
using namespace foxit;
using namespace pdf;
using namespace interform;
... 
// Assuming PDFDoc doc has been loaded.
// Assuming PDFPage page has been loaded and parsed.
// Add text field
Control control = form.AddControl(page, L"Text Field0", Field::e_TypeTextField, RectF(50, 600, 90, 640));
control.GetField().SetValue(L"3");
// Update text field's appearance.
control.GetWidget().ResetAppearanceStream();
Control control1 = form.AddControl(page, L"Text Field1", Field::e_TypeTextField, RectF(100, 600, 140, 640));
control1.GetField().SetValue(L"123");
// Update text field's appearance.
control1.GetWidget().ResetAppearanceStream();
...

How to remove a text form field from a PDF

#include "include/common/fs_common.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/fs_pdfpage.h"
#include "include/pdf/interform/fs_pdfform.h"
using namespace foxit;
using namespace pdf;
using namespace interform;
... 
// Assuming PDFDoc doc has been loaded.
Form form(doc);
const wchar_t* filter = L"text1";
int countFields = form.GetFieldCount(NULL);
for (int i = 0; i < countFields; i++)
{
 Field field = form.GetField(i, filter);
 if (field.GetType() == Field::e_TypeTextField) {
 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: