window.onload = function() { //GcPdfViewer.LicenseKey = "***key***"; var viewer; var React; var ReactDOM; var initialAccessTable = null; function applyAccess(selectedUserName, access) { console.log('Apply access ' + selectedUserName + ': ' + access + ', viewer.currentUserName=' + viewer.currentUserName); if (selectedUserName === viewer.currentUserName) { viewer.findAnnotation(20, // 20 - AnnotationTypeCode.WIDGET { findField: 'annotationType', pageNumberConstraint: 1, findAll: true }).then(function (annotationsDataArr) { if (access === 'LockAll') { viewer.updateAnnotations(0, annotationsDataArr.map(function (data) { data.annotation.locked = true; return data.annotation; })); } else { viewer.updateAnnotations(0, annotationsDataArr.map(function (data) { data.annotation.locked = false; return data.annotation; })); } }); } } function getInitialAccessTable(users) { if(!initialAccessTable) { initialAccessTable = {}; for(var i = 0; i < users.length; i++) { initialAccessTable[users[i]] = 'UnlockAll'; } } return initialAccessTable; } function FieldsLockerPanel(props) { var availableUsers = props.availableUsers; var stateArr1 = React.useState(availableUsers[0] || ''), selectedUserName = stateArr1[0], setSelectedUserName = stateArr1[1]; var stateArr2 = React.useState(getInitialAccessTable(availableUsers)); var accessTable = stateArr2[0], setAccessTable = stateArr2[1]; var userOpts = []; for (var i = 0; i < availableUsers.length; i++) { var curUserName = availableUsers[i]; userOpts.push( /*#__PURE__*/React.createElement('option', { selected: !!(selectedUserName === curUserName), value: curUserName }, curUserName)); } var currentAccess = accessTable[selectedUserName]; return /*#__PURE__*/React.createElement('div', { style: { margin: '30px 30px 30px 30px' }, class: 'fields-locker-panel-outer' }, /*#__PURE__*/React.createElement('p', { class: 'gc-heading__text' }, 'Current user:'), /*#__PURE__*/React.createElement('p', null, /*#__PURE__*/React.createElement('label', { class: 'current-user-label' }, /*#__PURE__*/React.createElement('b', null, props.userName))), /*#__PURE__*/React.createElement('p', { class: 'gc-heading__text' }, 'Select user:'), /*#__PURE__*/React.createElement('p', null, /*#__PURE__*/React.createElement('select', { style: { width: '100%' }, class: 'gc-combo select-user-combo', onChange: function (e) { setSelectedUserName(e.target.value); } }, userOpts), /*#__PURE__*/React.createElement('label', { style: { margin: '10px 10px 10px 10px' } }, 'Access: ', currentAccess)), /*#__PURE__*/React.createElement('p', { class: 'gc-heading__text' }, 'Choose access type:'), /*#__PURE__*/React.createElement('p', null, /*#__PURE__*/React.createElement('select', { style: { width: '100%' }, value: currentAccess, class: 'gc-combo access-combo', onChange: function (e) { var newAccessTable = JSON.parse(JSON.stringify(accessTable)); newAccessTable[selectedUserName] = e.target.value; initialAccessTable = newAccessTable; setAccessTable(newAccessTable); applyAccess(selectedUserName, e.target.value); } }, /*#__PURE__*/React.createElement('option', { selected: currentAccess === 'UnlockAll', value: 'UnlockAll' }, 'Unlock all fields'), /*#__PURE__*/React.createElement('option', { selected: currentAccess === 'LockAll', value: 'LockAll' }, 'Lock all fields'))), /*#__PURE__*/React.createElement('p', null, /*#__PURE__*/React.createElement('button', { style: { width: '100%', textAlign: 'center' }, class: 'gc-btn', onClick: function (e) { viewer.layoutMode = 2; } }, /*#__PURE__*/React.createElement('span', { class: 'gc-btn__text' }, 'Open Form Editor')))); } function createFieldsLockerPanel(userName) { // Create react element: return React.createElement(FieldsLockerPanel, { userName: userName, availableUsers: ['Anonymous', 'James', 'Jame', 'Lisa', 'John'] }); } function createSvgIconElement() { return React.createElement('svg', { xmlns: 'http://www.w3.org/2000/svg', version: '1.1', width: '24', height: '24', viewBox: '0 0 24 24', fill: '#ffffff' }, React.createElement('path', { d: 'M10 13.32q-0.352-0.273-3.184-2.48t-4.316-3.34l7.5-5.82 7.5 5.82q-1.484 1.133-4.297 3.32t-3.203 2.5zM10 15.469l6.133-4.805 1.367 1.055-7.5 5.82-7.5-5.82 1.367-1.055z' })); } viewer = new GcPdfViewer("#viewer", { workerSrc: "/documents-api-pdfviewer/demos/product-bundles/build/gcpdfviewer.worker.js", supportApi: { apiUrl: window.top.SUPPORTAPI_URL, token: window.top.SUPPORTAPI_TOKEN, webSocketUrl: false }, language: "en" }); React = viewer.getType('React'); ReactDOM = viewer.getType('ReactDOM'); viewer.addFormEditorPanel(); accessPanelHandle = viewer.createPanel(createFieldsLockerPanel(viewer.currentUserName || 'World'), null, 'AccessLevel', { icon: { type: 'svg', content: createSvgIconElement() }, label: 'Access level', description: 'Access level administration', visible: false, enabled: false } ); // Add 'AccessLevel' panel to panels layout: viewer.layoutPanels(['AccessLevel', 'FormEditor']); viewer.onAfterOpen.register(()=>{ // Enable 'AccessLevel' panel on document open: viewer.updatePanel(accessPanelHandle, { visible: true, enabled: true }); // Open and pin panel: viewer.leftSidebar.menu.panels.open(accessPanelHandle.id); viewer.leftSidebar.menu.panels.pin(accessPanelHandle.id); }); viewer.open("/documents-api-pdfviewer/demos/product-bundles/assets/pdf/viewer-edit-lock-fields.pdf"); }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Locking fields</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="./src/styles.css"> <script src="/documents-api-pdfviewer/demos/product-bundles/build/gcpdfviewer.js"></script> <script src="./src/app.js"></script> </head> <body> <div id="viewer"></div> </body> </html>
#viewer { height: 100%; }