XFDF - Forms Data Format (FDF) Import - Sample Code

Requirements

Sample JavaScript code for using Apryse SDK to programmatically merge forms data with the PDF in order to fill forms, or to extract form field data from the PDF. Apryse SDK has full support for Forms Data Format (FDF).

This sample is utilizing the option to run code programmatically, without the Viewer. Need functionality in another language? Check out FDF with Server SDK.

Learn more about our Web SDK and PDF Data Extraction SDK Capabilities.

Implementation steps

To merge data and fill forms in a PDF with WebViewer SDK:

Step 1: Follow get started in your preferred web stack for WebViewer
Step 2: Follow set up to implement Full API code without viewer
Step 3: Add the sample code provided in this guide

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6(exports => {
7
8 const PDFNet = exports.Core.PDFNet;
9
10 const createFDFFromXFDFURL = url =>
11 new Promise((resolve, reject) => {
12 const xhttp = new XMLHttpRequest();
13
14 xhttp.onreadystatechange = function() {
15 if (this.readyState === this.DONE) {
16 if (xhttp.status === 200) {
17 const data = xhttp.responseText;
18 PDFNet.FDFDoc.createFromXFDF(data).then(
19 fdfdoc => {
20 resolve(fdfdoc);
21 },
22 e => {
23 reject(e);
24 }
25 );
26 } else {
27 reject('Request for URL ' + url + ' received incorrect HTTP response code ' + xhttp.status);
28 }
29 }
30 };
31 xhttp.open('GET', url, true);
32 xhttp.send();
33 });
34
35 exports.runFDFTest = () => {
36 const main = async () => {
37 console.log('Beginning FDF Test.');
38 const inputUrl = '../TestFiles/';
39
40 // Import XFDF into FDF, then update adjust the PDF annotations to match the FDF
41 try {
42 // Annotations
43 console.log('Import annotations from XFDF to FDF.');
44 const fdfDoc = await createFDFFromXFDFURL(inputUrl + 'form1_annots.xfdf');
45
46 const doc = await PDFNet.PDFDoc.createFromURL(inputUrl + 'form1.pdf');
47 doc.initSecurityHandler();
48
49 console.log('Update annotations from fdf');
50 doc.fdfUpdate(fdfDoc);
51
52 const docbuf = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
53 saveBufferAsPDFDoc(docbuf, 'form1_with_annots.pdf');
54 console.log('Done sample');
55 } catch (err) {
56 console.log(err);
57 }
58
59 try {
60 console.log('Extract annotations from PDF.');
61 const doc = await PDFNet.PDFDoc.createFromURL(`${inputUrl}form1.pdf`);
62 doc.initSecurityHandler();
63
64 // extract fdf
65 const fdfDoc = await doc.fdfExtract(PDFNet.PDFDoc.ExtractFlag.e_both);
66 // save as xfdf
67 const xfdfStr = await fdfDoc.saveAsXFDFAsString();
68 console.log('XFDF extracted from form1.pdf');
69 console.log(xfdfStr);
70
71 console.log('Done sample');
72 } catch (err) {
73 console.log(err);
74 }
75
76 try {
77 console.log('Extract existing fields from PDF.');
78 const doc = await PDFNet.PDFDoc.createFromURL(`${inputUrl}form1.pdf`);
79 doc.initSecurityHandler();
80
81 for (const itr = await doc.getFieldIteratorBegin(); await itr.hasNext(); itr.next()) {
82 const field = await itr.current();
83 console.log(await field.getName());
84 }
85 console.log('Done sample');
86 } catch (err) {
87 console.log(err);
88 }
89 };
90
91 // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
92 PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY');
93 };
94})(window);
95// eslint-disable-next-line spaced-comment
96//# sourceURL=FDFTest.js

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales