For the complete documentation index, see llms.txt. This page is also available as Markdown.

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.

WebViewer({
  fullAPI: true,
  // other constructor options
}

// config.js
const { documentViewer, annotationManager } = instance.Core;

document.getElementById('myBtn').addEventListener('click', async () => {
  const doc = documentViewer.getDocument();
  const xfdfString = await annotationManager.exportAnnotations();
  const options = { xfdfString, flatten: true };
  const data = await doc.getFileData(options);
  const arr = new Uint8Array(data);
  const blob = new Blob([arr], { type: 'application/pdf' });
  // upload blob to your server
});
WebViewer({
  fullAPI: true,
  // other constructor options
}

// config.js
const { docViewer, annotManager } = instance;

document.getElementById('myBtn').addEventListener('click', async () => {
  const doc = docViewer.getDocument();
  const xfdfString = await annotManager.exportAnnotations();
  const options = { xfdfString, flatten: true };
  const data = await doc.getFileData(options);
  const arr = new Uint8Array(data);
  const blob = new Blob([arr], { type: 'application/pdf' });
  // upload blob to your server
});

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.

Flatten annotations in the viewer

Use flattenAnnotations method to flatten all annotations into a document.

Flatten select annotations

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

Last updated

Was this helpful?