How to use XFA Forms in Foxit PDF SDK
Contents
What are XFA forms?
PDF currently supports two different forms for integrating data – Acroforms and XFA forms. Acroforms are the original PDF-based fillable forms, based on the PDF architecture. XFA (XML Forms Architecture) are XML-based forms, wrapped inside a PDF. The XML Forms Architecture provides a template-based grammar and a set of processing rules that allow businesses to build interactive forms. At its simplest, a template-based grammar defines fields in which a user provides data.
Foxit PDF SDK provides APIs to render the XFA form, fill the form in, and export or import the form’s data.
Note: Foxit PDF SDK provides foxit::addon::xfa::DocProviderCallback and foxit::addon::xfa::AppProviderCallback to represent callback objects as an XFA document provider and an XFA application provider. All the functions in those classes are used as callback functions. Pure virtual functions should be implemented by the user.
How to load a XFA Doc and represent an Interactive XFA form
Example:
(C++)
#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"
#include "../../../include/addon/xfa/fs_xfa.h"
using namespace std;
using namespace foxit;
using namespace foxit::common;
using foxit::common::Library;
using namespace pdf;
using namespace interform;
using namespace foxit::addon::xfa;
...
CFS_XFAAppHandler* pXFAAppHandler = new CFS_XFAAppHandler(); // implement from foxit::addon::xfa::AppProviderCallback
Library::RegisterXFAAppProviderCallback(pXFAAppHandler);
WString input_file = input_path + L"xfa_dynamic.pdf";
PDFDoc doc(input_file);
ErrorCode error_code = doc.Load();
if (error_code != foxit::e_ErrSuccess) {
printf("The Doc [%s] Error: %d\n", (const char*)String::FromUnicode(input_file), error_code);
return 1;
}
CFS_XFADocHandler* pXFADocHandler = new CFS_XFADocHandler(); // implement from foxit::addon::xfa::DocProviderCallback
XFADoc xfa_doc(doc, pXFADocHandler);
xfa_doc.StartLoad(NULL);
...
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
using foxit.addon;
using foxit.addon.xfa;
...
CFS_XFAAppHandler pXFAAppHandler = new CFS_XFAAppHandler(); // implement from AppProviderCallback
Library.RegisterXFAAppProviderCallback(pXFAAppHandler);
string input_file = input_path + "xfa_dynamic.pdf";
using (PDFDoc doc = new PDFDoc(input_file))
{
error_code = doc.Load(null);
if (error_code != ErrorCode.e_ErrSuccess)
{
Console.WriteLine("The PDFDoc [{0}] Error: {1}\n", input_file, error_code);
Library.Release();
return;
}
CFS_XFADocHandler pXFADocHandler = new CFS_XFADocHandler(); //implement from DocProviderCallback
using (XFADoc xfa_doc = new XFADoc(doc, pXFADocHandler))
{
...
}
}
Java
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.PDFException;
import com.foxit.sdk.common.Library;
import com.foxit.sdk.common.WStringArray;
import com.foxit.sdk.common.Constants;
import com.foxit.sdk.addon.xfa.*;
import com.foxit.sdk.addon.xfa.XFADoc.*;
import com.foxit.sdk.common.fxcrt.*;
import com.foxit.sdk.common.WStringArray;
...
try {
XFAAppHandler xfa_app = new XFAAppHandler(); //create addon::xfa::AppProviderCallback handler
Library.registerXFAAppProviderCallback(xfa_app); // register it in application
String input_file = input_path + "foxit.pdf";
PDFDoc pdf_doc = new PDFDoc(input_file); //load pdf document
error_code = pdf_doc.load(null);
if (error_code != e_ErrSuccess) {
System.out.println("The Doc " + input_file + " Error: " + error_code);
return;
}
XFADocHandler xfa_dochandler = new XFADocHandler();// create foxit::addon::xfa::DocProviderCallback handler
XFADoc xfa_doc = new XFADoc(pdf_doc,xfa_dochandler); //load xfa document from pdf document
xfa_doc.startLoad(null);
} catch (PDFException e) {
System.out.println(e.getMessage());
return;
OC
#include "FSPDFObjC.h"
...
CFS_XFAAppHandler* pXFAAppHandler = [CFS_XFAAppHandler alloc]; //implement from FSAppProviderCallback
[FSLibrary registerXFAAppProviderCallback:pXFAAppHandler];
FSPDFDoc *doc = [[FSPDFDoc alloc] initWithPath:input_file];
errorCode = [doc load:@""];
if (errorCode != FSErrSuccess) {
NSLog(@"The Doc [%@] Error: %ld\n", input_file, errorCode);
return -1;
}
CFS_XFADocHandler* pXFADocHandler = [CFS_XFADocHandler alloc];
FSXFADoc* xfa_doc = [[FSXFADoc alloc] initWithDocument:doc xfa_doc_provider_handler:pXFADocHandler]; //implement from FSDocProviderCallback
[xfa_doc startLoad:nil];
...
How to export and import XFA form data
Example:
(C++)
...
xfa_doc.ExportData(output_directory + L"xfa_form.xml", XFADoc::e_ExportDataTypeXML);
xfa_doc.ResetForm();
doc.SaveAs(output_directory + L"xfa_dynamic_resetform.pdf");
xfa_doc.ImportData(output_directory + L"xfa_form.xml");
doc.SaveAs(output_directory + L"xfa_dynamic_importdata.pdf");
...
C#
...
xfa_doc.ExportData(output_path + "xfa_form.xml", XFADoc.ExportDataType.e_ExportDataTypeXML);
xfa_doc.ResetForm();
doc.SaveAs(output_path + "xfa_dynamic_resetform.pdf", (int)foxit.pdf.PDFDoc.SaveFlags.e_SaveFlagNormal);
xfa_doc.ImportData(output_path + "xfa_form.xml");
doc.SaveAs(output_path + "xfa_dynamic_importdata.pdf", (int)foxit.pdf.PDFDoc.SaveFlags.e_SaveFlagNormal);
...
Java
...
String output_xml = output_path + "xfa_form.xml";
xfa_doc.exportData(output_xml, XFADoc.ExportDataType.e_ExportDataTypeXML);
xfa_doc.resetForm();
doc.saveAs(output_path + "xfa_dynamic_resetform.pdf", PDFDoc.e_SaveFlagNormal);
xfa_doc.importData(output_xml);
doc.saveAs(output_path + "xfa_dynamic_importdata.pdf", PDFDoc.e_SaveFlagNormal);
...
OC
...
[xfa_doc exportData:[output_directory stringByAppendingString:@"xfa_form.xml"] export_type:FSXFADocExportDataTypeXML];
[xfa_doc resetForm];
[doc saveAs:[output_directory stringByAppendingString:@"xfa_dynamic_resetform.pdf"] save_flags:FSPDFDocSaveFlagNormal];
[xfa_doc importData:[output_directory stringByAppendingString:@"xfa_form.xml"]];
[doc saveAs:[output_directory stringByAppendingString:@"xfa_dynamic_importdata.pdf"] save_flags:FSPDFDocSaveFlagNormal];
...
Updated on February 18, 2019