Skip to content
This repository was archived by the owner on Feb 19, 2025. It is now read-only.

Commit 9eacc71

Browse files
Merge pull request #4 from PDFTron/react-sample-update
Updated React sample to use latest WebViewer npm module and React Hooks
2 parents 92edf49 + f5fe3b2 commit 9eacc71

File tree

6 files changed

+56
-76
lines changed

6 files changed

+56
-76
lines changed

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
22

33
# WebViewer
4-
public/lib
5-
public/license-key.js
6-
WebViewer.zip
4+
public/webviewer
75

86
# dependencies
97
/node_modules

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
"dependencies": {
66
"react": "^16.6.0",
77
"react-dom": "^16.6.0",
8-
"react-scripts": "2.1.1"
8+
"react-scripts": "2.1.1",
9+
"@pdftron/webviewer": "^6.2.3"
910
},
1011
"devDependencies": {
11-
"@pdftron/webviewer-downloader": "^1.0.7",
1212
"btoa": "^1.2.1",
1313
"download": "^7.1.0",
1414
"fs-extra": "^7.0.1"
@@ -18,8 +18,7 @@
1818
"build": "react-scripts build",
1919
"test": "react-scripts test",
2020
"eject": "react-scripts eject",
21-
"postinstall": "npm run download-webviewer",
22-
"download-webviewer": "npx @pdftron/webviewer-downloader"
21+
"postinstall": "node tools/copy-webviewer-files.js"
2322
},
2423
"eslintConfig": {
2524
"extends": "react-app"

public/files/PDFTRON_about.pdf

374 KB
Binary file not shown.
-905 KB
Binary file not shown.

src/App.js

Lines changed: 40 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,45 @@
1-
import React, { Component } from 'react';
1+
import React, { useRef, useEffect } from 'react';
2+
import WebViewer from '@pdftron/webviewer';
23
import './App.css';
34

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);
3633
});
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+
};
7344

7445
export default App;

tools/copy-webviewer-files.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const fs = require('fs-extra');
2+
3+
const copyFiles = async () => {
4+
try {
5+
await fs.copy('./node_modules/@pdftron/webviewer/public', './public/webviewer/lib');
6+
console.log('WebViewer files copied over successfully');
7+
} catch (err) {
8+
console.error(err);
9+
}
10+
};
11+
12+
copyFiles();

0 commit comments

Comments
 (0)