Skip to content

Commit 7607d3f

Browse files
834567: How to Add Rectangle Annotations Using Search Text Bounds
1 parent f2fe02f commit 7607d3f

File tree

15 files changed

+101
-62
lines changed

15 files changed

+101
-62
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom/client';
3+
import './index.css';
4+
import {
5+
PdfViewerComponent,
6+
Toolbar,
7+
Magnification,
8+
Navigation,
9+
Annotation,
10+
TextSelection,
11+
TextSearch,
12+
FormFields,
13+
FormDesigner,
14+
PageOrganizer,
15+
Inject
16+
} from '@syncfusion/ej2-react-pdfviewer';
17+
18+
class App extends React.Component {
19+
constructor(props) {
20+
super(props);
21+
// Create a ref for the PdfViewerComponent to access its methods and properties
22+
this.viewerRef = React.createRef();
23+
}
24+
25+
componentDidMount() {
26+
// Obtain the current instance of the PdfViewerComponent
27+
const viewer = this.viewerRef.current;
28+
29+
if (viewer) {
30+
// Attach an event handler for text search highlight
31+
viewer.textSearchHighlight = this.handleTextSearchHighlight;
32+
}
33+
}
34+
35+
// Method to handle the text search highlight event
36+
handleTextSearchHighlight = (args) => {
37+
console.log(args); // Logs Highlighted text search details
38+
39+
const pageNumber = args.pageNumber;
40+
const bounds = args.bounds;
41+
42+
// Add a rectangle annotation on the highlighted text
43+
this.viewerRef.current.annotationModule.addAnnotation('Rectangle', {
44+
pageNumber: pageNumber,
45+
offset: { x: bounds.left, y: bounds.top },
46+
width: bounds.width,
47+
height: bounds.height,
48+
});
49+
};
50+
51+
// Method to initiate a text search for the term 'PDF'
52+
handleSearch = () => {
53+
this.viewerRef.current.textSearchModule.searchText('PDF', false);
54+
};
55+
56+
// Method to go to the next instance of the search term
57+
handleSearchNext = () => {
58+
this.viewerRef.current.textSearchModule.searchNext();
59+
};
60+
61+
// Method to cancel the current text search operation
62+
handleCancelSearch = () => {
63+
this.viewerRef.current.textSearchModule.cancelTextSearch();
64+
};
65+
66+
render() {
67+
return (
68+
<div>
69+
<div style={{ marginTop: '50px' }}>
70+
<button onClick={this.handleSearch}>Search PDF</button>
71+
<button onClick={this.handleSearchNext}>Search Next</button>
72+
<button onClick={this.handleCancelSearch}>Cancel Search</button>
73+
</div>
74+
<div className='control-section' style={{ marginTop: '5px' }}>
75+
<PdfViewerComponent
76+
ref={this.viewerRef}
77+
id="PdfViewer"
78+
documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf"
79+
resourceUrl="https://cdn.syncfusion.com/ej2/28.1.33/dist/ej2-pdfviewer-lib"
80+
style={{ height: '640px' }}
81+
>
82+
<Inject services={[
83+
Toolbar,
84+
Magnification,
85+
Navigation,
86+
Annotation,
87+
TextSelection,
88+
TextSearch,
89+
FormFields,
90+
FormDesigner,
91+
PageOrganizer
92+
]} />
93+
</PdfViewerComponent>
94+
</div>
95+
</div>
96+
);
97+
}
98+
}
99+
100+
const root = ReactDOM.createRoot(document.getElementById('sample'));
101+
root.render(<App />);

How to/Get co-ordinates of Annotations/src/index.js

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)