|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Export form data in the TypeScript PDF Viewer component | Syncfusion |
| 4 | +description: Learn how to export PDF form field data (FDF, XFDF, JSON, and as an object) using the Syncfusion TypeScript PDF Viewer component. |
| 5 | +platform: document-processing |
| 6 | +control: PDF Viewer |
| 7 | +documentation: ug |
| 8 | +--- |
| 9 | + |
| 10 | +# Export form data from PDF |
| 11 | + |
| 12 | +The PDF Viewer provides APIs to export the values of interactive form fields from the loaded PDF. You can export to FDF, XFDF, JSON, or as a JavaScript object for custom processing. |
| 13 | + |
| 14 | +Supported APIs: |
| 15 | +- exportFormFields(pathOrFileName, format) |
| 16 | +- exportFormFieldsAsObject(format) → Promise<object> |
| 17 | + |
| 18 | +Note: When using the server-backed viewer, set serviceUrl before exporting. |
| 19 | + |
| 20 | +## Export as FDF |
| 21 | + |
| 22 | +```html |
| 23 | +<button id="exportFdf">Export FDF</button> |
| 24 | +``` |
| 25 | +```ts |
| 26 | +import { PdfViewer, FormFieldDataFormat, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, FormFields, FormDesigner } from '@syncfusion/ej2-pdfviewer'; |
| 27 | + |
| 28 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, FormFields, FormDesigner); |
| 29 | + |
| 30 | +const viewer = new PdfViewer({ |
| 31 | + documentPath: 'https://cdn.syncfusion.com/content/pdf/form-designer.pdf', |
| 32 | + // serviceUrl: 'https://document.syncfusion.com/web-services/pdf-viewer/api/pdfviewer/' // Server-backed |
| 33 | + resourceUrl: "https://cdn.syncfusion.com/ej2/31.1.23/dist/ej2-pdfviewer-lib" |
| 34 | +}); |
| 35 | +viewer.appendTo('#PdfViewer'); |
| 36 | + |
| 37 | +document.getElementById('exportFdf')!.addEventListener('click', () => { |
| 38 | + // Data must be the desired path or file name for the exported document. |
| 39 | + viewer.exportFormFields('ExportedData', FormFieldDataFormat.Fdf); |
| 40 | +}); |
| 41 | +``` |
| 42 | + |
| 43 | +## Export as XFDF |
| 44 | + |
| 45 | +```ts |
| 46 | +<button id="exportXfdf">Export XFDF</button> |
| 47 | +<div id="pdfViewer" style="height: 640px; width: 100%"></div> |
| 48 | + |
| 49 | +import { PdfViewer, FormFieldDataFormat, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection } from '@syncfusion/ej2-pdfviewer'; |
| 50 | + |
| 51 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection); |
| 52 | + |
| 53 | +const viewer = new PdfViewer({ documentPath: 'https://cdn.syncfusion.com/content/pdf/form-designer.pdf' }); |
| 54 | +viewer.appendTo('#pdfViewer'); |
| 55 | + |
| 56 | +document.getElementById('exportXfdf')!.addEventListener('click', () => { |
| 57 | + viewer.exportFormFields('FormData', FormFieldDataFormat.Xfdf); |
| 58 | +}); |
| 59 | +``` |
| 60 | + |
| 61 | +## Export as JSON |
| 62 | + |
| 63 | +```ts |
| 64 | +<button id="exportJson">Export JSON</button> |
| 65 | +<div id="pdfViewer" style="height: 640px; width: 100%"></div> |
| 66 | + |
| 67 | +import { PdfViewer, FormFieldDataFormat, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection } from '@syncfusion/ej2-pdfviewer'; |
| 68 | + |
| 69 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection); |
| 70 | + |
| 71 | +const viewer = new PdfViewer({ documentPath: 'https://cdn.syncfusion.com/content/pdf/form-designer.pdf' }); |
| 72 | +viewer.appendTo('#pdfViewer'); |
| 73 | + |
| 74 | +document.getElementById('exportJson')!.addEventListener('click', () => { |
| 75 | + viewer.exportFormFields('FormData', FormFieldDataFormat.Json); |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +## Export as Object |
| 80 | + |
| 81 | +Export the form data to a JavaScript object for custom persistence (database, API, or client storage). |
| 82 | + |
| 83 | +```ts |
| 84 | +<button id="exportObj">Export Object</button> |
| 85 | +<div id="pdfViewer" style="height: 640px; width: 100%"></div> |
| 86 | + |
| 87 | +import { PdfViewer, FormFieldDataFormat, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection } from '@syncfusion/ej2-pdfviewer'; |
| 88 | + |
| 89 | +PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection); |
| 90 | + |
| 91 | +const viewer = new PdfViewer({ documentPath: 'https://cdn.syncfusion.com/content/pdf/form-designer.pdf' }); |
| 92 | +viewer.appendTo('#pdfViewer'); |
| 93 | + |
| 94 | +let exportedData: object | undefined; |
| 95 | +document.getElementById('exportObj')!.addEventListener('click', () => { |
| 96 | + viewer.exportFormFieldsAsObject(FormFieldDataFormat.Fdf).then(data => { |
| 97 | + exportedData = data; // Save or send to server |
| 98 | + console.log('Exported object:', exportedData); |
| 99 | + }); |
| 100 | + |
| 101 | + // Alternative formats: |
| 102 | + // viewer.exportFormFieldsAsObject(FormFieldDataFormat.Xfdf).then(...) |
| 103 | + // viewer.exportFormFieldsAsObject(FormFieldDataFormat.Json).then(...) |
| 104 | +}); |
| 105 | +``` |
| 106 | + |
| 107 | +## Common use cases |
| 108 | + |
| 109 | +- Persist user-entered data to your server without modifying the original PDF. |
| 110 | +- Export as JSON for easy integration with REST APIs. |
| 111 | +- Export as FDF/XFDF for interoperability with other PDF tools. |
| 112 | +- Export as object to combine with your app state and store securely. |
| 113 | +- Automate exports after validation using validateFormFields. |
0 commit comments