|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Show and Hide Annotations in EJ2 ASP.NET CORE PdfViewer | Syncfusion |
| 4 | +description: Learn how to show and hide annotations in the Syncfusion ASP.NET CORE PDF Viewer component of Syncfusion Essential JS 2 and more. |
| 5 | +platform: ej2-asp-core-mvc |
| 6 | +control: PDF Viewer |
| 7 | +publishingplatform: ##Platform_Name## |
| 8 | +documentation: ug |
| 9 | +--- |
| 10 | + |
| 11 | +# Show and Hide Annotations in PDF Viewer |
| 12 | + |
| 13 | +### Overview |
| 14 | + |
| 15 | +This guide demonstrates how to dynamically show and hide annotations in the Syncfusion PDF Viewer component in an ASP.NET Core application. This functionality is useful when you want to provide users with the ability to toggle the visibility of annotations within PDF documents. |
| 16 | + |
| 17 | +##### Conclusion |
| 18 | + |
| 19 | +By implementing the show and hide annotations functionality in the Syncfusion PDF Viewer, you can provide users with a more flexible document viewing experience. This approach maintains the original annotations data while giving users control over their visibility, which is particularly useful in document review, presentation, and analysis scenarios. The toggle mechanism demonstrates how to effectively use the PDF Viewer's annotation APIs to create enhanced user interactions with PDF documents. |
| 20 | + |
| 21 | +[View sample in GitHub](https://github.com/SyncfusionExamples/asp-core-pdf-viewer-examples/tree/master/How%20to/ShowHideAnnotations) Implementation Steps |
| 22 | + |
| 23 | +**Step 1:** Set up the PDF Viewer in your ASP.NET Core project |
| 24 | + |
| 25 | +First, create a basic PDF Viewer implementation in your ASP.NET Core application. |
| 26 | + |
| 27 | +**Step 2:** Add a toggle button and implement the show/hide functionality |
| 28 | + |
| 29 | +Add a button that allows users to toggle the visibility of annotations within the PDF document. |
| 30 | + |
| 31 | +```html |
| 32 | +@page "{handler?}" |
| 33 | +@using ShowHideAnnotations.Pages |
| 34 | +@model IndexModel |
| 35 | +@{ |
| 36 | + ViewData["Title"] = "Home page"; |
| 37 | +} |
| 38 | +<div class="text-center"> |
| 39 | + <button id="toggleBtn" onclick="toggleAnnotations()">Hide Annotations</button> |
| 40 | + <ejs-pdfviewer id="pdfviewer" style="height:600px" serviceUrl="/Index" documentPath=""> |
| 41 | + </ejs-pdfviewer> |
| 42 | +</div> |
| 43 | + |
| 44 | +<script type="text/javascript"> |
| 45 | + var exportObject = null; |
| 46 | + var annotationsVisible = true; |
| 47 | + |
| 48 | + // Function to run when page loads |
| 49 | + document.addEventListener('DOMContentLoaded', function() { |
| 50 | + // Get viewer instance |
| 51 | + var viewer = document.getElementById('pdfviewer').ej2_instances[0]; |
| 52 | + // Load the PDF document |
| 53 | + if (viewer) { |
| 54 | + viewer.documentPath="Data/Annotations.pdf"; |
| 55 | + // You can also add any initialization code here |
| 56 | + console.log("PDF viewer initialized and document loading started"); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + function toggleAnnotations() { |
| 61 | + var viewer = document.getElementById('pdfviewer').ej2_instances[0]; |
| 62 | + |
| 63 | + if (annotationsVisible) { |
| 64 | + // Hide annotations by exporting and deleting them |
| 65 | + viewer.exportAnnotationsAsObject().then(function(value) { |
| 66 | + exportObject = value; |
| 67 | + var count = viewer.annotationCollection.length; |
| 68 | + for (var i = 0; i < count; i++) { |
| 69 | + // Always delete the first item as the collection shrinks |
| 70 | + viewer.annotationModule.deleteAnnotationById(viewer.annotationCollection[0].annotationId); |
| 71 | + } |
| 72 | + annotationsVisible = false; |
| 73 | + document.getElementById('toggleBtn').textContent = 'Show Annotations'; |
| 74 | + }); |
| 75 | + } else { |
| 76 | + // Restore annotations |
| 77 | + if (exportObject) { |
| 78 | + var exportAnnotObject = JSON.parse(exportObject); |
| 79 | + viewer.importAnnotation(exportAnnotObject); |
| 80 | + } |
| 81 | + annotationsVisible = true; |
| 82 | + document.getElementById('toggleBtn').textContent = 'Hide Annotations'; |
| 83 | + } |
| 84 | + } |
| 85 | +</script> |
| 86 | +``` |
| 87 | + |
| 88 | +### Conclusion |
| 89 | + |
| 90 | +This implementation provides a clean, efficient way to toggle the visibility of annotations in your PDF documents. It's particularly useful for presentation scenarios or when you need to focus on the document content without the distraction of annotations. |
| 91 | + |
| 92 | +[View sample in GitHub](https://github.com/SyncfusionExamples/asp-core-pdf-viewer-examples/tree/master/How%20to/ShowHideAnnotations) |
0 commit comments