Prevent editing signed PDF

This example shows how to prevent changing an electronically signed PDF

function getViewerOptions() { return { workerSrc: "/document-solutions/javascript-pdf-viewer/demos/product-bundles/build/dspdfviewer.worker.js", supportApi: { apiUrl: window.top.SUPPORTAPI_URL, token: window.top.SUPPORTAPI_TOKEN, webSocketUrl: false } }; } function configureViewerUI(viewer) { var documentSigned = false; viewer.addDefaultPanels(); viewer.addAnnotationEditorPanel(); viewer.addFormEditorPanel(); viewer.onAfterOpen.register(function() { viewer.getSignatureInfo().then(function (info) { documentSigned = info.signed; }); }); // Cancel edit events if the current document is electronically signed: const onBeforeEditHandler = (args) => { const cancel = documentSigned && args.annotation.annotationType !== 13 /* Allow editing stamps */; if(cancel) { args.cancel = true; setTimeout(()=> { viewer.showMessage("Cannot edit a signed document.", "The document was signed with an electronic digital signature.", "warn"); viewer.hideSecondToolbar(); viewer.layoutMode = 0; viewer.editMode = 0; viewer.repaint(); }, 200); } }; viewer.onBeforeAddAnnotation.register(onBeforeEditHandler); viewer.onBeforeUpdateAnnotation.register(onBeforeEditHandler); viewer.onBeforeRemoveAnnotation.register(onBeforeEditHandler); } window.onload = function() { var viewer = new DsPdfViewer("#viewer", getViewerOptions()); configureViewerUI(viewer); viewer.open("/document-solutions/javascript-pdf-viewer/demos/product-bundles/assets/pdf/prevent-editing-signed-pdf.pdf"); }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Prevent editing signed PDF</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="./src/styles.css"> <script src="/document-solutions/javascript-pdf-viewer/demos/product-bundles/build/dspdfviewer.js"></script> <script src="/document-solutions/javascript-pdf-viewer/demos/product-bundles/build/wasmSupportApi.js"></script> <script src="/document-solutions/javascript-pdf-viewer/demos/resource/js/init.js"></script> <script src="./src/app.js"></script> </head> <body> <div id="viewer"></div> </body> </html>
#viewer { height: 100%; }