|
1 | 1 | import React, { Component } from 'react';
|
2 | 2 | import './App.css';
|
3 |
| -import WebViewer from './WebViewer'; |
| 3 | + |
4 | 4 |
|
5 | 5 | class App extends Component {
|
6 | 6 | constructor() {
|
7 | 7 | super();
|
8 |
| - this.webviewer = React.createRef(); |
| 8 | + this.viewer = React.createRef(); |
| 9 | + this.docViewer = null; |
| 10 | + this.annotManager = null; |
| 11 | + this.instance = null; |
9 | 12 | }
|
10 | 13 |
|
11 | 14 | componentDidMount() {
|
12 |
| - this.webviewer.current.getElement().addEventListener('ready', this.wvReadyHandler); |
13 |
| - this.webviewer.current.getElement().addEventListener('documentLoaded', this.wvDocumentLoadedHandler); |
14 |
| - } |
| 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') |
15 | 31 |
|
16 |
| - wvReadyHandler = () => { |
17 |
| - // now you can access APIs through this.webviewer.current.getInstance() |
18 |
| - this.webviewer.current.getInstance().openElement('notesPanel'); |
19 |
| - // see https://www.pdftron.com/documentation/web/guides/ui/apis for the full list of APIs |
20 |
| - |
21 |
| - // or listen to events from the viewer element |
22 |
| - this.webviewer.current.getElement().addEventListener('pageChanged', (e) => { |
23 |
| - const [ pageNumber ] = e.detail; |
24 |
| - console.log(`Current page is ${pageNumber}`); |
25 |
| - }); |
26 |
| - |
27 |
| - // or from the docViewer instance |
28 |
| - this.webviewer.current.getInstance().docViewer.on('annotationsLoaded', () => { |
29 |
| - console.log('annotations loaded'); |
30 |
| - }); |
| 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}`); |
| 36 | + }); |
| 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 | + }) |
31 | 45 | }
|
32 | 46 |
|
| 47 | + |
33 | 48 | wvDocumentLoadedHandler = () => {
|
34 |
| - // you can access docViewer object for low-level APIs |
35 |
| - const { docViewer } = this.webviewer.current.getInstance(); |
36 |
| - const annotManager = docViewer.getAnnotationManager(); |
37 |
| - // and access classes defined in the WebViewer iframe |
38 |
| - const { Annotations } = this.webviewer.current.getWindow(); |
| 49 | + // call methods relating to the loaded document |
| 50 | + const { Annotations } = this.instance; |
39 | 51 | const rectangle = new Annotations.RectangleAnnotation();
|
40 | 52 | rectangle.PageNumber = 1;
|
41 | 53 | rectangle.X = 100;
|
42 | 54 | rectangle.Y = 100;
|
43 | 55 | rectangle.Width = 250;
|
44 | 56 | rectangle.Height = 250;
|
45 | 57 | rectangle.StrokeThickness = 5;
|
46 |
| - rectangle.Author = annotManager.getCurrentUser(); |
47 |
| - annotManager.addAnnotation(rectangle); |
48 |
| - annotManager.drawAnnotations(rectangle.PageNumber); |
49 |
| - // see https://www.pdftron.com/api/web/PDFTron.WebViewer.html for the full list of low-level APIs |
| 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 |
50 | 62 | }
|
51 | 63 |
|
52 | 64 | render() {
|
53 | 65 | return (
|
54 | 66 | <div className="App">
|
55 | 67 | <div className="header">React sample</div>
|
56 |
| - <WebViewer ref={this.webviewer}/> |
| 68 | + <div className="webviewer" ref={this.viewer}></div> |
57 | 69 | </div>
|
58 | 70 | );
|
59 | 71 | }
|
|
0 commit comments