Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button fields remain disabled after removing read only flag #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"private": true,
"homepage": "https://pdftron.github.io/webviewer-react-sample",
"dependencies": {
"@pdftron/webviewer": "^10.6.0",
"react": "^16.6.0",
"react-dom": "^16.6.0",
"react-scripts": "2.1.1",
"@pdftron/webviewer": "^10.0.0"
"react-scripts": "2.1.1"
},
"devDependencies": {
"btoa": "^1.2.1",
Expand All @@ -33,4 +33,4 @@
"not ie <= 11",
"not op_mini all"
]
}
}
Binary file added public/files/sample_pdf.pdf
Binary file not shown.
1 change: 1 addition & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
font-size: 1.2em;
line-height: 44px;
color: white;
text-align: right;
}
60 changes: 37 additions & 23 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,57 @@
import React, { useRef, useEffect } from 'react';
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)

// if using a class, equivalent of componentDidMount
useEffect(() => {
WebViewer(
WebViewer.Iframe(
{
path: '/webviewer/lib',
initialDoc: '/files/PDFTRON_about.pdf',
licenseKey: 'your_license_key' // sign up to get a free trial key at https://dev.apryse.com
initialDoc: '/files/sample_pdf.pdf',
licenseKey: 'your_license_key',
},
viewer.current,
).then((instance) => {
const { documentViewer, annotationManager, Annotations } = instance.Core;

documentViewer.addEventListener('documentLoaded', () => {
const rectangleAnnot = new Annotations.RectangleAnnotation({
PageNumber: 1,
// values are in page coordinates with (0, 0) in the top left
X: 100,
Y: 150,
Width: 200,
Height: 50,
Author: annotationManager.getCurrentUser()
});

annotationManager.addAnnotation(rectangleAnnot);
// need to draw the annotation otherwise it won't show up until the page is refreshed
annotationManager.redrawAnnotation(rectangleAnnot);
});
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">React sample</div>
<div className="header"><button onClick={() => setIsEditingMode(isEditingMode => !isEditingMode)}>{isEditingMode ? 'Disable Editing' : 'Enable Editing'}</button></div>
<div className="webviewer" ref={viewer}></div>
</div>
);
Expand Down