Some test text!

Search
Hamburger Icon

Web / Guides / Decrypt

Decrypt & read a PDF document using JavaScript

Make sure you have Full API enabled in WebViewer.

To read a PDF document with password protection.

<html>
  <script src="../lib/core/webviewer-core.min.js"></script>
  <script src="../lib/core/pdf/PDFNet.js"></script>
  <script>
    (async function() {
      Core.setWorkerPath('../lib/core');
      const securedDoc = await PDFNet.PDFDoc.createFromURL(filename);

      // At this point MySecurityHandler callbacks will be invoked.
      // MySecurityHandler.GetAuthorizationData() should collect the password and
      // AuthorizeFailed() is called if user repeatedly enters a wrong password.
      if ((await securedDoc.initStdSecurityHandlerUString(password))) {
        // The password is correct! Document can now be used for reading and editing
      }
    })()
  </script>
</html>

Encrypt and Decrypt PDF Files
Full sample code which illustrates some of our encryption support.

About reading a secured document

Apryse SDK fully supports the reading of secured and encrypted PDF documents. To test whether a document requires a password, check the return value of PDFDoc.InitSecurityHandler() as shown above. Because InitSecurityHandler() doesn't have any side effects on documents that are not encrypted you should always invoke this method, or InitStdSecurityHandler(), after constructing a document.

If a document doesn't require authentication data (such as a user password) in order to view its content, InitSecurityHandler() is enough to work with encrypted documents. If, on the other hand, the document requires a password, InitStdSecurityHandler allows you to provide one.

After the document's security handler is initialized, you can access it using the doc.GetSecurityHandler() method. You can edit permissions and authorization data on an existing handler, or set a completely new security handler using the doc.SetSecurityHandler(handler) method.

Removing security from a document

To remove PDF security, call the RemoveSecurity() method:

<html>
  <script src="../lib/core/webviewer-core.min.js"></script>
  <script src="../lib/core/pdf/PDFNet.js"></script>
  <script>
    (async function() {
      Core.setWorkerPath('../lib/core');
      const doc = await PDFNet.PDFDoc.createFromURL(filename);
      await doc.initSecurityHandler();
      doc.removeSecurity();
    })()
  </script>
</html>

A document may have both a current and a new security handler associated with it because a PDF document is not fully loaded in memory and decrypted when it is loaded. To fully decrypt the document, even after applying a new security handler, the original security handler is still required.

PDF security permissions

The security mechanism for the high-level document works in the same way as with an SDF document. To secure a document, use the PDFDoc.SetSecurityHandler() method. To open a secured document, call PDFDoc.InitSecurityHandler(). To open a document with a password, call PDFDoc.InitStdSecurityHandler().

The following table lists security permissions available through the standard security handler:

PermissionDescription
e_ownerAll permissions are granted.
e_doc_openA permission to open a document.
e_doc_modifyModify the contents of the document.
e_printPrint the document.
e_print_highPrint the document to a representation from which a faithful digital copy of the PDF content could be generated. When this permission is not set, printing is limited to a low level representation of the appearance, possibly of degraded quality.
e_extract_contentCopy or otherwise extract text and graphics from the document.
e_mod_annotAdd or modify text annotations, fill in interactive form fields.
e_fill_formsFill in existing interactive form fields (including signature fields).
e_access_supportExtract text and graphics (in support of accessibility to disabled users or for other purposes).
e_assemble_docAssemble the document (insert, rotate, or delete pages and create bookmarks or thumbnail images), even if e_doc_modify is not set.

Trial setup questions? Ask experts on Discord
Need other help? Contact Support
Pricing or product questions? Contact Sales