This repository was archived by the owner on Feb 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathApp.js
More file actions
60 lines (50 loc) · 1.72 KB
/
App.js
File metadata and controls
60 lines (50 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import React, { useRef, useEffect, useState, useCallback } from 'react';
import WebViewer from '@pdftron/webviewer';
import './App.css';
const App = () => {
const viewer = useRef(null);
const [instance, setInstance] = useState(null);
const [isEditingMode, setIsEditingMode] = useState(false)
useEffect(() => {
WebViewer.Iframe(
{
path: '/webviewer/lib',
initialDoc: '/files/sample_pdf.pdf',
licenseKey: 'your_license_key',
},
viewer.current,
).then((instance) => {
setInstance(instance);
});
}, []);
const enableCustomReadOnlyMode = useCallback(
(isReadOnlyMode = !isEditingMode) => {
const { annotationManager } = instance.Core;
const fieldManager = annotationManager.getFieldManager();
fieldManager.forEachField(field => {
field.flags.set("ReadOnly", isReadOnlyMode);
});
},
[instance, isEditingMode]
);
useEffect(() => {
if (instance) {
const { documentViewer } = instance.Core;
// For the first time when pdf loads
documentViewer.addEventListener("annotationsLoaded", enableCustomReadOnlyMode);
// For the subsequent loads when switched between edit and read-only modes.
enableCustomReadOnlyMode();
return () => {
documentViewer.removeEventListener("annotationsLoaded", enableCustomReadOnlyMode);
};
}
return undefined;
}, [instance, enableCustomReadOnlyMode]);
return (
<div className="App">
<div className="header"><button onClick={() => setIsEditingMode(isEditingMode => !isEditingMode)}>{isEditingMode ? 'Disable Editing' : 'Enable Editing'}</button></div>
<div className="webviewer" ref={viewer}></div>
</div>
);
};
export default App;