How to import a template-based FDF to PDF
Contents
Support environment
Platform: Windows, Mac, Linux
Programming Language: C++, Dotnet, Dotnetcore, Java, Python,Windows C, Objective-C
License Key requirement: Standard
SDK Version: Foxit PDF SDK 8.4 or later
What’s template-based FDF
FDF (Forms Data Format) is a kind of file format, used for interactive form data. If these FDF data are customized into a template, and then imported into the target PDF file according to the specified content of the template.
This template contains one or more source PDF files that need to be merged, and the required fields in these files are selected, and these fields can also be adjusted on the basis of the original ones. Therefore, Template-based FDF will be more flexible and more orderly imported into the target PDF file.
Template-based FDF file’s structure
An Template-based FDF file shall be structured in essentially the same way as a PDF file but contains only those elements required for the export and import of interactive form data.
The file is displayed as shown in the figure:
Its structure consists of the following parts:
• A one-line header identifying the version number of the PDF specification to which the file conforms,As follows:
• A body containing the objects that make up the content of the file and Template objects to be imported,As follows:
These one or more template objects contain the path of the PDF file to be imported and the form information to be imported.
• Need to import source PDF file path,As follows:
• Specify the form data that needs to be imported, As follows:
Where /T represents the form name, As follows:
/V represents the text that the form needs to display, As follows:
Import function introduction
Foxit PDF SDK now supports to import form fields in FDF template. The page associated with the FDF template will be inserted into the end of document.
Next,This article will tell you how to import a template-based FDF to PDF.
How to import a template-based FDF to PDF
Foxit PDF SDK provides a fdf demo located in the “\examples\simple_demo\fdf” folder to show you how to use Foxit PDF SDK to import form fields in FDF template.You can use this DEMO to import a template-based FDF to PDF.
Before running the demo, you need to convert the prepared FDF template, and then call the following code:
C++ language
#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"
...
// Import Template-based FDF file
pdf::PDFDoc pdf_doc(input_file);
ErrorCode error_code = pdf_doc.Load();
fdf::FDFDoc fdf_doc(fdf_file);
pdf_doc.ImportFromFDF(fdf_doc, PDFDoc::e_Annots);
pdf_doc.SaveAs(output_file, PDFDoc::e_SaveFlagNoOriginal);
...
C# language
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
using System.IO;
using System.Runtime.InteropServices;
...
// Import Template-based FDF file
using (PDFDoc pdf_doc = new PDFDoc(input_file))
{
error_code = pdf_doc.Load(null);
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 foxit.common.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);
}
}
...
Python language
import os
import sys
import site
if _PYTHON2_:
site.addsitedir('../../../')
from FoxitPDFSDKPython2 import *
else:
from FoxitPDFSDKPython3 import *
...
// Import Template-based FDF file
pdf_doc = PDFDoc(input_file)
error_code = pdf_doc.Load("")
fdf_doc = FDFDoc(fdf_file)
pdf_doc.ImportFromFDF(fdf_doc, PDFDoc.e_Annots)
pdf_doc.SaveAs(output_file, PDFDoc.e_SaveFlagNoOriginal)
...
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 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;
...
// Import Template-based FDF file
PDFDoc pdf_doc = new PDFDoc(input_file);
error_code = pdf_doc.load(null);
FDFDoc fdf_doc = new FDFDoc(fdf_file);
pdf_doc.importFromFDF(fdf_doc, e_Annots, empty_range);
pdf_doc.saveAs(output_file, e_SaveFlagNoOriginal);
...
C language
#include <iostream>
#include<direct.h>
#include<string>
#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"
...
// Import Template-based FDF file
FS_PDFDOC_HANDLE pdf_doc;
FSDK_PDFDoc_Create0(input_file.c_str(), pdf_doc);
FSErrorCode error_code = FSDK_PDFDoc_Load(pdf_doc, NULL);
FS_FDFDOC_HANDLE fdf_doc;
FSDK_FDFDoc_Create1(fdf_file.c_str(), fdf_doc);
FS_RANGE_HANDLE page_range;
FSDK_Range_Create(page_range);
FS_BOOL return_result;
FSDK_PDFDoc_ImportFromFDF(pdf_doc, fdf_doc, e_FSAnnots, page_range, return_result);
FS_BOOL isSave;
FSDK_PDFDoc_SaveAs(pdf_doc, output_file.c_str(), e_FSSaveFlagNoOriginal, isSave);
FSDK_FDFDoc_Release(fdf_doc);
...
Objective-C language
#include "FSPDFObjC.h"
...
// Import Template-based FDF file
FSPDFDoc* doc = [[FSPDFDoc alloc] initWithPath:input_pdf_path];
FSErrorCode errorCode = [doc load:nil];
FSFDFDoc* fdf_doc = [[FSFDFDoc alloc] initWithPath:input_fdf_path];
[doc importFromFDF:fdf_doc types:FSPDFDocAnnots page_range:[FSRange new]];
[doc saveAs:save_pdf_path save_flags:FSPDFDocSaveFlagNoOriginal];
...
Result compare
The Form data in the template is imported into the generated PDF, and the PDF of each template is imported into a new PDF in turn,As follow:
This completes the import a template-based FDF to PDF.
Updated on October 16, 2023