How to create and manage forms with Foxit PDF SDK (.NET)
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:
Contents
- How to load forms in a PDF
- How to count form fields and get properties
- How to export form data from a PDF to a XML file
- How to import form data from a XML file
- How to get and set the properties of form fields
- Form Filler
- Form Design
- How to add a text form field to a PDF
- How to remove a text form field from a PDF
How to load forms in a PDF
using foxit; using foxit.common; using foxit.common.fxcrt; using foxit.pdf; using foxit.pdf.interform; using foxit.pdf.annots; using foxit.pdf.actions; ... // 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
using foxit; using foxit.common; using foxit.common.fxcrt; using foxit.pdf; using foxit.pdf.interform; using foxit.pdf.annots; using foxit.pdf.actions; ... // Assuming PDFDoc doc has been loaded. Boolean hasForm = doc.HasForm(); If(hasForm) Form form = new Form(doc); int count = form.GetFieldCount(""); for (int i = 0; i < count; i++) { Field field = form.GetField(i, ""); ... }
How to export form data from a PDF to a XML file
using foxit; using foxit.common; using foxit.common.fxcrt; using foxit.pdf; using foxit.pdf.interform; using foxit.pdf.annots; using foxit.pdf.actions; ... // Assuming PDFDoc doc has been loaded. Boolean hasForm = doc.HasForm(); If(hasForm) Form form = new Form(doc); ... form.ExportToXML(XMLFilePath); ...
How to import form data from a XML file
using foxit; using foxit.common; using foxit.common.fxcrt; using foxit.pdf; using foxit.pdf.interform; using foxit.pdf.annots; using foxit.pdf.actions; ... // Assuming PDFDoc doc has been loaded. Boolean hasForm = doc.HasForm(); If(hasForm) Form form = new Form(doc); ... form.ImportFromXML(XMLFilePath); ...
How to get and set the properties of form fields
using foxit; using foxit.common; using foxit.common.fxcrt; using foxit.pdf; using foxit.pdf.interform; using foxit.pdf.annots; using foxit.pdf.actions; ... // Assuming PDFDoc doc has been loaded. Boolean hasForm = doc.HasForm(); If(hasForm) Form form = new Form(doc); Field field = form.GetField(0, "Text Field0"); field.GetAlignment(); field.GetAlternateName(); field.SetAlignment(Alignment.e_AlignmentLeft); field.SetValue("3"); ...
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 some callback functions for PDF SDK to call. To fill the form, please construct a Filler object by current Form object or retrieve the Filler object by the function Form.GetFormFiller if such 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.
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
using foxit; using foxit.common; using foxit.common.fxcrt; using foxit.pdf; using foxit.pdf.interform; using foxit.pdf.annots; using foxit.pdf.actions; ... // Assuming PDFDoc doc has been loaded. Control control = form.AddControl(page, "Text Field0", Field.Type.e_TypeTextField, new RectF(50f, 600f, 90f, 640f)) ...
How to remove a text form field from a PDF
using foxit; using foxit.common; using foxit.common.fxcrt; using foxit.pdf; using foxit.pdf.interform; using foxit.pdf.annots; using foxit.pdf.actions; ... // Assuming PDFDoc doc has been loaded. Field field = form.GetField(0, "Text Field0"); form.RemoveField(field);
Updated on April 29, 2019