-
Notifications
You must be signed in to change notification settings - Fork 13
1012852: Updated flatten annotation UG #2344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+268
−0
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
643408c
1012852: Updated flatten and control annotation visibility UG
SF4524LogeshKumar 1d8129f
1012852: Resolved CI failures
SF4524LogeshKumar 6a6bc94
1012852: Removed toggle-annotation-visibility
SF4524LogeshKumar 9848ade
1012852: Resolved CI failure
SF4524LogeshKumar 6a54ccd
1012852: Updated the Review Changes for Flatten and preprocess-pdf
SF4524LogeshKumar 352a433
1012852: Addressed the review changes
SF4524LogeshKumar b8e58c5
1012852: Resolved CI Failure
SF4524LogeshKumar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
Document-Processing/PDF/PDF-Viewer/react/annotation/flatten-annotation.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| --- | ||
| layout: post | ||
| title: Flatten Annotations Before Saving the PDF | Syncfusion | ||
| description: Learn how all about how to flatten annotations and formfields before saving a PDF in the Syncfusion React PDF Viewer. | ||
| platform: document-processing | ||
| control: PDF Viewer | ||
| documentation: ug | ||
| domainurl: ##DomainURL## | ||
| --- | ||
|
|
||
| # Flatten Annotations Before Saving the PDF in React | ||
| Flattening annotations permanently merges them into the PDF content. Once flattened: | ||
| - Annotations are **no longer editable** in any PDF viewer. | ||
| - Useful for **secure sharing**, preventing modifications. | ||
| - Ideal when **finalizing markup** before distribution. | ||
|
|
||
| ## How to Flatten Annotations (React) | ||
| Follow these steps to flatten annotations and form fields before saving the PDF. | ||
|
|
||
| {% tabs %} | ||
| {% highlight js tabtitle="Standalone" %} | ||
| {% raw %} | ||
| import { createRoot } from 'react-dom/client'; | ||
| import './index.css'; | ||
| import * as React from 'react'; | ||
| import { | ||
| PdfViewerComponent, | ||
| Toolbar, | ||
| Magnification, | ||
| Navigation, | ||
| LinkAnnotation, | ||
| BookmarkView, | ||
| ThumbnailView, | ||
| Print, | ||
| TextSelection, | ||
| TextSearch, | ||
| Annotation, | ||
| FormFields, | ||
| FormDesigner, | ||
| PageOrganizer, | ||
| Inject, | ||
| } from '@syncfusion/ej2-react-pdfviewer'; | ||
| import { | ||
| PdfDocument | ||
| } from '@syncfusion/ej2-pdf'; | ||
|
|
||
| function Default() { | ||
| let viewer; | ||
|
|
||
| const flattenPdf = () => { | ||
| viewer.saveAsBlob().then((value) => { | ||
| const reader = new FileReader(); | ||
| reader.onloadend = function () { | ||
| const arrayBuffer = reader.result; | ||
| const byteArray = new Uint8Array(arrayBuffer); | ||
| const document = new PdfDocument(byteArray); | ||
| // Flatten all annotations and form fields | ||
| document.flatten = true; | ||
| document.save('flattened.pdf'); | ||
| }; | ||
| reader.readAsArrayBuffer(value); | ||
| }); | ||
| }; | ||
|
|
||
| return ( | ||
| <div> | ||
| <div className="control-section"> | ||
| <button onClick={flattenPdf}>Flatten and download PDF</button> | ||
|
|
||
| <PdfViewerComponent | ||
| ref={(scope) => { viewer = scope; }} | ||
| id="container" | ||
| documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf" | ||
| resourceUrl="https://cdn.syncfusion.com/ej2/23.2.6/dist/ej2-pdfviewer-lib" | ||
| style={{ height: '640px' }} | ||
| > | ||
| <Inject | ||
| services={[ | ||
| Toolbar, | ||
| Magnification, | ||
| Navigation, | ||
| LinkAnnotation, | ||
| BookmarkView, | ||
| ThumbnailView, | ||
| Print, | ||
| TextSelection, | ||
| TextSearch, | ||
| Annotation, | ||
| FormFields, | ||
| FormDesigner, | ||
| PageOrganizer, | ||
| ]} | ||
| /> | ||
| </PdfViewerComponent> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
| export default Default; | ||
|
|
||
| const root = createRoot(document.getElementById('sample')); | ||
| root.render(<Default />); | ||
| {% endraw %} | ||
| {% endhighlight %} | ||
| {% endtabs %} | ||
|
|
||
| ## Notes | ||
|
|
||
| - Flattening applies to **all annotation types**: text markup, shapes, stamps, notes, ink, and form fields. | ||
| - Once flattened, annotations **cannot be edited or removed**. | ||
| - Use flattening **only at export time**, not during regular document interactions. | ||
|
|
||
| ## See also | ||
|
|
||
| - [Annotation Overview](../overview) | ||
| - [Annotation Types](../annotation/annotation-types/area-annotation) | ||
| - [Annotation Toolbar](../toolbar-customization/annotation-toolbar) | ||
| - [Create and Modify Annotation](../annotation/create-modify-annotation) | ||
| - [Customize Annotation](../annotation/customize-annotation) | ||
| - [Handwritten Signature](../annotation/signature-annotation) | ||
| - [Export and Import Annotation](../annotation/export-import/export-annotation) | ||
| - [Annotation Permission](../annotation/annotation-permission) | ||
| - [Annotation in Mobile View](../annotation/annotations-in-mobile-view) | ||
| - [Annotation Events](../annotation/annotation-event) | ||
| - [Annotation API](../annotation/annotations-api) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
233 changes: 233 additions & 0 deletions
233
Document-Processing/PDF/PDF-Viewer/react/how-to/toggle-annotation-visibility.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| --- | ||
| layout: post | ||
| title: Show, Hide, or Toggle Annotation Visibility | Syncfusion | ||
| description: Learn here all about how to Show, Hide, or Toggle Annotation Visibility and Control annotation visibility in the React PDF Viewer | ||
| control: PDF Viewer | ||
| platform: document-processing | ||
| documentation: ug | ||
| domainurl: ##DomainURL## | ||
| --- | ||
|
|
||
| # Show, Hide, or Toggle Annotation Visibility | ||
|
|
||
| You can control annotation visibility at two levels in the React PDF Viewer: | ||
|
|
||
| - **In the Viewer UI**: Temporarily hide or show annotations to simplify the screen for certain workflows (presentations, reviews) without modifying the source PDF. | ||
| - **At export time**: Keep annotations visible inside your app but **hide them only in the downloaded PDF** by setting the `noView` flag. | ||
|
|
||
| N> If you’re new to the component, set up the **Standalone** Viewer first using the [Getting Started guide](https://help.syncfusion.com/document-processing/pdf/pdf-viewer/react/getting-started). | ||
|
|
||
| ## Hide a Specific Annotation | ||
|
|
||
| For concise Viewer‑level hiding, remove an annotation by its **id**. If you’ll want to re‑show it later, export annotations to a cache first, then delete the target. See the full, step‑by‑step sample in [Show or hide annotations in React PDF Viewer](./show-hide-annotation) | ||
|
|
||
| **Minimal snippet (delete by id):** | ||
|
|
||
| {% tabs %} | ||
| {% highlight js tabtitle="Standalone" %} | ||
| {% raw %} | ||
| // Hide (remove) a specific annotation by id | ||
| function hideAnnotationById(annotationId) { | ||
| const viewer = document.getElementById('container').ej2_instances[0]; | ||
| viewer.annotationModule.deleteAnnotationById(annotationId); | ||
| } | ||
|
|
||
| // Example: hide the first annotation currently in the document | ||
| function exampleHideFirst() { | ||
| const viewer = document.getElementById('container').ej2_instances[0]; | ||
| if (viewer.annotationCollection?.length) { | ||
| hideAnnotationById(viewer.annotationCollection[0].annotationId || viewer.annotationCollection[0].id); | ||
| } | ||
| } | ||
| {% endraw %} | ||
| {% endhighlight %} | ||
| {% endtabs %} | ||
|
|
||
| ## Show a Specific Annotation | ||
|
|
||
| Re‑import previously exported annotations and (optionally) filter the JSON to reinsert only the one you need. (Export must be done **before** deletion.) | ||
|
|
||
| **Minimal snippet (re‑import from cache):** | ||
|
|
||
| {% tabs %} | ||
| {% highlight js tabtitle="Standalone" %} | ||
| {% raw %} | ||
| // Assume you cached this earlier via viewer.exportAnnotationsAsObject() | ||
| function showFromCache(exportedObjectString) { | ||
| const viewer = document.getElementById('container').ej2_instances[0]; | ||
| if (exportedObjectString) { | ||
| viewer.importAnnotation(JSON.parse(exportedObjectString)); | ||
| } | ||
| } | ||
| {% endraw %} | ||
| {% endhighlight %} | ||
| {% endtabs %} | ||
|
|
||
| ## Hide or Show All Annotations | ||
|
|
||
| Use `deleteAnnotations()` to remove **all** annotations, then re-import them from cache to show again. | ||
|
|
||
| **Minimal snippet (toggle all using cache):** | ||
|
|
||
| {% tabs %} | ||
| {% highlight js tabtitle="Standalone" %} | ||
| {% raw %} | ||
| let __cache = null; | ||
| let __areHidden = false; | ||
|
|
||
| async function toggleAllAnnotations() { | ||
| const viewer = document.getElementById('container').ej2_instances[0]; | ||
|
|
||
| if (!__areHidden) { | ||
| __cache = await viewer.exportAnnotationsAsObject(); | ||
| viewer.deleteAnnotations(); | ||
| __areHidden = true; | ||
| } else { | ||
| if (__cache) viewer.importAnnotation(JSON.parse(__cache)); | ||
| __areHidden = false; | ||
| } | ||
| } | ||
| {% endraw %} | ||
| {% endhighlight %} | ||
| {% endtabs %} | ||
|
|
||
| ## Conditional Visibility (Annotations by Type) | ||
|
|
||
| To hide a **type** (e.g., all text markups) while keeping others, export to cache, clear the UI, then **re-import only the allowed types**. The export payload is JSON; filter by each type’s bucket before importing. (Structure may vary by version—adjust keys accordingly.) | ||
|
|
||
| {% tabs %} | ||
| {% highlight js tabtitle="Standalone" %} | ||
| {% raw %} | ||
| // Example: hide all Text Markup (Highlight, Underline, Strikethrough, Squiggly) | ||
| async function hideTextMarkup() { | ||
| const viewer = document.getElementById('container').ej2_instances[0]; | ||
|
|
||
| const exportObject = await viewer.exportAnnotationsAsObject(); | ||
| viewer.deleteAnnotations(); | ||
|
|
||
| const parsed = JSON.parse(exportObject); | ||
| const filtered = { | ||
| ...parsed, | ||
| textMarkupAnnotations: [] // Key name depends on version; inspect your export object | ||
| }; | ||
|
|
||
| viewer.importAnnotation(filtered); | ||
| } | ||
| {% endraw %} | ||
| {% endhighlight %} | ||
| {% endtabs %} | ||
|
|
||
| ## Role‑based Visibility Control | ||
|
|
||
| Implement role filters on top of the export/delete/import strategy: | ||
|
|
||
| - **Admin →** see **all annotations** | ||
| - **Editors →** see **all markup** | ||
| - **Viewers →** see **only comments or approved stamps** | ||
|
|
||
| Use per‑annotation metadata (e.g., `subject`, `author`, `customData`) to include/exclude what each role can view. | ||
|
|
||
| {% tabs %} | ||
| {% highlight js tabtitle="Standalone" %} | ||
| {% raw %} | ||
| // Example: 'Viewer' role → only Approved stamps | ||
| async function applyViewerRole() { | ||
| const viewer = document.getElementById('container').ej2_instances[0]; | ||
| const exportObject = await viewer.exportAnnotationsAsObject(); | ||
| const payload = JSON.parse(exportObject); | ||
|
|
||
| const roleFiltered = { | ||
| ...payload, | ||
| textMarkupAnnotations: [], | ||
| shapeAnnotations: [], | ||
| freeTextAnnotations: [], | ||
| inkAnnotations: [], | ||
| stampAnnotations: (payload.stampAnnotations || []).filter(s => | ||
| s.subject === 'Approved' || s.customData?.status === 'approved' | ||
| ) | ||
| }; | ||
|
|
||
| viewer.deleteAnnotations(); | ||
| viewer.importAnnotation(roleFiltered); | ||
| } | ||
| {% endraw %} | ||
| {% endhighlight %} | ||
| {% endtabs %} | ||
|
|
||
| ## Toggle Annotation Visibility (UI Button Example) | ||
|
|
||
| For a simple UI toggle (single button), reuse the **toggle all** function above. | ||
|
|
||
| ## Events Related to Visibility | ||
|
|
||
| There aren’t built‑in `annotationShown`/`annotationHidden` events. Use **export/import** life-cycle events to infer visibility changes (e.g., `exportStart`/`exportSuccess` when hiding, and `importStart`/`importSuccess` when showing). For all annotation‑level events (add/remove/select/resize/etc.), see **Annotation Events** (link below). | ||
SF4524LogeshKumar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| **Tiny example:** | ||
|
|
||
| {% tabs %} | ||
| {% highlight js tabtitle="Standalone" %} | ||
| {% raw %} | ||
| function wireVisibilityEvents() { | ||
| const viewer = document.getElementById('container').ej2_instances[0]; | ||
|
|
||
| viewer.exportStart = () => console.log('annotationHidden:start'); | ||
| viewer.exportSuccess = () => console.log('annotationHidden:success'); | ||
|
|
||
| viewer.importStart = () => console.log('annotationShown:start'); | ||
| viewer.importSuccess = () => console.log('annotationShown:success'); | ||
| } | ||
| {% endraw %} | ||
| {% endhighlight %} | ||
| {% endtabs %} | ||
|
|
||
| ## Control annotation visibility only in the exported PDF | ||
|
|
||
| If you want annotations **visible in your app** but **hidden in the saved PDF**, set the **`PdfAnnotationFlag.noView`** flag on each annotation before download using the EJ2 PDF library. | ||
|
|
||
| {% tabs %} | ||
| {% highlight js tabtitle="Standalone" %} | ||
| {% raw %} | ||
| import { PdfAnnotationFlag, PdfDocument } from '@syncfusion/ej2-pdf'; | ||
|
|
||
| // Download the current PDF with annotations flagged as `noView` | ||
| async function downloadWithNoView() { | ||
| const viewer = document.getElementById('container').ej2_instances[0]; | ||
|
|
||
| const blob = await viewer.saveAsBlob(); | ||
| const reader = new FileReader(); | ||
| reader.onload = function () { | ||
| const base64 = String(reader.result).split('base64,')[1]; | ||
| const doc = new PdfDocument(base64); | ||
|
|
||
| for (let i = 0; i < doc.pageCount; i++) { | ||
| const page = doc.getPage(i); | ||
| for (let j = 0; j < page.annotations.count; j++) { | ||
| const annot = page.annotations.at(j); | ||
| annot.flags |= PdfAnnotationFlag.noView; // hide in exported PDF | ||
| } | ||
| } | ||
|
|
||
| doc.saveAsBlob().then((modified) => { | ||
| const linkReader = new FileReader(); | ||
| linkReader.onload = function () { | ||
| const a = document.createElement('a'); | ||
| a.href = linkReader.result; | ||
| a.download = 'without-annotations.pdf'; | ||
| a.click(); | ||
| }; | ||
| linkReader.readAsDataURL(modified.blobData); | ||
| }); | ||
| }; | ||
| reader.readAsDataURL(blob); | ||
| } | ||
| {% endraw %} | ||
| {% endhighlight %} | ||
| {% endtabs %} | ||
|
|
||
| [View sample in GitHub](https://github.com/SyncfusionExamples/react-pdf-viewer-examples/tree/master/How%20to) | ||
|
|
||
| ## See also | ||
|
|
||
| - [Annotation Overview](../overview) | ||
| - [Show or hide annotations in React PDF Viewer](./show-hide-annotation) | ||
| - [Import and export annotations](../annotation/export-import/export-annotation) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.