|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Show, Hide, or Toggle Annotation Visibility | Syncfusion |
| 4 | +description: Learn here all about how to Show, Hide, or Toggle Annotation Visibility and Control annotation visibility in the React PDF Viewer |
| 5 | +control: PDF Viewer |
| 6 | +platform: document-processing |
| 7 | +documentation: ug |
| 8 | +domainurl: ##DomainURL## |
| 9 | +--- |
| 10 | + |
| 11 | +# Show, Hide, or Toggle Annotation Visibility |
| 12 | + |
| 13 | +You can control annotation visibility at two levels in the React PDF Viewer: |
| 14 | + |
| 15 | +- **In the Viewer UI**: Temporarily hide or show annotations to simplify the screen for certain workflows (presentations, reviews) without modifying the source PDF. |
| 16 | +- **At export time**: Keep annotations visible inside your app but **hide them only in the downloaded PDF** by setting the `noView` flag. |
| 17 | + |
| 18 | +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). |
| 19 | + |
| 20 | +## Hide a Specific Annotation |
| 21 | + |
| 22 | +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) |
| 23 | + |
| 24 | +**Minimal snippet (delete by id):** |
| 25 | + |
| 26 | +{% tabs %} |
| 27 | +{% highlight js tabtitle="Standalone" %} |
| 28 | +{% raw %} |
| 29 | +// Hide (remove) a specific annotation by id |
| 30 | +function hideAnnotationById(annotationId) { |
| 31 | + const viewer = document.getElementById('container').ej2_instances[0]; |
| 32 | + viewer.annotationModule.deleteAnnotationById(annotationId); |
| 33 | +} |
| 34 | + |
| 35 | +// Example: hide the first annotation currently in the document |
| 36 | +function exampleHideFirst() { |
| 37 | + const viewer = document.getElementById('container').ej2_instances[0]; |
| 38 | + if (viewer.annotationCollection?.length) { |
| 39 | + hideAnnotationById(viewer.annotationCollection[0].annotationId || viewer.annotationCollection[0].id); |
| 40 | + } |
| 41 | +} |
| 42 | +{% endraw %} |
| 43 | +{% endhighlight %} |
| 44 | +{% endtabs %} |
| 45 | + |
| 46 | +## Show a Specific Annotation |
| 47 | + |
| 48 | +Re‑import previously exported annotations and (optionally) filter the JSON to reinsert only the one you need. (Export must be done **before** deletion.) |
| 49 | + |
| 50 | +**Minimal snippet (re‑import from cache):** |
| 51 | + |
| 52 | +{% tabs %} |
| 53 | +{% highlight js tabtitle="Standalone" %} |
| 54 | +{% raw %} |
| 55 | +// Assume you cached this earlier via viewer.exportAnnotationsAsObject() |
| 56 | +function showFromCache(exportedObjectString) { |
| 57 | + const viewer = document.getElementById('container').ej2_instances[0]; |
| 58 | + if (exportedObjectString) { |
| 59 | + viewer.importAnnotation(JSON.parse(exportedObjectString)); |
| 60 | + } |
| 61 | +} |
| 62 | +{% endraw %} |
| 63 | +{% endhighlight %} |
| 64 | +{% endtabs %} |
| 65 | + |
| 66 | +## Hide or Show All Annotations |
| 67 | + |
| 68 | +Use `deleteAnnotations()` to remove **all** annotations, then re-import them from cache to show again. |
| 69 | + |
| 70 | +**Minimal snippet (toggle all using cache):** |
| 71 | + |
| 72 | +{% tabs %} |
| 73 | +{% highlight js tabtitle="Standalone" %} |
| 74 | +{% raw %} |
| 75 | +let __cache = null; |
| 76 | +let __areHidden = false; |
| 77 | + |
| 78 | +async function toggleAllAnnotations() { |
| 79 | + const viewer = document.getElementById('container').ej2_instances[0]; |
| 80 | + |
| 81 | + if (!__areHidden) { |
| 82 | + __cache = await viewer.exportAnnotationsAsObject(); |
| 83 | + viewer.deleteAnnotations(); |
| 84 | + __areHidden = true; |
| 85 | + } else { |
| 86 | + if (__cache) viewer.importAnnotation(JSON.parse(__cache)); |
| 87 | + __areHidden = false; |
| 88 | + } |
| 89 | +} |
| 90 | +{% endraw %} |
| 91 | +{% endhighlight %} |
| 92 | +{% endtabs %} |
| 93 | + |
| 94 | +## Conditional Visibility (Annotations by Type) |
| 95 | + |
| 96 | +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.) |
| 97 | + |
| 98 | +{% tabs %} |
| 99 | +{% highlight js tabtitle="Standalone" %} |
| 100 | +{% raw %} |
| 101 | +// Example: hide all Text Markup (Highlight, Underline, Strikethrough, Squiggly) |
| 102 | +async function hideTextMarkup() { |
| 103 | + const viewer = document.getElementById('container').ej2_instances[0]; |
| 104 | + |
| 105 | + const exportObject = await viewer.exportAnnotationsAsObject(); |
| 106 | + viewer.deleteAnnotations(); |
| 107 | + |
| 108 | + const parsed = JSON.parse(exportObject); |
| 109 | + const filtered = { |
| 110 | + ...parsed, |
| 111 | + textMarkupAnnotations: [] // Key name depends on version; inspect your export object |
| 112 | + }; |
| 113 | + |
| 114 | + viewer.importAnnotation(filtered); |
| 115 | +} |
| 116 | +{% endraw %} |
| 117 | +{% endhighlight %} |
| 118 | +{% endtabs %} |
| 119 | + |
| 120 | +## Role‑based Visibility Control |
| 121 | + |
| 122 | +Implement role filters on top of the export/delete/import strategy: |
| 123 | + |
| 124 | +- **Admins →** see **all annotations** |
| 125 | +- **Editors →** see **all markup** |
| 126 | +- **Viewers →** see **only comments or approved stamps** |
| 127 | + |
| 128 | +Use per‑annotation metadata (e.g., `subject`, `author`, `customData`) to include/exclude what each role can view. |
| 129 | + |
| 130 | +{% tabs %} |
| 131 | +{% highlight js tabtitle="Standalone" %} |
| 132 | +{% raw %} |
| 133 | +// Example: 'Viewer' role → only Approved stamps |
| 134 | +async function applyViewerRole() { |
| 135 | + const viewer = document.getElementById('container').ej2_instances[0]; |
| 136 | + const exportObject = await viewer.exportAnnotationsAsObject(); |
| 137 | + const payload = JSON.parse(exportObject); |
| 138 | + |
| 139 | + const roleFiltered = { |
| 140 | + ...payload, |
| 141 | + textMarkupAnnotations: [], |
| 142 | + shapeAnnotations: [], |
| 143 | + freeTextAnnotations: [], |
| 144 | + inkAnnotations: [], |
| 145 | + stampAnnotations: (payload.stampAnnotations || []).filter(s => |
| 146 | + s.subject === 'Approved' || s.customData?.status === 'approved' |
| 147 | + ) |
| 148 | + }; |
| 149 | + |
| 150 | + viewer.deleteAnnotations(); |
| 151 | + viewer.importAnnotation(roleFiltered); |
| 152 | +} |
| 153 | +{% endraw %} |
| 154 | +{% endhighlight %} |
| 155 | +{% endtabs %} |
| 156 | + |
| 157 | +## Toggle Annotation Visibility (UI Button Example) |
| 158 | + |
| 159 | +For a simple UI toggle (single button), reuse the **toggle all** function above. |
| 160 | + |
| 161 | +## Events Related to Visibility |
| 162 | + |
| 163 | +There aren’t built‑in `annotationShown`/`annotationHidden` events. Use **export/import** lifecycle 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). |
| 164 | + |
| 165 | +**Tiny example:** |
| 166 | + |
| 167 | +{% tabs %} |
| 168 | +{% highlight js tabtitle="Standalone" %} |
| 169 | +{% raw %} |
| 170 | +function wireVisibilityEvents() { |
| 171 | + const viewer = document.getElementById('container').ej2_instances[0]; |
| 172 | + |
| 173 | + viewer.exportStart = () => console.log('annotationHidden:start'); |
| 174 | + viewer.exportSuccess = () => console.log('annotationHidden:success'); |
| 175 | + |
| 176 | + viewer.importStart = () => console.log('annotationShown:start'); |
| 177 | + viewer.importSuccess = () => console.log('annotationShown:success'); |
| 178 | +} |
| 179 | +{% endraw %} |
| 180 | +{% endhighlight %} |
| 181 | +{% endtabs %} |
| 182 | + |
| 183 | +## Control annotation visibility only in the exported PDF |
| 184 | + |
| 185 | +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. |
| 186 | + |
| 187 | +{% tabs %} |
| 188 | +{% highlight js tabtitle="Standalone" %} |
| 189 | +{% raw %} |
| 190 | +import { PdfAnnotationFlag, PdfDocument } from '@syncfusion/ej2-pdf'; |
| 191 | + |
| 192 | +// Download the current PDF with annotations flagged as `noView` |
| 193 | +async function downloadWithNoView() { |
| 194 | + const viewer = document.getElementById('container').ej2_instances[0]; |
| 195 | + |
| 196 | + const blob = await viewer.saveAsBlob(); |
| 197 | + const reader = new FileReader(); |
| 198 | + reader.onload = function () { |
| 199 | + const base64 = String(reader.result).split('base64,')[1]; |
| 200 | + const doc = new PdfDocument(base64); |
| 201 | + |
| 202 | + for (let i = 0; i < doc.pageCount; i++) { |
| 203 | + const page = doc.getPage(i); |
| 204 | + for (let j = 0; j < page.annotations.count; j++) { |
| 205 | + const annot = page.annotations.at(j); |
| 206 | + annot.flags |= PdfAnnotationFlag.noView; // hide in exported PDF |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + doc.saveAsBlob().then((modified) => { |
| 211 | + const linkReader = new FileReader(); |
| 212 | + linkReader.onload = function () { |
| 213 | + const a = document.createElement('a'); |
| 214 | + a.href = linkReader.result; |
| 215 | + a.download = 'without-annotations.pdf'; |
| 216 | + a.click(); |
| 217 | + }; |
| 218 | + linkReader.readAsDataURL(modified.blobData); |
| 219 | + }); |
| 220 | + }; |
| 221 | + reader.readAsDataURL(blob); |
| 222 | +} |
| 223 | +{% endraw %} |
| 224 | +{% endhighlight %} |
| 225 | +{% endtabs %} |
| 226 | + |
| 227 | +[View sample in GitHub](https://github.com/SyncfusionExamples/react-pdf-viewer-examples/tree/master/How%20to) |
| 228 | + |
| 229 | +## See also |
| 230 | + |
| 231 | +- [Annotation Overview](../overview) |
| 232 | +- [Show or hide annotations in React PDF Viewer](./show-hide-annotation) |
| 233 | +- [Import and export annotations](../annotation/export-import/export-annotation) |
0 commit comments