|
1 | | -import React, { Component } from 'react'; |
| 1 | +import React, { useRef, useEffect } from 'react'; |
| 2 | +import WebViewer from '@pdftron/webviewer'; |
2 | 3 | import './App.css'; |
3 | 4 |
|
4 | | - |
5 | | -class App extends Component { |
6 | | - constructor() { |
7 | | - super(); |
8 | | - this.viewer = React.createRef(); |
9 | | - this.docViewer = null; |
10 | | - this.annotManager = null; |
11 | | - this.instance = null; |
12 | | - } |
13 | | - |
14 | | - componentDidMount() { |
15 | | - window.WebViewer({ |
16 | | - path: '/lib', |
17 | | - initialDoc: '/files/webviewer-demo-annotated.pdf' |
18 | | - }, this.viewer.current).then(instance => { |
19 | | - // at this point, the viewer is 'ready' |
20 | | - // call methods from instance, docViewer and annotManager as needed |
21 | | - this.instance = instance; |
22 | | - this.docViewer = instance.docViewer; |
23 | | - this.annotManager = instance.annotManager; |
24 | | - |
25 | | - // you can also access major namespaces from the instance as follows: |
26 | | - // var Tools = instance.Tools; |
27 | | - // var Annotations = instance.Annotations; |
28 | | - |
29 | | - // now you can access APIs through `this.instance` |
30 | | - this.instance.openElement('notesPanel') |
31 | | - |
32 | | - // or listen to events from the viewer element |
33 | | - this.viewer.current.addEventListener('pageChanged', (e) => { |
34 | | - const [ pageNumber ] = e.detail; |
35 | | - console.log(`Current page is ${pageNumber}`); |
| 5 | +const App = () => { |
| 6 | + const viewer = useRef(null); |
| 7 | + |
| 8 | + // if using a class, equivalent of componentDidMount |
| 9 | + useEffect(() => { |
| 10 | + WebViewer( |
| 11 | + { |
| 12 | + path: '/webviewer/lib', |
| 13 | + initialDoc: '/files/pdftron_about.pdf', |
| 14 | + }, |
| 15 | + viewer.current, |
| 16 | + ).then((instance) => { |
| 17 | + const { docViewer, Annotations } = instance; |
| 18 | + const annotManager = docViewer.getAnnotationManager(); |
| 19 | + |
| 20 | + docViewer.on('documentLoaded', () => { |
| 21 | + const rectangleAnnot = new Annotations.RectangleAnnotation(); |
| 22 | + rectangleAnnot.PageNumber = 1; |
| 23 | + // values are in page coordinates with (0, 0) in the top left |
| 24 | + rectangleAnnot.X = 100; |
| 25 | + rectangleAnnot.Y = 150; |
| 26 | + rectangleAnnot.Width = 200; |
| 27 | + rectangleAnnot.Height = 50; |
| 28 | + rectangleAnnot.Author = annotManager.getCurrentUser(); |
| 29 | + |
| 30 | + annotManager.addAnnotation(rectangleAnnot); |
| 31 | + // need to draw the annotation otherwise it won't show up until the page is refreshed |
| 32 | + annotManager.redrawAnnotation(rectangleAnnot); |
36 | 33 | }); |
37 | | - |
38 | | - // or from the docViewer instance |
39 | | - this.docViewer.on('annotationsLoaded', () => { |
40 | | - console.log('annotations loaded'); |
41 | | - }); |
42 | | - |
43 | | - this.docViewer.on('documentLoaded', this.wvDocumentLoadedHandler) |
44 | | - }) |
45 | | - } |
46 | | - |
47 | | - |
48 | | - wvDocumentLoadedHandler = () => { |
49 | | - // call methods relating to the loaded document |
50 | | - const { Annotations } = this.instance; |
51 | | - const rectangle = new Annotations.RectangleAnnotation(); |
52 | | - rectangle.PageNumber = 1; |
53 | | - rectangle.X = 100; |
54 | | - rectangle.Y = 100; |
55 | | - rectangle.Width = 250; |
56 | | - rectangle.Height = 250; |
57 | | - rectangle.StrokeThickness = 5; |
58 | | - rectangle.Author = this.annotManager.getCurrentUser(); |
59 | | - this.annotManager.addAnnotation(rectangle); |
60 | | - this.annotManager.drawAnnotations(rectangle.PageNumber); |
61 | | - // see https://www.pdftron.com/api/web/WebViewer.html for the full list of low-level APIs |
62 | | - } |
63 | | - |
64 | | - render() { |
65 | | - return ( |
66 | | - <div className="App"> |
67 | | - <div className="header">React sample</div> |
68 | | - <div className="webviewer" ref={this.viewer}></div> |
69 | | - </div> |
70 | | - ); |
71 | | - } |
72 | | -} |
| 34 | + }); |
| 35 | + }, []); |
| 36 | + |
| 37 | + return ( |
| 38 | + <div className="App"> |
| 39 | + <div className="header">React sample</div> |
| 40 | + <div className="webviewer" ref={viewer}></div> |
| 41 | + </div> |
| 42 | + ); |
| 43 | +}; |
73 | 44 |
|
74 | 45 | export default App; |
0 commit comments