Export form data from PDF

9 Jan 20265 minutes to read

The PDF Viewer component supports exporting and importing form field data using the importFormFields, exportFormFields, and exportFormFieldsAsObject methods in the following formats:

Export as FDF

Using the exportFormFields method, the form field data can be exported in the specified data format. This method accepts two parameters:

  • The first one must be the destination path for the exported data. If the path is not specified, it will ask for the location while exporting.
  • The second parameter should be the format type of the form data.

The following example exports and imports form field data as FDF.

<button id="exportFdf">Export FDF</button>
import { PdfViewer, FormFieldDataFormat, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, FormFields, FormDesigner } from '@syncfusion/ej2-pdfviewer';

PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, FormFields, FormDesigner);

const viewer = new PdfViewer({
  documentPath: 'https://cdn.syncfusion.com/content/pdf/form-designer.pdf',
  // serviceUrl: 'https://document.syncfusion.com/web-services/pdf-viewer/api/pdfviewer/' // Server-backed
  resourceUrl: "https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib"
});
viewer.appendTo('#pdfViewer');

document.getElementById('exportFdf')!.addEventListener('click', () => {
  // Data must be the desired path or file name for the exported document.
  viewer.exportFormFields('ExportedData', FormFieldDataFormat.Fdf);
});

Export as XFDF

The following example exports form field data as XFDF.

<button id="exportXfdf">Export XFDF</button>
import { PdfViewer, FormFieldDataFormat, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, FormFields, FormDesigner } from '@syncfusion/ej2-pdfviewer';

PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, FormFields, FormDesigner);

const viewer = new PdfViewer({
  documentPath: 'https://cdn.syncfusion.com/content/pdf/form-designer.pdf',
  // serviceUrl: 'https://document.syncfusion.com/web-services/pdf-viewer/api/pdfviewer/' // Server-backed
  resourceUrl: "https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib"
});
viewer.appendTo('#pdfViewer');

document.getElementById('exportXfdf')!.addEventListener('click', () => {
  // Data must be the desired path or file name for the exported document.
  viewer.exportFormFields('FormData', FormFieldDataFormat.Xfdf);
});

Export as JSON

The following example exports form field data as JSON.

<button id="exportJson">Export JSON</button>
import { PdfViewer, FormFieldDataFormat, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, FormFields, FormDesigner } from '@syncfusion/ej2-pdfviewer';

PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, FormFields, FormDesigner);

const viewer = new PdfViewer({
  documentPath: 'https://cdn.syncfusion.com/content/pdf/form-designer.pdf',
  // serviceUrl: 'https://document.syncfusion.com/web-services/pdf-viewer/api/pdfviewer/' // Server-backed
  resourceUrl: "https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib"
});
viewer.appendTo('#pdfViewer');

document.getElementById('exportJson')!.addEventListener('click', () => {
  // Data must be the desired path or file name for the exported document.
  viewer.exportFormFields('FormData', FormFieldDataFormat.Json);
});

Export as Object

Export the form data to a JavaScript object for custom persistence (database, API, or client storage).
The following example exports and imports form field data as Object.

<button id="exportObj">Export Object</button>
import { PdfViewer, FormFieldDataFormat, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, FormFields, FormDesigner } from '@syncfusion/ej2-pdfviewer';

PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, FormFields, FormDesigner);

const viewer = new PdfViewer({
  documentPath: 'https://cdn.syncfusion.com/content/pdf/form-designer.pdf',
  // serviceUrl: 'https://document.syncfusion.com/web-services/pdf-viewer/api/pdfviewer/' // Server-backed
  resourceUrl: "https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib"
});
viewer.appendTo('#pdfViewer');

let exportedData: object | undefined;
document.getElementById('exportObj')!.addEventListener('click', () => {
  viewer.exportFormFieldsAsObject(FormFieldDataFormat.Fdf).then(data => {
    exportedData = data; // Save or send to server
    console.log('Exported object:', exportedData);
  });

  // Alternative formats:
  // viewer.exportFormFieldsAsObject(FormFieldDataFormat.Xfdf).then(...)
  // viewer.exportFormFieldsAsObject(FormFieldDataFormat.Json).then(...)
});

Common use cases

  • Persist user-entered data to your server without modifying the original PDF.
  • Export as JSON for easy integration with REST APIs.
  • Export as FDF/XFDF for interoperability with other PDF tools.
  • Export as object to combine with your app state and store securely.
  • Automate exports after validation using validateFormFields.

View Sample on GitHub

See also