|
| 1 | +import { pdf } from '@syncfusion/ej2'; |
| 2 | +import { PdfViewer, TextSelection, TextSearch, Print, Navigation, Toolbar, Magnification, Annotation, FormDesigner, FormFields, TextSearchHighlightEventArgs, RectangleBounds, RectangleBoundsModel, RectangleSettings } from '@syncfusion/ej2-pdfviewer'; |
| 3 | + |
| 4 | +// Inject required modules |
| 5 | +PdfViewer.Inject(TextSelection, TextSearch, Print, Navigation, Toolbar, Magnification, Annotation, FormDesigner, FormFields); |
| 6 | + |
| 7 | +const pdfviewer: PdfViewer = new PdfViewer({ |
| 8 | + documentPath: 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf', |
| 9 | + resourceUrl:'https://cdn.syncfusion.com/ej2/28.1.33/dist/ej2-pdfviewer-lib' |
| 10 | +}); |
| 11 | + |
| 12 | +pdfviewer.appendTo('#PdfViewer'); |
| 13 | + |
| 14 | +// Highlight event handler for text search, which adds a rectangle annotation where the text is found |
| 15 | +pdfviewer.textSearchHighlight = function(args: TextSearchHighlightEventArgs): void { |
| 16 | + console.log(args); |
| 17 | + const pageNumber: number = args.pageNumber; |
| 18 | + const bounds: RectangleBoundsModel = args.bounds; |
| 19 | + pdfviewer.annotation.addAnnotation('Rectangle', { |
| 20 | + offset: { x: bounds.left, y: bounds.top }, |
| 21 | + pageNumber: pageNumber, |
| 22 | + width: bounds.width, |
| 23 | + height: bounds.height, |
| 24 | + } as RectangleSettings); |
| 25 | +}; |
| 26 | + |
| 27 | +// Add event listener to "searchText" button to trigger a search for the term 'PDF' |
| 28 | +const searchTextButton = document.getElementById('searchText'); |
| 29 | +if (searchTextButton) { |
| 30 | + searchTextButton.addEventListener('click', function() { |
| 31 | + pdfviewer.textSearchModule.searchText('PDF', false); |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +// Add event listener to "searchNext" button to navigate to the next search result |
| 36 | +const searchNextButton = document.getElementById('searchNext'); |
| 37 | +if (searchNextButton) { |
| 38 | + searchNextButton.addEventListener('click', function() { |
| 39 | + pdfviewer.textSearch.searchNext(); |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +// Add event listener to "searchCancel" button to cancel the current text search operation |
| 44 | +const searchCancelButton = document.getElementById('searchCancel'); |
| 45 | +if (searchCancelButton) { |
| 46 | + searchCancelButton.addEventListener('click', function() { |
| 47 | + pdfviewer.textSearch.cancelTextSearch(); |
| 48 | + }); |
| 49 | +} |
0 commit comments