Section:

Flatten PDF forms using Javascript

Annotation or form flattening refers to the operation that changes annotations (such as markup, widgets, 3D models, etc.) into a static area that is part of the PDF document, just like the other text and images in the document.

Forms share a relationship with annotations because the visual display of a form is a widget annotation. The process of flattening annotations therefore can optionally flatten forms as well. By flattening and merging existing annotation appearances with page content, the original annotations are deleted from the PDF pages.

Note that it is not possible to undo the flatten operation. An alternative approach to annotation or form flattening is to set the annotation to ReadOnly. The ReadOnly property on the Annotation class enables this modification. If it's read only then it can't be edited or deleted.

To flatten annotations, you can perform this when getting the PDF file data or while downloading the PDF file locally. There is also the option to flatten select annotations.

Get PDF data with flattened annotations

Use the getFileData method and the flatten option for the annotations to be flattened in the document output when getting the data for uploading to a server.

flatten option

A flag that is only useful when the xfdfString option is used. If true all the annotations in the saved document will be flattened.

1WebViewer({
2 fullAPI: true,
3 // other constructor options
4}
5
6// config.js
7const { documentViewer, annotationManager } = instance.Core;
8
9document.getElementById('myBtn').addEventListener('click', async () => {
10 const doc = documentViewer.getDocument();
11 const xfdfString = await annotationManager.exportAnnotations();
12 const options = { xfdfString, flatten: true };
13 const data = await doc.getFileData(options);
14 const arr = new Uint8Array(data);
15 const blob = new Blob([arr], { type: 'application/pdf' });
16 // upload blob to your server
17});

Download PDF locally with flattened annotations

Use the downloadPdf method and the flatten option for the annotations to be flattened in the document output when downloading the PDF locally.

flatten option

Whether or not to flatten all the annotations in the downloaded document. Only useful if fullAPI is enabled and either `xfdfString` or `includeAnnotations` is used.

1WebViewer({
2 fullAPI: true,
3 // other constructor options
4}
5
6// config.js
7document.getElementById('myBtn').addEventListener('click', () => {
8 // download pdf with all annotations flattened
9 instance.UI.downloadPdf({
10 includeAnnotations: true,
11 flatten: true,
12 });
13});

Flatten annotations in the viewer

Use flattenAnnotations method to flatten all annotations into a document.

1WebViewer({
2 fullAPI: true,
3 // other constructor options
4}
5
6// config.js
7const { documentViewer, PDFNet, annotationManager } = instance.Core;
8
9document.getElementById('myBtn').addEventListener('click', async () => {
10 await PDFNet.initialize();
11 const doc = await documentViewer.getDocument().getPDFDoc();
12
13 // export annotations from the document
14 const annots = await annotationManager.exportAnnotations();
15
16 // Run PDFNet methods with memory management
17 await PDFNet.runWithCleanup(async () => {
18
19 // lock the document before a write operation
20 // runWithCleanup will auto unlock when complete
21 doc.lock();
22
23 // import annotations to PDFNet
24 const fdf_doc = await PDFNet.FDFDoc.createFromXFDF(annots);
25 await doc.fdfUpdate(fdf_doc);
26
27 // flatten all annotations in the document
28 await doc.flattenAnnotations();
29
30 // or optionally only flatten forms
31 // await doc.flattenAnnotations(true);
32
33 // clear the original annotations
34 annotationManager.deleteAnnotations(annotationManager.getAnnotationsList());
35
36 // optionally only clear widget annotations if forms were only flattened
37 // const widgetAnnots = annots.filter(a => a instanceof Annotations.WidgetAnnotation);
38 // annotationManager.deleteAnnotations(widgetAnnots);
39 });
40
41 // clear the cache (rendered) data with the newly updated document
42 documentViewer.refreshAll();
43
44 // Update viewer to render with the new document
45 documentViewer.updateView();
46
47 // Refresh searchable and selectable text data with the new document
48 documentViewer.getDocument().refreshTextData();
49});

Flatten select annotations

Use the flatten method on annotation objects to flatten select annotations into the document.

1WebViewer({
2 fullAPI: true,
3 // other constructor options
4}
5
6// config.js
7const { documentViewer, PDFNet, annotManager } = instance.Core;
8
9document.getElementById('myBtn').addEventListener('click', async () => {
10 await PDFNet.initialize();
11 const doc = await documentViewer.getDocument().getPDFDoc();
12
13 // export annotations from the document
14 const annots = await annotManager.exportAnnotations();
15
16 // Run PDFNet methods with memory management
17 await PDFNet.runWithCleanup(async () => {
18
19 // lock the document before a write operation
20 // runWithCleanup will auto unlock when complete
21 doc.lock();
22
23 // import annotations to PDFNet
24 const fdf_doc = await PDFNet.FDFDoc.createFromXFDF(annots);
25 await doc.fdfUpdate(fdf_doc);
26
27 const page = await doc.getPage(1);
28 const annotation = await page.getAnnot(0);
29 await annotation.flatten(page); //flatten this annotation
30
31 // clear the original annotations
32 annotManager.deleteAnnotations(annotManager.getAnnotationsList());
33
34 // import annotations from PDFNet
35 const fdfDoc = await doc.fdfExtract(PDFNet.PDFDoc.ExtractFlag.e_both);
36 const xfdf = await fdfDoc.saveAsXFDFAsString();
37 annotManager.importAnnotations(xfdf);
38 });
39
40 // clear the cache (rendered) data with the newly updated document
41 documentViewer.refreshAll();
42
43 // Update viewer to render with the new document
44 documentViewer.updateView();
45
46 // Refresh searchable and selectable text data with the new document
47 documentViewer.getDocument().refreshTextData();
48});

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales