Some test text!

menu

PDF form fill and form data extraction in Javascript

Sample Javascript code for using PDFTron SDK to programmatically merge forms data with the PDF in order to fill forms, or to extract form field data from the PDF. PDFTron SDK has full support for Forms Data Format (FDF). Learn more about our Javascript PDF Library and PDF Form Filler SDK.

Get StartedSamplesDownload

To run this sample, get started with a free trial of PDFTron SDK.

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------


const { PDFNet } = require('../../lib/pdfnet.js');

((exports) => {

  var createFDFFromXFDFURL = url => new Promise((resolve, reject) => {
    const xhttp = new XMLHttpRequest();

    xhttp.onreadystatechange = function() {
      if (this.readyState === this.DONE) {
        if (xhttp.status === 200) {
          const data = xhttp.responseText;
          PDFNet.FDFDoc.createFromXFDF(data).then((fdfdoc) => {
            resolve(fdfdoc);
          }, (e) => {
            reject(e);
          });
        } else {
          reject('Request for URL ' + url + ' received incorrect HTTP response code ' + xhttp.status);
        }
      }
    };
    xhttp.open('GET', url, true);
    xhttp.send();
  });


  exports.runFDFTest = () => {
    const main = async() => {
      console.log('Beginning FDF Test.');
      const inputUrl = '../TestFiles/';

      // Import XFDF into FDF, then update adjust the PDF annotations to match the FDF
      try {
        // Annotations
        console.log('Import annotations from XFDF to FDF.');
        const fdfDoc = await PDFNet.FDFDoc.createFromXFDF(inputUrl + 'form1_annots.xfdf');

        const doc = await PDFNet.PDFDoc.createFromFilePath(inputUrl + 'form1.pdf');
        doc.initSecurityHandler();

        console.log('Update annotations from fdf');
        doc.fdfUpdate(fdfDoc);

        await doc.save('../TestFiles/Output/form1_with_annots.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
        console.log('Done sample');
      } catch (err) {
        console.log(err);
      }

      try {
        console.log('Extract annotations from PDF.');
        const doc = await PDFNet.PDFDoc.createFromFilePath(`${inputUrl}form1.pdf`);
        doc.initSecurityHandler();

        // extract fdf
        const fdfDoc = await doc.fdfExtract(PDFNet.SDFDoc.SaveOptions.e_both);
        // save as xfdf
        const xfdfStr = await fdfDoc.saveAsXFDFAsString();
        console.log('XFDF extracted from form1.pdf');
        console.log(xfdfStr);

        console.log('Done sample');
      } catch (err) {
        console.log(err);
      }

      try {
        console.log('Extract existing fields from PDF.');
        const doc = await PDFNet.PDFDoc.createFromFilePath(`${inputUrl}form1.pdf`);
        doc.initSecurityHandler();

        for (const itr = await doc.getFieldIteratorBegin(); await itr.hasNext(); itr.next()) {
          const field = await itr.current();
          console.log(await field.getName());
        }
        console.log('Done sample');
      } catch (err) {
        console.log(err);
      }
    };

    // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
    PDFNet.runWithCleanup(main).then(function(){PDFNet.shutdown();});
  };
  exports.runFDFTest();
})(exports);
// eslint-disable-next-line spaced-comment
//# sourceURL=FDFTest.js
close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

Join our live demo to learn about use cases & capabilities for WebViewer

Learn more
close