Skip to content
Guides

Encrypt and Protect PDF

This sample shows how to set permission passwords for opening documents and decrypt them.

javascript
// Import the JS file of ComPDFKit Web Demo.
import ComPDFKitViewer from "/@compdfkit/webviewer";

const viewer = document.getElementById('webviewer');
ComPDFKitViewer.init({
  pdfUrl: 'Your PDF Url',
  license: 'Input your license here'
}, viewer)
.then((core) => {
  const docViewer = core.docViewer;
  docViewer.addEvent('documentloaded', () => {
    console.log('ComPDFKit Web Demo loaded');

    // Sample1: Encrypt
    if (docViewer.setPassword('password')) {
      console.log('File is encrypted.');
    } else {
      console.log('File encrypt failed');
    }

    // Sample2: Unlock
    if (docViewer.removePassword('password')) {
      console.log('File is unlocked.');
    } else {
      console.log('File unlock failed');
    }
  })
})