Foxit PDF SDK

About fdf/xfdf/xml

Foxit PDF SDK, as an outstanding document processing tool, offers users abundant functionalities and high flexibility. Among them, the FDF, XFDF, and XML file formats play crucial roles in PDF processing. This article will provide you with a detailed introduction to these file formats, their definitions, uses, and their utilization within the Foxit SDK.

What is the FDF file?

FDF (Forms Data Format) is a file format used for processing interactive form data. It is responsible for storing form data, annotations, and page layout information within a PDF document. FDF can be used to submit form data to servers, receive responses, and integrate them into interactive forms. Additionally, FDF supports exporting form data as standalone files, facilitating storage, transmission, and reimporting into corresponding PDF interactive forms. Moreover, FDF files can define annotation containers, separating them from applicable PDF documents, enabling flexible annotation management.

What is the XFDF file?

XFDF (XML Forms Data Format) is an XML-based version of FDF with similar functionalities. It stores form data and annotations in XML format, offering higher readability and ease of handling. XFDF files can be easily exported and imported to manage form data storage and sharing conveniently. XFDF maintains compatibility with FDF, allowing data exchange in different environments, thus providing greater flexibility for form data processing.

What is the XML file?

XML (eXtensible Markup Language) is a universal markup language used for representing structured data. In the context of the Foxit PDF SDK, XML files are commonly used to describe the structure, content, and attributes of PDF documents. Through XML files, users can extensively configure and customize PDF documents to fulfill personalized document processing needs. The XML format enables users to define elements such as page layout, bookmarks, links, annotations, styles, and more, enabling comprehensive document customization.

Applications of FDF, XFDF, and XML

●  FDF 

  1. Dedicated to Form Data: The FDF file format is specifically designed for processing interactive form data, efficiently handling submission, integration, and storage of form data.
  2. Annotation Container Support: FDF files allow defining annotation containers, separating annotations from PDF documents for more flexible annotation management.
  3. User-Friendly: The FDF file format is relatively simple, making it easy to create, edit, and process, facilitating convenient data exchange and integration.

●  XFDF 

  1. Based on XML Markup: XFDF files use XML markup language, providing higher readability and intuitive handling for data exchange.
  2. Structured Data Exchange: XFDF files support rich data structuring, facilitating data import, export, and sharing.
  3. Flexibility and Compatibility: Building upon the FDF format, XFDF maintains a level of compatibility, enabling exchange across different environments, and also allows user-defined markup for personalized data descriptions.

●  XML 

  1. Versatility: XML file format is a universal markup language applicable for describing various types of data, serving a wide range of application domains.
  2. Structured Representation: XML files support highly structured data descriptions, enabling definition of complex document elements, attributes, and relationships to meet diverse processing needs.
  3. Extensibility: Users can easily extend XML file markup to achieve specific application scenarios, satisfying customized requirements.

In conclusion, FDF, XFDF, and XML file formats each possess unique advantages in document processing. Developers can choose the most suitable format based on their actual needs to achieve optimal results in data exchange, storage, and customization. Foxit PDF SDK provides robust support for these file formats, empowering developers to better address document processing challenges.

Application of FDF/XFDF/XML in Foxit PDF SDK

Foxit PDF SDK offers support for the FDF file format, making interactive form data processing simple and efficient:

●  Form Data Interaction: With Foxit SDK, you can easily create, read, and edit FDF files, achieving form data interaction and integration.
For example: Import form fields and annotations from an FDF/XFDF document.

This allows importing fields and annotations into a PDF document, as shown in the illustration:

Additionally, you can also load FDF/XFDF files using streams:

It also supports importing form fields in FDF template. The page associated with the FDF template will be inserted into the end of the document.

For more details, refer to the article: https://developers.foxit.com/developer-hub/document/how-to-import-a-template-based-fdf-to-pdf/.

●  Import and Export: Foxit SDK allows you to associate FDF files with PDF documents, enabling data import and export. You can export form data as FDF files for convenient storage, transmission, and backup, and subsequently reapply the data to the corresponding PDF forms through import operations.

For example:Export form fields and annotations to a FDF/XFDF document:

Export the form data to an XML file:

Import the form data from an XML file:

In addition, importing and exporting XML files to form fields also support streaming methods.

File Structure of FDF/XFDF/XML

●  FDF File Structure

  • Header: An FDF file begins with a header, usually containing version information and encoding indications. This section defines the FDF file’s format version and the character encoding used.
  • Start and End: FDF files use specific markers to indicate their start and end. The start marker is typically %FDF-1.2, indicating the FDF file’s version. The end marker is %%EOF, indicating the file’s end.
  • Dictionary Objects: FDF files contain one or more dictionary objects describing form fields and annotations. Each dictionary object is enclosed in angle brackets << and >> and contains a series of key-value pairs. These pairs define the field’s name, value, type, and other properties. For instance, a form field’s dictionary object might contain key-value pairs like /T (Field Name), /V (Field Value), /FT (Field Type), etc.
  • Annotation Descriptions: In FDF files, annotation descriptions define information related to annotations within the PDF document, such as annotation type, position, color, etc. These descriptions appear in the form of dictionary objects and can contain multiple key-value pairs to describe various annotation attributes.
  • Link Information: FDF files can also contain information related to links for creating and managing links within the PDF document.
  • Other Information: FDF files can include other information relevant to PDF document interaction and processing, such as form submission addresses, data export methods, etc.
    For example, a typical FDF format might look like:

●  XFDF File Structure:

  • Root Element: The root element of an XFDF file is typically <xfdf>.
  • Field Data: Each form field in the form is represented by one or more <field> elements, containing the field’s name (<fieldname>) and value (<value>).
  • Annotation Data: If the PDF file contains annotations such as comments or highlights, they can be represented using the <annots> element.
  • Page Data: Data can be associated with specific PDF pages using the <page> element.
  • Additional Data: XFDF files can also include other data related to PDF forms.

●  XML File Structure:

  • Tags and Elements: XML files use tags and elements to represent data. Tags are enclosed in angle brackets, and elements consist of tags, attributes, and text content.
  • Hierarchy: Elements in an XML file can be nested, creating a hierarchical structure. Parent elements can contain child elements, forming relationships between data.
  • Attributes: Elements can have attributes, providing key-value pairs with additional information about the element.
  • Text Content: Elements can contain text content, representing the actual data.
  • Comments: XML files can include comments for explanatory and descriptive purposes.

Code display of using fdf/xfdf/xml  function in various languages:

C++ language

#if defined(_WIN32) || defined(_WIN64)
#include<direct.h>
#else
#include <sys/stat.h>
#endif
 
#include "../../../include/common/fs_common.h"
#include "../../../include/pdf/fs_pdfdoc.h"
#include "../../../include/pdf/fs_pdfpage.h"
#include "../../../include/pdf/annots/fs_annot.h"
#include "../../../include/pdf/interform/fs_pdfform.h"
 
using namespace std;
using namespace foxit;
using namespace foxit::common;
using foxit::common::Library;
using namespace pdf;
using namespace annots;
using namespace actions;
 
static const char* sn = "VQv/htdRKu1rjhk90MTKF+/aCs9DfQRtBF4SNc0GwPlOX8ualGeTJg==";
static const char* key = "ezJvj18mtBp399sXJVWofHfLliq8pf9v7BMskv+zLerFJVWviGEfZ+kCs+nfwjfVTthVUzkIj1qr9k37wnS1f2YSW3jOA/0EfK806dB9bigN097uSthk6eeafc30XHVMSGerLpzYziDW9zL/1oMTZ80YX6Trs0BWaif611VZm/SJ4PkaRzN0sCCOUUKXl8+dUun4a2hGn1fxneYEh8ROSeTXRbNA0ltpL6d4xs+X4o5p+sJBSDjgoProtzbAeTELgzieFZtk/2qbfUzT0Zi3hIIFde/UHOEemCp88Une4k87YS8hK3n5H0s1EJLW8P4QUKdYIFU+r6iLZ3SVrT61l1IBvE0DoO+cUd3GpcA3XnAUamIDwtLzg6c7atTPKDnb3lM25K+LB7/1HIUTMZHmvE2JCuQM7J/c7hkYGoD4KvKl3UIgbGYCyEjQabHNVE0FiSWX7ZlE2RAA6DlzIxWhwhAbz4pkVggWTesZP+vVclWC2Vypebbt2zx+qTVWMNQTsXD9MKUMdOqRAvuytH/Hqw9LE3AoTR6oJE9FWw6NzkXAS2k0ylHHE7kdJh3z3MrCHblR3z1T28O1Me8WbGl1+1WaVEod5gowIsn/ut1/2MLYzyo3DCgDkNr+2nzeZSKIx9J92z6mEwexxlTfOea8cabcYO4QLbyCb+CNq5EhqsVv6UVMDkex6s9YA26WEjj4/Vp8hCKUBavqqqFOJtg8m7EDQQ9G0ZtfX4gLuQulsbluuxynUHrIF0BLdKfKLdAZav3+9lFiA2ORRwLWysYqASdCv4jE4oj2zpTIq4t4iFeOx2/PwJ8fuTMHZkihAbeKolrFq9MZCh6kUg0o1M6kowt8AHPhSG0I1Ng/OCz1lA6m2eBWu2dXtL4P7OJCs447MYgHPhc9k/K+LJvfzOBovIfogZKdwEWbN86ItHVQTDLs3Qlx0qn4tpTJfBcZJUpi03satDiaipWhVNznvr9kDKlGx1VBu+ciqB8MDaC/MGAVeRl+ByqT5t2L6I8koG58o/sNRjMZ1Y5CfFIl5J7j3J2+acYW9vPNrkLgbD5DPAQGhJUlo2K59v62/HrEJDXzz2ue6FEbIASS37X9JdBWqoMav5Ti1dlNpDONbprffNqVyiGromUvTGT4Y9KJWqBCou/p1KzW9s4rCuwzONuKNF7nOqx0lGsCQrL8pkvDwDKJ94lx5oyHZdHNS2xMh98qPo1V7nc=";
 
#if defined(_WIN32) || defined(_WIN64)
static WString output_path = WString::FromLocal("../output_files/");
static WString input_path = WString::FromLocal("../input_files/");
#else
static WString output_path = WString::FromLocal("./output_files/");
static WString input_path = WString::FromLocal("./input_files/");
#endif
 
class SdkLibMgr {
public:
  SdkLibMgr() : is_initialize_(false){};
  ErrorCode Initialize() {
    ErrorCode error_code = Library::Initialize(sn, key);
    if (error_code != foxit::e_ErrSuccess) {
      printf("Library Initialize Error: %d\n", error_code);
    } else {
      is_initialize_ = true;
    }
    return error_code;
 
  }
  ~SdkLibMgr(){
    if(is_initialize_)
      Library::Release();
  }
private:
  bool is_initialize_;
};
 
int main(int argc, char *argv[])
{
  WString output_directory = output_path + L"fdf/";
#if defined(_WIN32) || defined(_WIN64)
  _mkdir(String::FromUnicode(output_directory));
#else
  mkdir(String::FromUnicode(output_directory), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
#endif
 
  int err_ret = 0;
  SdkLibMgr sdk_lib_mgr;
  // Initialize library.
  ErrorCode error_code = sdk_lib_mgr.Initialize();
  if (error_code != foxit::e_ErrSuccess) {
    return 1;
  }
 
  try {
    {
      // Import FDF file
      WString input_file = input_path + L"AboutFoxit.pdf";
      WString fdf_file = input_path + L"AnnotationData.fdf";
      pdf::PDFDoc pdf_doc(input_file);
      ErrorCode error_code = pdf_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;
      }
 
      fdf::FDFDoc fdf_doc(fdf_file);
      pdf_doc.ImportFromFDF(fdf_doc, PDFDoc::e_Annots);
      cout << "Import annotations from fdf with file." << endl;
      WString output_file = output_directory + L"AboutFoxit_importFDF.pdf";
      pdf_doc.SaveAs(output_file, PDFDoc::e_SaveFlagNoOriginal);
    }
 
    {
       // Import FDF file
       WString input_file = input_path + L"AboutFoxit.pdf";
       WString fdf_file = input_path + L"AnnotationData.fdf";
       pdf::PDFDoc pdf_doc(input_file);
       ErrorCode error_code = pdf_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;
       }
 
       FILE* file = NULL;
#if defined(_WIN32) || defined(_WIN64)
       fopen_s(&file, (const char*)(const char*)String::FromUnicode(fdf_file), "rb+");
#else
       file = fopen((const char*)(const char*)String::FromUnicode(fdf_file), "rb+");
#endif
       fseek(file, 0, SEEK_END);
       size_t file_size = (size_t)ftell(file);
       char* buffer = (char*)malloc(file_size * sizeof(char));
       memset(buffer, 0 , file_size);
 
       fseek(file, 0, SEEK_SET);
       fread(buffer, sizeof(char), file_size, file);
       fclose(file);
 
       fdf::FDFDoc fdf_doc(buffer, file_size);
       pdf_doc.ImportFromFDF(fdf_doc, PDFDoc::e_Annots);
       cout << "Import annotations from fdf with memory buffer." << endl;
       WString output_file = output_directory + L"AboutFoxit_importFDF_FromMemory.pdf";
       pdf_doc.SaveAs(output_file, PDFDoc::e_SaveFlagNoOriginal);
       free(buffer);
    }
 
    {
      // Export FDF file
      WString input_file = input_path + L"AnnotationDataExport.pdf";
      WString output_fdf = output_directory + L"AnnotationDataExport_fdf.fdf";
 
      pdf::PDFDoc pdf_doc(input_file);
      ErrorCode error_code = pdf_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;
      }
 
      fdf::FDFDoc fdf_doc(foxit::fdf::FDFDoc::e_FDF);
      pdf_doc.ExportToFDF(fdf_doc, pdf::PDFDoc::e_Annots);
 
      fdf_doc.SaveAs(output_fdf);
      cout << "Export a fdf file." << endl;
 
    }
    {
      // Export XFDF file
      WString input_file = input_path + L"AnnotationDataExport.pdf";
      WString output_xfdf = output_directory + L"AnnotationDataExport_xfdf.xfdf";
 
      pdf::PDFDoc pdf_doc(input_file);
      ErrorCode error_code = pdf_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;
      }
 
      fdf::FDFDoc fdf_doc(foxit::fdf::FDFDoc::e_XFDF);
      pdf_doc.ExportToFDF(fdf_doc, pdf::PDFDoc::e_Annots);
      cout << "Export an xfdf file." << endl;
      fdf_doc.SaveAs(output_xfdf);
    }
    {
      // ImportFromXML
      const char*  xmlinput_file = "../input_files/formfield.xml";
      WString input_file = input_path + L"blank.pdf";
      WString output_pdf = output_directory + L"ImportFromXML.pdf";
 
      pdf::PDFDoc pdf_doc(input_file);
      foxit::pdf::interform::Form form(pdf_doc);
      bool importret = form.ImportFromXML(xmlinput_file);
      ErrorCode error_code = pdf_doc.Load();
      if (error_code != foxit::e_ErrSuccess) {
        printf("The Doc [%s] Error: %d\n", input_file, error_code);
        return 1;
      }
      pdf_doc.SaveAs(output_pdf);
    }
 
    {
      // ExportToXML
      WString input_file = input_path + L"Foxitform.pdf";
      const char* output_pdf = "../output_files/ExportToXML.xml";
 
      pdf::PDFDoc pdf_doc(input_file);
      ErrorCode error_code = pdf_doc.Load();
      if (error_code != foxit::e_ErrSuccess) {
        printf("The Doc [%s] Error: %d\n", input_file, error_code);
        return 1;
      }
      foxit::pdf::interform::Form form(pdf_doc);
      bool importret = form.ExportToXML(output_pdf);
      pdf_doc.SaveAs(output_pdf);
    }    } catch (const Exception& e) {
    cout << e.GetMessage() << endl;
    err_ret = 1;
  }
  catch(...)
  {
    cout << "Unknown Exception" << endl;
    err_ret = 1;
  }
 
  return err_ret;
}  

C# language

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 System.IO;
using System.Runtime.InteropServices;
 
namespace fdfCS
{
    class fdf
    {
        static void Main(string[] args)
        {
            string input_path = "../input_files/";
            string output_path = "../output_files/fdf/";
            System.IO.Directory.CreateDirectory(output_path);
 
            string sn = "VQv/htdRKu1rjhk90MTKF+/aCs9DfQRtBF4SNc0GwPlOX8ualGeTJg==";
            string key = "ezJvj18mtBp399sXJVWofHfLliq8pf9v7BMskv+zLerFJVWviGEfZ+kCs+nfwjfVTthVUzkIj1qr9k37wnS1f2YSW3jOA/0EfK806dB9bigN097uSthk6eeafc30XHVMSGerLpzYziDW9zL/1oMTZ80YX6Trs0BWaif611VZm/SJ4PkaRzN0sCCOUUKXl8+dUun4a2hGn1fxneYEh8ROSeTXRbNA0ltpL6d4xs+X4o5p+sJBSDjgoProtzbAeTELgzieFZtk/2qbfUzT0Zi3hIIFde/UHOEemCp88Une4k87YS8hK3n5H0s1EJLW8P4QUKdYIFU+r6iLZ3SVrT61l1IBvE0DoO+cUd3GpcA3XnAUamIDwtLzg6c7atTPKDnb3lM25K+LB7/1HIUTMZHmvE2JCuQM7J/c7hkYGoD4KvKl3UIgbGYCyEjQabHNVE0FiSWX7ZlE2RAA6DlzIxWhwhAbz4pkVggWTesZP+vVclWC2Vypebbt2zx+qTVWMNQTsXD9MKUMdOqRAvuytH/Hqw9LE3AoTR6oJE9FWw6NzkXAS2k0ylHHE7kdJh3z3MrCHblR3z1T28O1Me8WbGl1+1WaVEod5gowIsn/ut1/2MLYzyo3DCgDkNr+2nzeZSKIx9J92z6mEwexxlTfOea8cabcYO4QLbyCb+CNq5EhqsVv6UVMDkex6s9YA26WEjj4/Vp8hCKUBavqqqFOJtg8m7EDQQ9G0ZtfX4gLuQulsbluuxynUHrIF0BLdKfKLdAZav3+9lFiA2ORRwLWysYqASdCv4jE4oj2zpTIq4t4iFeOx2/PwJ8fuTMHZkihAbeKolrFq9MZCh6kUg0o1M6kowt8AHPhSG0I1Ng/OCz1lA6m2eBWu2dXtL4P7OJCs447MYgHPhc9k/K+LJvfzOBovIfogZKdwEWbN86ItHVQTDLs3Qlx0qn4tpTJfBcZJUpi03satDiaipWhVNznvr9kDKlGx1VBu+ciqB8MDaC/MGAVeRl+ByqT5t2L6I8koG58o/sNRjMZ1Y5CfFIl5J7j3J2+acYW9vPNrkLgbD5DPAQGhJUlo2K59v62/HrEJDXzz2ue6FEbIASS37X9JdBWqoMav5Ti1dlNpDONbprffNqVyiGromUvTGT4Y9KJWqBCou/p1KzW9s4rCuwzONuKNF7nOqx0lGsCQrL8pkvDwDKJ94lx5oyHZdHNS2xMh98qPo1V7nc=";
            // Initialize library.
            ErrorCode error_code = Library.Initialize(sn, key);
            if (error_code != ErrorCode.e_ErrSuccess)
            {
                Console.WriteLine("Library Initialize Error: {0}\n", error_code);
                return;
            }
 
            try
            {
                {
                    // Import FDF file
                    string input_file = input_path + "AboutFoxit.pdf";
                    string fdf_file = input_path + "AnnotationData.fdf";
                    using (PDFDoc pdf_doc = new PDFDoc(input_file))
                    {
                        error_code = pdf_doc.Load(null);
                        if (error_code != ErrorCode.e_ErrSuccess)
                        {
                            Console.WriteLine("The Doc [{0}] Error: {1}\n", input_file, error_code);
                            Library.Release();
                            return;
                        }
                        using (foxit.fdf.FDFDoc fdf_doc = new foxit.fdf.FDFDoc(fdf_file))
                        {
                            pdf_doc.ImportFromFDF(fdf_doc, (int)foxit.pdf.PDFDoc.DataType.e_Annots, new Range());
                            Console.WriteLine("Import annotations from fdf with file.\n");
                            string output_file = output_path + "AboutFoxit_importFDF.pdf";
                            pdf_doc.SaveAs(output_file, (int)PDFDoc.SaveFlags.e_SaveFlagNoOriginal);
                        }
                    }
 
                    byte[] buffer = File.ReadAllBytes(fdf_file);
                    IntPtr buffer_intptr = Marshal.AllocHGlobal(buffer.Length);
                    Marshal.Copy(buffer, 0, buffer_intptr, buffer.Length);
 
                    using (PDFDoc pdf_doc = new PDFDoc(input_file))
                    {
                        error_code = pdf_doc.Load(null);
                        if (error_code != ErrorCode.e_ErrSuccess)
                        {
                            Console.WriteLine("The Doc [{0}] Error: {1}\n", input_file, error_code);
                            Library.Release();
                            return;
                        }
                        using (foxit.fdf.FDFDoc fdf_doc = new foxit.fdf.FDFDoc(buffer_intptr, (uint)buffer.Length))
                        {
                            pdf_doc.ImportFromFDF(fdf_doc, (int)foxit.pdf.PDFDoc.DataType.e_Annots, new Range());
                            Console.WriteLine("Import annotations from fdf with memory buffer.\n");
                            string output_file = output_path + "AboutFoxit_importFDF_From_Memory.pdf";
                            pdf_doc.SaveAs(output_file, (int)PDFDoc.SaveFlags.e_SaveFlagNoOriginal);
                        }
                    }
                    Marshal.FreeHGlobal(buffer_intptr);
 
                    {
                        // Export FDF file
                        input_file = input_path + "AnnotationDataExport.pdf";
                        string output_fdf = output_path + "AnnotationDataExport_fdf.fdf";
       
                        using(foxit.pdf.PDFDoc pdf_doc = new foxit.pdf.PDFDoc(input_file))
                        {
                            error_code = pdf_doc.Load(null);
                            if (error_code != ErrorCode.e_ErrSuccess)
                            {
                                Console.WriteLine("The Doc [{0}] Error: {1}\n", input_file, error_code);
                                Library.Release();
                                return;
                            }
 
                            using (foxit.fdf.FDFDoc fdf_doc = new foxit.fdf.FDFDoc(foxit.fdf.FDFDoc.Type.e_FDF))
                            {
                                pdf_doc.ExportToFDF(fdf_doc, (int)foxit.pdf.PDFDoc.DataType.e_Annots, new Range());
                                fdf_doc.SaveAs(output_fdf);
                                Console.WriteLine("Export a fdf file.\n");
                            }
                        }
                    }
                     
                    {
                        // Export XFDF file
                        input_file = input_path + "AnnotationDataExport.pdf";
                        string output_xfdf = output_path + "AnnotationDataExport_xfdf.xfdf";
 
                        using (foxit.pdf.PDFDoc pdf_doc = new foxit.pdf.PDFDoc(input_file))
                        {
                            error_code = pdf_doc.Load(null);
                            if (error_code != ErrorCode.e_ErrSuccess)
                            {
                                Console.WriteLine("The Doc [{0}] Error: {1}\n", input_file, error_code);
                                Library.Release();
                                return;
                            }
 
                            using (foxit.fdf.FDFDoc fdf_doc = new foxit.fdf.FDFDoc(foxit.fdf.FDFDoc.Type.e_XFDF))
                            {
                                pdf_doc.ExportToFDF(fdf_doc, (int)foxit.pdf.PDFDoc.DataType.e_Annots, new Range());
                                fdf_doc.SaveAs(output_xfdf);
                                Console.WriteLine("Export an xfdf file.\n");
                            }
                        }
                    }
                              {
                                // ImportFromXML
                                string xmlinput_file = input_path + "formfield.xml";
                                string input_file = input_path + L"blank.pdf";
 
                                string output_fdf = output_path + "ImportFromXML.pdf";
 
                                using(foxit.pdf.PDFDoc pdf_doc = new foxit.pdf.PDFDoc(input_file))
                                {
                                  error_code = pdf_doc.Load(null);
                                  if (error_code != ErrorCode.e_ErrSuccess)
                                  {
                                    Console.WriteLine("The Doc [{0}] Error: {1}\n", input_file, error_code);
                                    Library.Release();
                                    return;
                                  }
        
 
                                  using (foxit.pdf.interform.Form form = new foxit.pdf.interform.Form(pdf_doc);)
                                  {
                                    bool importret = form.ImportFromXML(xmlinput_file);
                                    pdf_doc.SaveAs(output_fdf);
                                  }
                                }
                              }
 
                              {
                                // ImportFromXML
                                string xmlinput_file = input_path + "formfield.xml";
                                string input_file = input_path + "Foxitform.pdf";
 
                                string output_fdf = output_path + "ExportToXML.xml";
 
                                using(foxit.pdf.PDFDoc pdf_doc = new foxit.pdf.PDFDoc(input_file))
                                {
                                  error_code = pdf_doc.Load(null);
                                  if (error_code != ErrorCode.e_ErrSuccess)
                                  {
                                    Console.WriteLine("The Doc [{0}] Error: {1}\n", input_file, error_code);
                                    Library.Release();
                                    return;
                                  }
 
 
                                  using (foxit.pdf.interform.Form form = new foxit.pdf.interform.Form(pdf_doc);)
                                  {
                                    bool importret = form.ExportToXML(output_pdf);
                                    pdf_doc.SaveAs(output_fdf);
                                  }
                                }
                              }
 
            }
            catch (foxit.PDFException e)
            {
                Console.WriteLine(e.Message);
            }
 
            Library.Release();
        }
    }
}

Python language

import os
import sys
import site
 
if sys.version_info.major == 2:
    _PYTHON2_ = True
else:
    _PYTHON2_ = False
 
if _PYTHON2_:
    site.addsitedir('../../../')
    from FoxitPDFSDKPython2 import *
else:
    from FoxitPDFSDKPython3 import *
 
sn = "VQv/htdRKu1rjhk90MTKF+/aCs9kipmGO6LApa45lb/wyCx18tXQrA=="
key = "ezKXjt/HvGhz9DvoP0WQjY5Xqga7QWN7LdtDOn2x7U/yDsvKiOBa70neckn4NUAydhBYDzd04wEuIfvVtJ6rZh4BCq6i0Uqb3QHdujAZE5dBC4orX/uEYYe4dkEPNVVKACdsMPtabm+W0WLKiFtYhUsdQLKk3mAwodGFOSHjNxDq5NV6N2a99e/LJh0OIfSVpa01nwis7UUfks9AJBlJUx6XEMwe4YBlSuN8oDmDqIGpZEGyHpGJbz3gH4O/WVw4uc5msZzKeeFPwBkIitHlogibJUKzJaqNv1jtJMNQ2V5BwatI7PYLopmYgwMRfhxDxKwo/4BHs41/07+1YBv4Ym3WWbwjhgFDHGDf0eH079G2xw9HPXQu/iG7OHsD136Q8+IL/a3evyWveZVGTWRvsRgzof6ADDQBqBlVKqPWeceOTlbPilVUS6IKIsukfo1xwwo4kWnA9I2mFKTSM10J9Ny6cu99YLb683bpme6HcJm+1gEAnuLPg7UliHpMGbWiAjvzjFYKREnKn7WZWiDcnLn6dXvkOVlxs5Gl/AwlR7D3/M2s4SkOuk6YWt9hvxjouJRERDPGjvclfYjF59MhK7zfIvRqoiXnJrcLaAT4aH9adV2t60fZWYbhMzSZzTbUz+PzcxqHgCDzFs7XI8pXibPZWOgW2HuND9URjZqkPqf80GnFgEFeikvMFrEoth9OpAKQcPs1X4pn1HP5SgtEd6xfxmzy5aoB/9eEtbLF8gIpt4PJ80TFB6pt4B8FS9O55apzifByGRs8wjGeHQS5mO68Dgpo2WPQxyUJefHfIF1f3SItRgfVj+kb50TFsddxRqmknmYG2MZ7VPG//nzlq2R+rqVvKfUqDSrwuwiBxcjObxaZK1q/y2a1Da8QE7nnmrlcJXtZsjpIIN7aItKyNzFfLfI7nqVFzw2RcJltN9DVX/blIKiAJoASIFDYitKm9bjjaXqFaJCTnZQWe2oXLNqweTJGy3sc8d59zx8kWzc+mpaNwcUOf+8mDOSvfcohYA68aJnWcUYkUFG0mrZPD/jCuayhNZZl4nsqiXCrRcTrvcQ4ttIlz88misEFCyixVvZxFWZaXExmUZHQU7WZGIoMsiq4YWZQpoqLuzQzBvqUl+pR1ybTga/der/GmLyz6IfYrHsMiBQr6bncDB5quV5HyGOHy27jOLhZ2ViVWG0rnJYqqj5TkwrtgC7Jn/ymQ0wOppv6Hu2nVIZkhvSUvScfP+DBOC3eW5yT+mhKRcIuff4plyuLKbFH2fAUAT3InisKAeJF"
 
output_path = "../output_files/"
input_path = "../input_files/"
output_directory = output_path + "fdf/"
 
 
class SDKLibMgr:
 
    def __init__(self):
        self.is_initialize_ = False
 
    def Initialize(self):
        # Initialize library
        error_code = Library.Initialize(sn, key)
        if error_code != e_ErrSuccess:
            print("Library Initialize Error: {}".format(error_code))
            return False
        self.is_initialize_ = True
        return True
 
    def Release(self):
        if self.is_initialize_:
            Library.Release()
 
 
def main():
    err_ret = 0
    input_file = input_path + "AboutFoxit.pdf"
    fdf_file = input_path + "AnnotationData.fdf"
 
    if not os.path.exists(output_directory):
        os.makedirs(output_directory)
 
    try:
 
        pdf_doc = PDFDoc(input_file)
        error_code = pdf_doc.Load("")
        if error_code != e_ErrSuccess:
            print("The PDFDoc {} Error: {}".format(input_file, error_code))
            return 1
 
        fdf_doc = FDFDoc(fdf_file)
        pdf_doc.ImportFromFDF(fdf_doc, PDFDoc.e_Annots)
        print("Import annotations from fdf with file.")
        output_file = output_directory + "AboutFoxit_importFDF.pdf"
        pdf_doc.SaveAs(output_file, PDFDoc.e_SaveFlagNoOriginal)
 
        # Import FDF file
        input_file = input_path + "AboutFoxit.pdf"
        fdf_file = input_path + "AnnotationData.fdf"
        pdf_doc = PDFDoc(input_file)
        error_code = pdf_doc.Load("")
        if error_code != e_ErrSuccess:
            print("The Doc [{}] Error: {}\n".format(input_file, error_code))
            return 1
 
        file = open(fdf_file, "rb+")
        file.seek(0, os.SEEK_END)
        file_size = file.tell()
        file.seek(0, os.SEEK_SET)
        buffer = file.read(file_size)
        file.close()
 
        fdf_doc = FDFDoc(buffer, file_size)
        pdf_doc.ImportFromFDF(fdf_doc, PDFDoc.e_Annots)
        print("Import annotations from fdf with memory buffer.")
        output_file = output_directory + "AboutFoxit_importFDF_FromMemory.pdf"
        pdf_doc.SaveAs(output_file, PDFDoc.e_SaveFlagNoOriginal)
 
        # Export FDF file
        input_file = input_path + "AnnotationDataExport.pdf"
        output_fdf = output_directory + "AnnotationDataExport_fdf.fdf"
 
        pdf_doc = PDFDoc(input_file)
        error_code = pdf_doc.Load("")
        if error_code != e_ErrSuccess:
            print("The Doc [{}] Error: {}\n".format(input_file, error_code))
            return 1
 
        fdf_doc = FDFDoc(FDFDoc.e_FDF)
        pdf_doc.ExportToFDF(fdf_doc, PDFDoc.e_Annots)
 
        fdf_doc.SaveAs(output_fdf)
        print("Export a fdf file.")
 
        # Export XFDF file
        input_file = input_path + "AnnotationDataExport.pdf"
        output_xfdf = output_directory + "AnnotationDataExport_xfdf.xfdf"
 
        pdf_doc = PDFDoc(input_file)
        error_code = pdf_doc.Load("")
        if error_code != e_ErrSuccess:
            print("The Doc [%s] Error: %d\n".format(input_file, error_code))
            return 1
 
        fdf_doc = FDFDoc(FDFDoc.e_XFDF)
        pdf_doc.ExportToFDF(fdf_doc, PDFDoc.e_Annots)
        print("Export an xfdf file.")
        fdf_doc.SaveAs(output_xfdf)
       
        // ImportFromXML
        const char*  xmlinput_file = "../input_files/formfield.xml";
        WString input_file = input_path + L"blank.pdf";
        WString output_pdf = output_directory + L"ImportFromXML.pdf";
 
        pdf::PDFDoc pdf_doc(input_file);
        foxit.pdf.interform.Form form = new foxit.pdf.interform.Form(pdf_doc);
        bool importret = form.ImportFromXML(xmlinput_file);
        ErrorCode error_code = pdf_doc.Load();
        if (error_code != foxit::e_ErrSuccess) {
          printf("The Doc [%s] Error: %d\n", input_file, error_code);
          return 1;
        }
        pdf_doc.SaveAs(output_pdf);
       
 
 
       
        // Export XFDF file
        WString input_file = input_path + L"Foxitform.pdf";
        const char* output_pdf = "../output_files/ExportToXML.xml";
 
        pdf::PDFDoc pdf_doc(input_file);
        ErrorCode error_code = pdf_doc.Load();
        if (error_code != foxit::e_ErrSuccess) {
          printf("The Doc [%s] Error: %d\n", input_file, error_code);
          return 1;
        }
        foxit::pdf::interform::Form form(pdf_doc);
        bool importret = form.ExportToXML(output_pdf);
        pdf_doc.SaveAs(output_pdf);
       
                         
      except Exception as ex:
        print(ex.GetMessage())
        err_ret = 1
 
    return err_ret
 
 
if __name__ == '__main__':
    libmgr = SDKLibMgr()
    if libmgr.Initialize():
        main()
 
    libmgr.Release()

Java language

import com.foxit.sdk.PDFException;
import com.foxit.sdk.common.Library;
import com.foxit.sdk.common.Range;
import com.foxit.sdk.fdf.FDFDoc;
import com.foxit.sdk.pdf.PDFDoc;
 
import java.io.*;
import java.io.File;
import java.io.FileWriter;
 
import static com.foxit.sdk.common.Constants.e_ErrSuccess;
import static com.foxit.sdk.fdf.FDFDoc.e_FDF;
import static com.foxit.sdk.fdf.FDFDoc.e_XFDF;
import static com.foxit.sdk.pdf.PDFDoc.e_Annots;
import static com.foxit.sdk.pdf.PDFDoc.e_SaveFlagNoOriginal;
 
public class fdf {
    private static String key = "ezJvj18mtBp399sXJVWofHfLliq8pf9v7BMskv+zLerFJVWviGEfZ+kCs+nfwjfVTthVUzkIj1qr9k37wnS1f2YSW3jOA/0EfK806dB9bigN097uSthk6eeafc30XHVMSGerLpzYziDW9zL/1oMTZ80YX6Trs0BWaif611VZm/SJ4PkaRzN0sCCOUUKXl8+dUun4a2hGn1fxneYEh8ROSeTXRbNA0ltpL6d4xs+X4o5p+sJBSDjgoProtzbAeTELgzieFZtk/2qbfUzT0Zi3hIIFde/UHOEemCp88Une4k87YS8hK3n5H0s1EJLW8P4QUKdYIFU+r6iLZ3SVrT61l1IBvE0DoO+cUd3GpcA3XnAUamIDwtLzg6c7atTPKDnb3lM25K+LB7/1HIUTMZHmvE2JCuQM7J/c7hkYGoD4KvKl3UIgbGYCyEjQabHNVE0FiSWX7ZlE2RAA6DlzIxWhwhAbz4pkVggWTesZP+vVclWC2Vypebbt2zx+qTVWMNQTsXD9MKUMdOqRAvuytH/Hqw9LE3AoTR6oJE9FWw6NzkXAS2k0ylHHE7kdJh3z3MrCHblR3z1T28O1Me8WbGl1+1WaVEod5gowIsn/ut1/2MLYzyo3DCgDkNr+2nzeZSKIx9J92z6mEwexxlTfOea8cabcYO4QLbyCb+CNq5EhqsVv6UVMDkex6s9YA26WEjj4/Vp8hCKUBavqqqFOJtg8m7EDQQ9G0ZtfX4gLuQulsbluuxynUHrIF0BLdKfKLdAZav3+9lFiA2ORRwLWysYqASdCv4jE4oj2zpTIq4t4iFeOx2/PwJ8fuTMHZkihAbeKolrFq9MZCh6kUg0o1M6kowt8AHPhSG0I1Ng/OCz1lA6m2eBWu2dXtL4P7OJCs447MYgHPhc9k/K+LJvfzOBovIfogZKdwEWbN86ItHVQTDLs3Qlx0qn4tpTJfBcZJUpi03satDiaipWhVNznvr9kDKlGx1VBu+ciqB8MDaC/MGAVeRl+ByqT5t2L6I8koG58o/sNRjMZ1Y5CfFIl5J7j3J2+acYW9vPNrkLgbD5DPAQGhJUlo2K59v62/HrEJDXzz2ue6FEbIASS37X9JdBWqoMav5Ti1dlNpDONbprffNqVyiGromUvTGT4Y9KJWqBCou/p1KzW9s4rCuwzONuKNF7nOqx0lGsCQrL8pkvDwDKJ94lx5oyHZdHNS2xMh98qPo1V7nc=";
    private static String sn = "VQv/htdRKu1rjhk90MTKF+/aCs9DfQRtBF4SNc0GwPlOX8ualGeTJg==";
    private static String output_path = "../output_files/fdf/";
    private static String input_path = "../input_files/";
 
    // You can also use System.load("filename") instead. The filename argument must be an absolute path name.
    static {
        String os = System.getProperty("os.name").toLowerCase();
        String lib = "fsdk_java_";
        if (os.startsWith("win")) {
            lib += "win";
        } else if (os.startsWith("mac")) {
            lib += "mac";
        } else {
            lib += "linux";
        }
         
        if (System.getProperty("sun.arch.data.model").equals("64")) {
          if(System.getProperty("os.arch").equals("aarch64")){
            if(os.startsWith("mac")){
               lib += "arm";
             }
             else{
               lib += "armv8";
             }
           }
           else{
              lib += "64";
           }
        } else {
           if(System.getProperty("os.arch").equals("arm")){
              lib += "armv7";
           }
           else{
              lib += "32";
           }
        }
        System.loadLibrary(lib);
    }
 
    private static void createResultFolder(String output_path) {
        File myPath = new File(output_path);
        if (!myPath.exists()) {
            myPath.mkdir();
        }
    }
 
    public static void main(String[] args) throws PDFException {
        createResultFolder(output_path);
        // Initialize library
        int error_code = Library.initialize(sn, key);
        if (error_code != e_ErrSuccess) {
            System.out.println("Library Initialize Error: " + error_code);
            return;
        }
        try {
            Range empty_range = new Range();
{
    String input_file = input_path + "AboutFoxit.pdf";
    String fdf_file = input_path + "AnnotationData.fdf";
    PDFDoc pdf_doc = new PDFDoc(input_file);
    error_code = pdf_doc.load(null);
    if (error_code != e_ErrSuccess) {
        System.out.println("The Doc " + input_file + " Error: " + error_code);
        return;
    }
    FDFDoc fdf_doc = new FDFDoc(fdf_file);
    pdf_doc.importFromFDF(fdf_doc, e_Annots, empty_range);
    System.out.println("Import annotations from fdf with file.");
    String output_file = output_path + "AboutFoxit_importFDF.pdf";
    pdf_doc.saveAs(output_file, e_SaveFlagNoOriginal);
}
 
{
    String input_file = input_path + "AboutFoxit.pdf";
    String fdf_file = input_path + "AnnotationData.fdf";
    PDFDoc pdf_doc = new PDFDoc(input_file);
    error_code = pdf_doc.load(null);
    if (error_code != e_ErrSuccess) {
        System.out.println("The Doc " + input_file + " Error: " + error_code);
        return;
    }
File file = new File(fdf_file); 
Long filelength = file.length(); 
byte[] filecontent = new byte[filelength.intValue()]; 
try { 
FileInputStream in = new FileInputStream(file); 
in.read(filecontent); 
in.close(); 
} catch (FileNotFoundException e) { 
e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
 
    FDFDoc fdf_doc = new FDFDoc(filecontent);
    pdf_doc.importFromFDF(fdf_doc, e_Annots, empty_range);
    System.out.println("Import annotations from fdf with memory buffer.");
    String output_file = output_path + "AboutFoxit_importFDF_FromMemory.pdf";
    pdf_doc.saveAs(output_file, e_SaveFlagNoOriginal);
}
 
{
    // Export FDF file
    String input_file = input_path + "AnnotationDataExport.pdf";
    String output_fdf = output_path + "AnnotationDataExport_fdf.fdf";
 
 
    PDFDoc pdf_doc = new PDFDoc(input_file);
    error_code = pdf_doc.load(null);
    if (error_code != e_ErrSuccess) {
        System.out.println("The Doc " + input_file + "Error: " + error_code);
        return;
    }
 
    FDFDoc fdf_doc = new FDFDoc(e_FDF);
    pdf_doc.exportToFDF(fdf_doc, e_Annots, empty_range);
    System.out.println("Export a fdf file.");
    fdf_doc.saveAs(output_fdf);
}
 
{
    // Export XFDF file
    String input_file = input_path + "AnnotationDataExport.pdf";
    String output_xfdf = output_path + "AnnotationDataExport_xfdf.xfdf";
 
    PDFDoc pdf_doc = new PDFDoc(input_file);
    error_code = pdf_doc.load(null);
    if (error_code != e_ErrSuccess) {
        System.out.println("The Doc " + input_file + " Error: " + error_code);
        return;
    }
 
    FDFDoc fdf_doc = new FDFDoc(e_XFDF);
    pdf_doc.exportToFDF(fdf_doc, e_Annots, empty_range);
    System.out.println("Export an xfdf file.");
    fdf_doc.saveAs(output_xfdf);
}
  {
    // ImportFromXML
    string xmlinput_file = input_path + "formfield.xml";
    string input_file = input_path + L"blank.pdf";
 
    string output_fdf = output_path + "ImportFromXML.pdf";
 
    foxit.pdf.PDFDoc pdf_doc = new foxit.pdf.PDFDoc(input_file);
                                 
      error_code = pdf_doc.load(null);
      using (foxit.pdf.interform.Form form = new foxit.pdf.interform.Form(pdf_doc);)
      {
        bool importret = form.importFromXML(xmlinput_file);
        pdf_doc.saveAs(output_fdf);
      }
                                 
  }
 
  {
    // ImportFromXML
    string xmlinput_file = input_path + "formfield.xml";
    string input_file = input_path + "Foxitform.pdf";
 
    string output_fdf = output_path + "ExportToXML.xml";
 
    foxit.pdf.PDFDoc pdf_doc = new foxit.pdf.PDFDoc(input_file);
      error_code = pdf_doc.load(null);
      using (foxit.pdf.interform.Form form = new foxit.pdf.interform.Form(pdf_doc);)
      {
        bool importret = form.exportToXML(output_pdf);
        pdf_doc.saveAs(output_fdf);
      }
                                 
  }
                                 
                              }
 
} catch (Exception e) {
e.printStackTrace();
}
 
        Library.release();
    }
}  

C language

#include<direct.h>
#include <stdio.h>
#include <stdlib.h>
 
#include "../../../include/fs_basictypes_c.h"
#include "../../../include/fs_common_c.h"
#include "../../../include/fs_pdfdoc_c.h"
#include "../../../include/fs_fdfdoc_c.h"
#include "../../../include/fs_pdfpage_c.h"
#include "../../../include/fs_annot_c.h"
#include "../../../include/fs_pdfform_c.h"
 
#ifndef TRUE
#define TRUE 1
#endif
 
#ifndef FALSE
#define FALSE 0
#endif
 
#ifndef NULL
#define NULL (void*)0
#endif
 
#define MAX_FILE_PATH 260
 
static const char* sn = "VQv/htdRKu1rjhk90MTKF+/aCs9DfQRtBF4SNc0GwPlOX8ualGeTJg==";
static const char* key = "ezJvj18mtBp399sXJVWofHfLliq8pf9v7BMskv+zLerFJVWviGEfZ+kCs+nfwjfVTthVUzkIj1qr9k37wnS1f2YSW3jOA/0EfK806dB9bigN097uSthk6eeafc30XHVMSGerLpzYziDW9zL/1oMTZ80YX6Trs0BWaif611VZm/SJ4PkaRzN0sCCOUUKXl8+dUun4a2hGn1fxneYEh8ROSeTXRbNA0ltpL6d4xs+X4o5p+sJBSDjgoProtzbAeTELgzieFZtk/2qbfUzT0Zi3hIIFde/UHOEemCp88Une4k87YS8hK3n5H0s1EJLW8P4QUKdYIFU+r6iLZ3SVrT61l1IBvE0DoO+cUd3GpcA3XnAUamIDwtLzg6c7atTPKDnb3lM25K+LB7/1HIUTMZHmvE2JCuQM7J/c7hkYGoD4KvKl3UIgbGYCyEjQabHNVE0FiSWX7ZlE2RAA6DlzIxWhwhAbz4pkVggWTesZP+vVclWC2Vypebbt2zx+qTVWMNQTsXD9MKUMdOqRAvuytH/Hqw9LE3AoTR6oJE9FWw6NzkXAS2k0ylHHE7kdJh3z3MrCHblR3z1T28O1Me8WbGl1+1WaVEod5gowIsn/ut1/2MLYzyo3DCgDkNr+2nzeZSKIx9J92z6mEwexxlTfOea8cabcYO4QLbyCb+CNq5EhqsVv6UVMDkex6s9YA26WEjj4/Vp8hCKUBavqqqFOJtg8m7EDQQ9G0ZtfX4gLuQulsbluuxynUHrIF0BLdKfKLdAZav3+9lFiA2ORRwLWysYqASdCv4jE4oj2zpTIq4t4iFeOx2/PwJ8fuTMHZkihAbeKolrFq9MZCh6kUg0o1M6kowt8AHPhSG0I1Ng/OCz1lA6m2eBWu2dXtL4P7OJCs447MYgHPhc9k/K+LJvfzOBovIfogZKdwEWbN86ItHVQTDLs3Qlx0qn4tpTJfBcZJUpi03satDiaipWhVNznvr9kDKlGx1VBu+ciqB8MDaC/MGAVeRl+ByqT5t2L6I8koG58o/sNRjMZ1Y5CfFIl5J7j3J2+acYW9vPNrkLgbD5DPAQGhJUlo2K59v62/HrEJDXzz2ue6FEbIASS37X9JdBWqoMav5Ti1dlNpDONbprffNqVyiGromUvTGT4Y9KJWqBCou/p1KzW9s4rCuwzONuKNF7nOqx0lGsCQrL8pkvDwDKJ94lx5oyHZdHNS2xMh98qPo1V7nc=";
 
static const wchar_t* output_path = L"../output_files/";
static const wchar_t* input_path = L"../input_files/";
 
int main(int argc, char *argv[]) {
  int err_ret = 0;
  wchar_t output_directory[MAX_FILE_PATH] = { 0 };
  FSErrorCode error_code = e_FSErrSuccess;
 
  error_code = FSDK_Library_Initialize(sn, key);
  if (error_code != e_FSErrSuccess) {
    printf("Library Initialize Error: %d\n", error_code);
    return 1;
  }
 
  swprintf_s(output_directory, MAX_FILE_PATH, L"%lsfdf/", output_path);
  _wmkdir(output_directory);
 
  {
    {
      // Import FDF file
      wchar_t input_file[MAX_FILE_PATH] = { 0 };
      wchar_t fdf_file[MAX_FILE_PATH] = { 0 };
      FS_PDFDOC_HANDLE pdf_doc = NULL;
      FS_FDFDOC_HANDLE fdf_doc = NULL;
      FS_RANGE_HANDLE page_range = NULL;
      FS_BOOL return_result;
      wchar_t output_file[MAX_FILE_PATH] = { 0 };
      FS_BOOL result_value;
 
      swprintf_s(input_file, MAX_FILE_PATH, L"%lsAboutFoxit.pdf", input_path);
      swprintf_s(fdf_file, MAX_FILE_PATH, L"%lsAnnotationData.fdf", input_path);
 
      FSDK_PDFDoc_Create0(input_file, &pdf_doc);
      error_code = FSDK_PDFDoc_Load(pdf_doc, NULL);
      if (error_code != e_FSErrSuccess) {
        wprintf(L"The Doc [%ls] Error: %d\n", input_file, error_code);
        return 1;
      }
 
      FSDK_FDFDoc_Create1(fdf_file, &fdf_doc);
      FSDK_Range_Create(&page_range);
      FSDK_PDFDoc_ImportFromFDF(pdf_doc, fdf_doc, e_FSAnnots, page_range, &return_result);
      printf("Import annotations from fdf with file.\n");
 
      swprintf_s(output_file, MAX_FILE_PATH, L"%lsAboutFoxit_importFDF.pdf", output_directory);
 
      FSDK_PDFDoc_SaveAs(pdf_doc, output_file, e_FSSaveFlagNoOriginal, &result_value);
      FSDK_Range_Release(page_range);
      FSDK_FDFDoc_Release(fdf_doc);
      FSDK_PDFDoc_Release(pdf_doc);
    }
 
    {
      // Import FDF file
      wchar_t input_file[MAX_FILE_PATH] = { 0 };
      wchar_t fdf_file[MAX_FILE_PATH] = { 0 };
      FS_PDFDOC_HANDLE pdf_doc = NULL;
      FILE* file = NULL;
      FS_FDFDOC_HANDLE fdf_doc = NULL;
      FS_RANGE_HANDLE page_range = NULL;
      FS_BOOL return_result;
      wchar_t output_file[MAX_FILE_PATH] = { 0 };
      size_t file_size = 0;
      char* buffer = NULL;
 
      swprintf_s(input_file, MAX_FILE_PATH, L"%lsAboutFoxit.pdf", input_path);
 
      swprintf_s(fdf_file, MAX_FILE_PATH, L"%lsAnnotationData.fdf", input_path);
 
      FSDK_PDFDoc_Create0(input_file, &pdf_doc);
      error_code = FSDK_PDFDoc_Load(pdf_doc, NULL);
      if (error_code != e_FSErrSuccess) {
        wprintf(L"The Doc [%ls] Error: %d\n", input_file, error_code);
        return 1;
      }
 
      _wfopen_s(&file, fdf_file, L"rb+");
 
      fseek(file, 0, SEEK_END);
      file_size = (size_t)ftell(file);
      buffer = (char*)malloc(file_size * sizeof(char));
      memset(buffer, 0, file_size);
 
      fseek(file, 0, SEEK_SET);
      fread(buffer, sizeof(char), file_size, file);
      fclose(file);
 
      FSDK_FDFDoc_Create2(buffer, file_size, &fdf_doc);
 
      FSDK_Range_Create(&page_range);
 
      FSDK_PDFDoc_ImportFromFDF(pdf_doc, fdf_doc, e_FSAnnots, page_range, &return_result);
      printf("Import annotations from fdf with memory buffer.\n");
 
      swprintf_s(output_file, MAX_FILE_PATH, L"%lsAboutFoxit_importFDF_FromMemory.pdf", output_directory);
      FSDK_PDFDoc_SaveAs(pdf_doc, output_file, e_FSSaveFlagNoOriginal, &return_result);
      free(buffer);
      FSDK_Range_Release(page_range);
      FSDK_FDFDoc_Release(fdf_doc);
      FSDK_PDFDoc_Release(pdf_doc);
    }
 
    {
      // Export FDF file
      wchar_t input_file[MAX_FILE_PATH] = { 0 };
      wchar_t output_fdf[MAX_FILE_PATH] = { 0 };
      FS_PDFDOC_HANDLE pdf_doc = NULL;
      FS_FDFDOC_HANDLE fdf_doc = NULL;
      FS_RANGE_HANDLE page_range = NULL;
      FS_BOOL return_result;
 
      swprintf_s(input_file, MAX_FILE_PATH, L"%lsAnnotationDataExport.pdf", input_path);
 
      swprintf_s(output_fdf, MAX_FILE_PATH, L"%lsAnnotationDataExport_fdf.fdf", output_directory);
 
      FSDK_PDFDoc_Create0(input_file, &pdf_doc);
      error_code = FSDK_PDFDoc_Load(pdf_doc, NULL);
      if (error_code != e_FSErrSuccess) {
        wprintf(L"The Doc [%ls] Error: %d\n", input_file, error_code);
        return 1;
      }
 
      FSDK_FDFDoc_Create0(e_FSFDF, &fdf_doc);
 
      FSDK_Range_Create(&page_range);
      FSDK_PDFDoc_ExportToFDF(pdf_doc, fdf_doc, e_FSAnnots, page_range, &return_result);
      FSDK_FDFDoc_SaveAs(fdf_doc, output_fdf, &return_result);
      printf("Export a fdf file.\n");
      FSDK_Range_Release(page_range);
      FSDK_FDFDoc_Release(fdf_doc);
      FSDK_PDFDoc_Release(pdf_doc);
    }
    {
      // Export XFDF file
      wchar_t input_file[MAX_FILE_PATH] = { 0 };
      wchar_t output_xfdf[MAX_FILE_PATH] = { 0 };
      FS_PDFDOC_HANDLE pdf_doc = NULL;
      FS_FDFDOC_HANDLE fdf_doc = NULL;
      FS_RANGE_HANDLE page_range = NULL;
      FS_BOOL return_result;
 
      swprintf_s(input_file, MAX_FILE_PATH, L"%lsAnnotationDataExport.pdf", input_path);
      swprintf_s(output_xfdf, MAX_FILE_PATH, L"%lsAnnotationDataExport_xfdf.xfdf", output_directory);
 
      FSDK_PDFDoc_Create0(input_file, &pdf_doc);
      error_code = FSDK_PDFDoc_Load(pdf_doc, NULL);
      if (error_code != e_FSErrSuccess) {
        wprintf(L"The Doc [%ls] Error: %d\n", input_file, error_code);
        return 1;
      }
 
      FSDK_FDFDoc_Create0(e_FSXFDF, &fdf_doc);
 
      FSDK_Range_Create(&page_range);
 
      FSDK_PDFDoc_ExportToFDF(pdf_doc, fdf_doc, e_FSAnnots, page_range, &return_result);
      FSDK_FDFDoc_SaveAs(fdf_doc, output_xfdf, &return_result);
      printf("Export an xfdf file.\n");
 
      FSDK_Range_Release(page_range);
      FSDK_FDFDoc_Release(fdf_doc);
      FSDK_PDFDoc_Release(pdf_doc);
    }
  }
  FSDK_Library_Release();
  return err_ret;
}  

Objective-C language 

#include "FSPDFObjC.h"
 
NSString *input_path = @"../input_files/";
NSString *output_path = @"../output_files/";
 
NSString* sn = @"VQv/htdRKu1rjhk90MTKF+/aCs9DfQRtBF4SNc0GwPlOX8ualGeTJg==";
NSString* key = @"ezJvj18mtBp399sXJVWofHfLliq8pf9v7BMskv+zLerFJVWviGEfZ+kCs+nfwjfVTthVUzkIj1qr9k37wnS1f2YSW3jOA/0EfK806dB9bigN097uSthk6eeafc30XHVMSGerLpzYziDW9zL/1oMTZ80YX6Trs0BWaif611VZm/SJ4PkaRzN0sCCOUUKXl8+dUun4a2hGn1fxneYEh8ROSeTXRbNA0ltpL6d4xs+X4o5p+sJBSDjgoProtzbAeTELgzieFZtk/2qbfUzT0Zi3hIIFde/UHOEemCp88Une4k87YS8hK3n5H0s1EJLW8P4QUKdYIFU+r6iLZ3SVrT61l1IBvE0DoO+cUd3GpcA3XnAUamIDwtLzg6c7atTPKDnb3lM25K+LB7/1HIUTMZHmvE2JCuQM7J/c7hkYGoD4KvKl3UIgbGYCyEjQabHNVE0FiSWX7ZlE2RAA6DlzIxWhwhAbz4pkVggWTesZP+vVclWC2Vypebbt2zx+qTVWMNQTsXD9MKUMdOqRAvuytH/Hqw9LE3AoTR6oJE9FWw6NzkXAS2k0ylHHE7kdJh3z3MrCHblR3z1T28O1Me8WbGl1+1WaVEod5gowIsn/ut1/2MLYzyo3DCgDkNr+2nzeZSKIx9J92z6mEwexxlTfOea8cabcYO4QLbyCb+CNq5EhqsVv6UVMDkex6s9YA26WEjj4/Vp8hCKUBavqqqFOJtg8m7EDQQ9G0ZtfX4gLuQulsbluuxynUHrIF0BLdKfKLdAZav3+9lFiA2ORRwLWysYqASdCv4jE4oj2zpTIq4t4iFeOx2/PwJ8fuTMHZkihAbeKolrFq9MZCh6kUg0o1M6kowt8AHPhSG0I1Ng/OCz1lA6m2eBWu2dXtL4P7OJCs447MYgHPhc9k/K+LJvfzOBovIfogZKdwEWbN86ItHVQTDLs3Qlx0qn4tpTJfBcZJUpi03satDiaipWhVNznvr9kDKlGx1VBu+ciqB8MDaC/MGAVeRl+ByqT5t2L6I8koG58o/sNRjMZ1Y5CfFIl5J7j3J2+acYW9vPNrkLgbD5DPAQGhJUlo2K59v62/HrEJDXzz2ue6FEbIASS37X9JdBWqoMav5Ti1dlNpDONbprffNqVyiGromUvTGT4Y9KJWqBCou/p1KzW9s4rCuwzONuKNF7nOqx0lGsCQrL8pkvDwDKJ94lx5oyHZdHNS2xMh98qPo1V7nc=";
 
int main(int argc, const char * argv[]) {
     
    @autoreleasepool {
        NSString *output_directory = [output_path stringByAppendingString:@"fdf/"];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        BOOL isDir = NO;
        if ([fileManager fileExistsAtPath:output_directory isDirectory:&isDir]) {
            if (!isDir) {
                NSError *error = nil;
                if (![fileManager removeItemAtPath:output_directory error:&error] || error) {
                    return -1;
                }
                if (![fileManager createDirectoryAtPath:output_directory withIntermediateDirectories:YES attributes:nil error:&error] || error) {
                    return -1;
                }
            }
        } else {
            NSError *error = nil;
            if (![fileManager createDirectoryAtPath:output_directory withIntermediateDirectories:YES attributes:nil error:&error] || error) {
                return -1;
            }
        }
     
        // Initialize library
        FSErrorCode errorCode = [FSLibrary initialize:sn key:key];
        if (errorCode != FSErrSuccess) {
            NSLog(@"Library Initialize Error: %ld", errorCode);
            return -1;
        }
         
        bool is_run_success = false;
        @try {
            {
                // Import FDF file
                NSString* input_pdf_name = @"AboutFoxit.pdf";
                NSString* input_pdf_path = [input_path stringByAppendingPathComponent:input_pdf_name];
                FSPDFDoc* doc = [[FSPDFDoc alloc] initWithPath:input_pdf_path];
                FSErrorCode errorCode = [doc load:nil];
                if (errorCode != FSErrSuccess) {
                    NSLog(@"The Doc [%@] Error: %ld", input_pdf_path, errorCode);
                    return -1;
                }
                 
                NSString* input_fdf_name = @"AnnotationData.fdf";
                NSString* input_fdf_path = [input_path stringByAppendingPathComponent:input_fdf_name];
                FSFDFDoc* fdf_doc = [[FSFDFDoc alloc] initWithPath:input_fdf_path];
                NSLog(@"Import annotations from fdf with file.");
                [doc importFromFDF:fdf_doc types:FSPDFDocAnnots page_range:[FSRange new]];
 
                NSString* save_pdf_path = [output_directory stringByAppendingPathComponent:@"AboutFoxit_importFDF.pdf"];
                [doc saveAs:save_pdf_path save_flags:FSPDFDocSaveFlagNoOriginal];
            }
            {
                // Export FDF file
                NSString* input_pdf_name = @"AnnotationDataExport.pdf";
                NSString* input_pdf_path = [input_path stringByAppendingPathComponent:input_pdf_name];
                FSPDFDoc* doc = [[FSPDFDoc alloc] initWithPath:input_pdf_path];
                FSErrorCode errorCode = [doc load:nil];
                if (errorCode != FSErrSuccess) {
                    NSLog(@"The Doc [%@] Error: %ld", input_pdf_path, errorCode);
                    return -1;
                }
                 
                FSFDFDoc* fdf_doc = [[FSFDFDoc alloc] initWithType:FSFDFDocFDF];
                [doc exportToFDF:fdf_doc types:FSPDFDocAnnots page_range:[FSRange new]];
                 
                NSString* save_fdf_path = [output_directory stringByAppendingPathComponent:@"AnnotationDataExport_fdf.fdf"];
                [fdf_doc saveAs:save_fdf_path];
                NSLog(@"Export a fdf file.");
            }
            {
                // Export XFDF file
                NSString* input_pdf_name = @"AnnotationDataExport.pdf";
                NSString* input_pdf_path = [input_path stringByAppendingPathComponent:input_pdf_name];
                FSPDFDoc* doc = [[FSPDFDoc alloc] initWithPath:input_pdf_path];
                FSErrorCode errorCode = [doc load:nil];
                if (errorCode != FSErrSuccess) {
                    NSLog(@"The Doc [%@] Error: %ld", input_pdf_path, errorCode);
                    return -1;
                }
                 
                FSFDFDoc* fdf_doc = [[FSFDFDoc alloc] initWithType:FSFDFDocXFDF];
                [doc exportToFDF:fdf_doc types:FSPDFDocAnnots page_range:[FSRange new]];
                 
                NSString* save_xfdf_path = [output_directory stringByAppendingPathComponent:@"AnnotationDataExport_xfdf.xfdf"];
                [fdf_doc saveAs:save_xfdf_path];
                NSLog(@"Export an xfdf file.");
            }
            NSLog(@"FDF test.");
            is_run_success = true;
        }
        @catch (NSException *e) {
            NSLog(@"Unknown Exception, %@", e);
        }
        return is_run_success? 0 : 1;
    }
     
}

Updated on October 16, 2023

Was this article helpful?
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: