1- @page " {handler?}"
1+ @page " {handler?}"
22@using ShowHideAnnotations .Pages
33@model IndexModel
44@{
55 ViewData [" Title" ] = " Home page" ;
66}
77
88<div class =" text-center" >
9- <button id =" toggleBtn" onclick =" toggleAnnotations()" >Hide Annotations</button >
10- <ejs-pdfviewer id =" pdfviewer" style =" height :600px " serviceUrl =" /Index" documentPath =" " >
9+ <button id =" hideBtn" >Hide Annotations</button >
10+ <button id =" unhideBtn" >Show Annotations</button >
11+ <ejs-pdfviewer id =" pdfviewer" style =" height :600px " resourceUrl =" https://cdn.syncfusion.com/ej2/30.1.37/dist/ej2-pdfviewer-lib" documentPath =" https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf" >
1112 </ejs-pdfviewer >
1213</div >
1314
1415<script type =" text/javascript" >
1516 var exportObject = null ;
16- var annotationsVisible = true ;
1717
18- // Function to run when page loads
1918 document .addEventListener (' DOMContentLoaded' , function () {
20- // Get viewer instance
21- var viewer = document .getElementById (' pdfviewer' ).ej2_instances [0 ];
19+ var pdfviewer = document .getElementById (' pdfviewer' ).ej2_instances [0 ];
2220
23- // Load the PDF document
24- if (viewer) {
25- viewer .documentPath = " Data/Annotations.pdf" ;
26-
27- // You can also add any initialization code here
28- console .log (" PDF viewer initialized and document loading started" );
29- }
30- });
31-
32- function toggleAnnotations () {
33- var viewer = document .getElementById (' pdfviewer' ).ej2_instances [0 ];
34-
35- if (annotationsVisible) {
36- // Hide annotations by exporting and deleting them
37- viewer .exportAnnotationsAsObject ().then (function (value ) {
21+ function HideAnnotations () {
22+ pdfviewer .exportAnnotationsAsObject ().then (function (value ) {
3823 exportObject = value;
39-
40- var count = viewer .annotationCollection .length ;
41- for (var i = 0 ; i < count; i++ ) {
42- // Always delete the first item as the collection shrinks
43- viewer .annotationModule .deleteAnnotationById (viewer .annotationCollection [0 ].annotationId );
44- }
45-
46- annotationsVisible = false ;
47- document .getElementById (' toggleBtn' ).textContent = ' Show Annotations' ;
24+ pdfviewer .deleteAnnotations ();
4825 });
49- } else {
50- // Restore annotations
26+ }
27+
28+ function UnHideAnnotations () {
5129 if (exportObject) {
52- var exportAnnotObject = JSON .parse (exportObject);
53- viewer .importAnnotation (exportAnnotObject);
30+ pdfviewer .importAnnotation (JSON .parse (exportObject));
5431 }
55-
56- annotationsVisible = true ;
57- document .getElementById (' toggleBtn' ).textContent = ' Hide Annotations' ;
5832 }
59- }
33+
34+ document .getElementById (' hideBtn' ).addEventListener (' click' , HideAnnotations);
35+ document .getElementById (' unhideBtn' ).addEventListener (' click' , UnHideAnnotations);
36+ });
6037 </script >
0 commit comments