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). Learn more about our Web SDK and PDF Data Extraction SDK Capabilities.
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
9
10
11
12
13
14
15
16
17 const PDFNet = exports.Core.PDFNet;
18
19 const createFDFFromXFDFURL = url =>
20 new Promise((resolve, reject) => {
21 const xhttp = new XMLHttpRequest();
22
23 xhttp.onreadystatechange = function() {
24 if (this.readyState === this.DONE) {
25 if (xhttp.status === 200) {
26 const data = xhttp.responseText;
27 PDFNet.FDFDoc.createFromXFDF(data).then(
28 fdfdoc => {
29 resolve(fdfdoc);
30 },
31 e => {
32 reject(e);
33 }
34 );
35 } else {
36 reject('Request for URL ' + url + ' received incorrect HTTP response code ' + xhttp.status);
37 }
38 }
39 };
40 xhttp.open('GET', url, true);
41 xhttp.send();
42 });
43
44 exports.runFDFTest = () => {
45 const main = async () => {
46 console.log('Beginning FDF Test.');
47 const inputUrl = '../TestFiles/';
48
49 // Import XFDF into FDF, then update adjust the PDF annotations to match the FDF
50 try {
51 // Annotations
52 console.log('Import annotations from XFDF to FDF.');
53 const fdfDoc = await createFDFFromXFDFURL(inputUrl + 'form1_annots.xfdf');
54
55 const doc = await PDFNet.PDFDoc.createFromURL(inputUrl + 'form1.pdf');
56 doc.initSecurityHandler();
57
58 console.log('Update annotations from fdf');
59 doc.fdfUpdate(fdfDoc);
60
61 const docbuf = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
62 saveBufferAsPDFDoc(docbuf, 'form1_with_annots.pdf');
63 console.log('Done sample');
64 } catch (err) {
65 console.log(err);
66 }
67
68 try {
69 console.log('Extract annotations from PDF.');
70 const doc = await PDFNet.PDFDoc.createFromURL(`${inputUrl}form1.pdf`);
71 doc.initSecurityHandler();
72
73 // extract fdf
74 const fdfDoc = await doc.fdfExtract(PDFNet.PDFDoc.ExtractFlag.e_both);
75 // save as xfdf
76 const xfdfStr = await fdfDoc.saveAsXFDFAsString();
77 console.log('XFDF extracted from form1.pdf');
78 console.log(xfdfStr);
79
80 console.log('Done sample');
81 } catch (err) {
82 console.log(err);
83 }
84
85 try {
86 console.log('Extract existing fields from PDF.');
87 const doc = await PDFNet.PDFDoc.createFromURL(`${inputUrl}form1.pdf`);
88 doc.initSecurityHandler();
89
90 for (const itr = await doc.getFieldIteratorBegin(); await itr.hasNext(); itr.next()) {
91 const field = await itr.current();
92 console.log(await field.getName());
93 }
94 console.log('Done sample');
95 } catch (err) {
96 console.log(err);
97 }
98 };
99
100 // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
101 PDFNet.runWithCleanup(main);
102 };
103})(window);
104// eslint-disable-next-line spaced-comment
105//# sourceURL=FDFTest.js
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales