|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Flatten Annotations in the Syncfusion React PDF Viewer |
| 4 | +description: Learn how all about how to flatten annotations and formfields before saving a PDF in the Syncfusion React PDF Viewer. |
| 5 | +platform: document-processing |
| 6 | +control: PDF Viewer |
| 7 | +documentation: ug |
| 8 | +domainurl: ##DomainURL## |
| 9 | +--- |
| 10 | + |
| 11 | +# Flatten Annotations in React PDF Viewer |
| 12 | + |
| 13 | +Flattening takes the visual appearance of annotations and embeds them into each page's content stream. The visual result remains visible, but the annotation objects and interactive form field structures are removed or converted so they can no longer be selected, edited, or filled. |
| 14 | + |
| 15 | +Flattening annotations permanently merges them into the PDF content. Once flattened: |
| 16 | +- Annotations are **no longer editable** in any PDF viewer. |
| 17 | +- Useful for **secure sharing**, preventing modifications. |
| 18 | +- Ideal when **finalizing markup** before distribution. |
| 19 | + |
| 20 | +## How to Flatten Annotations |
| 21 | + |
| 22 | +You can flatten annotations either when a document is loaded (preprocessing) or when exporting/saving the file. Flattening on load makes the viewer display a flattened version immediately; flattening on export preserves the original viewer session while producing a flattened output file. |
| 23 | + |
| 24 | +Typical export-time steps: |
| 25 | +- Save the viewer contents to a Blob. |
| 26 | +- Create a `PdfDocument` from the saved bytes. |
| 27 | +- Enable `document.flatten = true` to merge annotations and form field appearances. |
| 28 | +- Save the resulting PDF. |
| 29 | + |
| 30 | +Use the example below to flatten at export time (on download). |
| 31 | + |
| 32 | +{% tabs %} |
| 33 | +{% highlight js tabtitle="Standalone" %} |
| 34 | +{% raw %} |
| 35 | +import { createRoot } from 'react-dom/client'; |
| 36 | +import './index.css'; |
| 37 | +import * as React from 'react'; |
| 38 | +import { |
| 39 | + PdfViewerComponent, |
| 40 | + Toolbar, |
| 41 | + Magnification, |
| 42 | + Navigation, |
| 43 | + LinkAnnotation, |
| 44 | + BookmarkView, |
| 45 | + ThumbnailView, |
| 46 | + Print, |
| 47 | + TextSelection, |
| 48 | + TextSearch, |
| 49 | + Annotation, |
| 50 | + FormFields, |
| 51 | + FormDesigner, |
| 52 | + PageOrganizer, |
| 53 | + Inject, |
| 54 | +} from '@syncfusion/ej2-react-pdfviewer'; |
| 55 | +import { |
| 56 | + PdfDocument |
| 57 | +} from '@syncfusion/ej2-pdf'; |
| 58 | + |
| 59 | +function Default() { |
| 60 | + let viewer; |
| 61 | + |
| 62 | + const flattenPdf = () => { |
| 63 | + viewer.saveAsBlob().then((value) => { |
| 64 | + const reader = new FileReader(); |
| 65 | + reader.onloadend = function () { |
| 66 | + const arrayBuffer = reader.result; |
| 67 | + const byteArray = new Uint8Array(arrayBuffer); |
| 68 | + const document = new PdfDocument(byteArray); |
| 69 | + // Flatten all annotations and form fields: this embeds appearances |
| 70 | + // into the page content so annotations are no longer interactive. |
| 71 | + document.flatten = true; |
| 72 | + document.save('flattened.pdf'); |
| 73 | + }; |
| 74 | + reader.readAsArrayBuffer(value); |
| 75 | + }); |
| 76 | + }; |
| 77 | + |
| 78 | + return ( |
| 79 | + <div> |
| 80 | + <div className="control-section"> |
| 81 | + <button onClick={flattenPdf}>Flatten and download PDF</button> |
| 82 | + |
| 83 | + <PdfViewerComponent |
| 84 | + ref={(scope) => { viewer = scope; }} |
| 85 | + id="container" |
| 86 | + documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf" |
| 87 | + resourceUrl="https://cdn.syncfusion.com/ej2/23.2.6/dist/ej2-pdfviewer-lib" |
| 88 | + style={{ height: '640px' }} |
| 89 | + > |
| 90 | + <Inject |
| 91 | + services={[ |
| 92 | + Toolbar, |
| 93 | + Magnification, |
| 94 | + Navigation, |
| 95 | + LinkAnnotation, |
| 96 | + BookmarkView, |
| 97 | + ThumbnailView, |
| 98 | + Print, |
| 99 | + TextSelection, |
| 100 | + TextSearch, |
| 101 | + Annotation, |
| 102 | + FormFields, |
| 103 | + FormDesigner, |
| 104 | + PageOrganizer, |
| 105 | + ]} |
| 106 | + /> |
| 107 | + </PdfViewerComponent> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + ); |
| 111 | +} |
| 112 | +export default Default; |
| 113 | + |
| 114 | +const root = createRoot(document.getElementById('sample')); |
| 115 | +root.render(<Default />); |
| 116 | +{% endraw %} |
| 117 | +{% endhighlight %} |
| 118 | +{% endtabs %} |
| 119 | + |
| 120 | +N> To flatten documents when they are uploaded/loaded into the viewer, see [Flatten on Load](../document-handling/preprocess-pdf#flatten-on-load). |
| 121 | + |
| 122 | + |
| 123 | +## Notes |
| 124 | + |
| 125 | +- Flattening applies to **all annotation types**: text markup, shapes, stamps, notes, ink, and form fields. |
| 126 | +- Once flattened, annotations **cannot be edited or removed**. |
| 127 | +- Use flattening **only at export time**, not during regular document interactions. |
| 128 | + |
| 129 | +## See also |
| 130 | + |
| 131 | +- [Annotation Overview](../overview) |
| 132 | +- [Annotation Types](../annotation/annotation-types/area-annotation) |
| 133 | +- [Annotation Toolbar](../toolbar-customization/annotation-toolbar) |
| 134 | +- [Create and Modify Annotation](../annotation/create-modify-annotation) |
| 135 | +- [Customize Annotation](../annotation/customize-annotation) |
| 136 | +- [Handwritten Signature](../annotation/signature-annotation) |
| 137 | +- [Export and Import Annotation](../annotation/export-import/export-annotation) |
| 138 | +- [Annotation Permission](../annotation/annotation-permission) |
| 139 | +- [Annotation in Mobile View](../annotation/annotations-in-mobile-view) |
| 140 | +- [Annotation Events](../annotation/annotation-event) |
| 141 | +- [Annotation API](../annotation/annotations-api) |
0 commit comments