|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Show and Hide Annotations in EJ2 ASP.NET MVC PdfViewer | Syncfusion |
| 4 | +description: Learn how to dynamically show and hide annotations in the Syncfusion ASP.NET MVC PDF Viewer component of Syncfusion Essential JS 2 and more. |
| 5 | +platform: ej2-asp-core-mvc |
| 6 | +control: Show and Hide Annotations |
| 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 implement functionality to dynamically show and hide annotations in a PDF document loaded in the Syncfusion PDF Viewer using ASP.NET MVC. This feature is particularly useful in scenarios where you need to present a clean view of the document or toggle between annotated and non-annotated views. |
| 16 | + |
| 17 | +### How to Show and Hide Annotations |
| 18 | + |
| 19 | +**Step 1:** Set Up the PdfViewer in Your ASP.NET MVC Project |
| 20 | + |
| 21 | +First, follow the steps provided in the [getting started guide](https://ej2.syncfusion.com/aspnetmvc/documentation/pdfviewer/getting-started) to create a simple PDF Viewer sample. |
| 22 | + |
| 23 | +**Step 2:** Add Toggle Button and Implementation |
| 24 | + |
| 25 | +Add a button to toggle annotation visibility and implement the necessary JavaScript functions to handle the show/hide functionality: |
| 26 | + |
| 27 | +{% tabs %} |
| 28 | +{% highlight html tabtitle="Standalone" %} |
| 29 | + |
| 30 | +@using Syncfusion.EJ2 |
| 31 | +@{ |
| 32 | + ViewBag.Title = "Home Page"; |
| 33 | +} |
| 34 | +<div> |
| 35 | + <div class="button-container" style="margin-bottom: 10px;"> |
| 36 | + <button id="hideBtn" class="e-btn e-primary">Hide Annotations</button> |
| 37 | + <button id="unhideBtn" class="e-btn e-primary">Show Annotations</button> |
| 38 | + </div> |
| 39 | + <div style="height:500px;width:100%;"> |
| 40 | + @Html.EJS().PdfViewer("pdfviewer").DocumentPath("https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf").ResourceUrl("https://cdn.syncfusion.com/ej2/30.1.37/dist/ej2-pdfviewer-lib").Render() |
| 41 | + </div> |
| 42 | +</div> |
| 43 | + |
| 44 | +<script type="text/javascript"> |
| 45 | + var exportObject; |
| 46 | + |
| 47 | + // Function to hide annotations |
| 48 | + function HideAnnotations() { |
| 49 | + var pdfviewer = document.getElementById('pdfviewer').ej2_instances[0]; |
| 50 | + pdfviewer.exportAnnotationsAsObject().then(function(value) { |
| 51 | + exportObject = value; |
| 52 | + pdfviewer.deleteAnnotations(); |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + // Function to unhide annotations |
| 57 | + function UnHideAnnotations() { |
| 58 | + var pdfviewer = document.getElementById('pdfviewer').ej2_instances[0]; |
| 59 | + if (exportObject) { |
| 60 | + pdfviewer.importAnnotation(JSON.parse(exportObject)); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Add event listeners to buttons |
| 65 | + document.getElementById('hideBtn').addEventListener('click', HideAnnotations); |
| 66 | + document.getElementById('unhideBtn').addEventListener('click', UnHideAnnotations); |
| 67 | +</script> |
| 68 | + |
| 69 | +{% endhighlight %} |
| 70 | +{% endtabs %} |
| 71 | + |
| 72 | +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. |
| 73 | + |
| 74 | +[View sample in GitHub](https://github.com/SyncfusionExamples/mvc-pdf-viewer-examples/tree/master/How%20to) |
0 commit comments