1+ @page " {handler?}"
2+ @model IndexModel
3+ @{
4+ ViewData [" Title" ] = " Home page" ;
5+ }
6+ <div class =" text-center" >
7+ <!-- Control buttons for PDF Search functionality -->
8+ <div style =" margin-top : 20px ;" >
9+ <button onclick =" handleSearch()" >Search PDF</button >
10+ <button onclick =" handleSearchNext()" >Search Next</button >
11+ <button onclick =" handleCancelSearch()" >Cancel Search</button >
12+ </div >
13+ <ejs-pdfviewer id =" pdfviewer" style =" height :600px "
14+ documentPath =" https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"
15+ resourceUrl =" https://cdn.syncfusion.com/ej2/28.1.33/dist/ej2-pdfviewer-lib"
16+ textSearchHighlight =" handleTextSearchHighlight" >
17+ </ejs-pdfviewer >
18+ </div >
19+ <script type =" text/javascript" >
20+
21+ // Function to get the PDF Viewer instance
22+ function getPdfViewerInstance () {
23+ return document .getElementById (' pdfviewer' ).ej2_instances [0 ];
24+ }
25+
26+ // Initiates a search for the term 'PDF' in the document
27+ function handleSearch () {
28+ const pdfViewer = getPdfViewerInstance ();
29+ pdfViewer .textSearchModule .searchText (' PDF' , false );
30+ }
31+
32+ // Searches for the next occurrence of the term
33+ function handleSearchNext () {
34+ const pdfViewer = getPdfViewerInstance ();
35+ pdfViewer .textSearchModule .searchNext ();
36+ }
37+
38+ // Cancels the current text search operation
39+ function handleCancelSearch () {
40+ const pdfViewer = getPdfViewerInstance ();
41+ pdfViewer .textSearchModule .cancelTextSearch ();
42+ }
43+
44+
45+ // Event handler for annotation addition
46+ // Adds a rectangle annotation around highlighted text
47+ function handleTextSearchHighlight (args ) {
48+ console .log (args); // Log details of the added annotation around highlighted text
49+ const pdfViewer = getPdfViewerInstance ();
50+ const bounds = args .bounds ;
51+ pdfViewer .annotationModule .addAnnotation (' Rectangle' , {
52+ pageNumber: args .pageNumber ,
53+ offset: { x: bounds .left , y: bounds .top },
54+ width: bounds .width ,
55+ height: bounds .height ,
56+ });
57+ }
58+ </script >
0 commit comments