Skip to main content Skip to footer

Protect PDF Files With Passwords and Permission in JavaScript

Wijmo's PDF API now allows to restrict access to a PDF document with a password and set permissions for certain operations like print or edit. To support this, Wijmo has a new wijmo.pdf.security module added. PdfDocument class also has four new properties: userPassword, ownerPassword, permissions and version.

You need to add the wijmo.pdf.security module on page in order to be able to use these features. This module adds PDF file encryption and PDF permissions capabilities to wijmo.pdf:

        import { PdfDocument } from '@grapecity/wijmo.pdf';
        import '@grapecity/wijmo.pdf.security';

Lets take a closer look at these new features.

Protecting PDF File With a Password

The userPassword property must be specified to restrict access to PDF file with the password:

        let doc = new PdfDocument({
            userPassword: 'abc'
        });

Users with user password are able to decrypt the file and have full access to the document.

PDF Permissions Setup

The ownerPassword and permissions properties must be specified to set PDF access permissions:

In this example, the permission to copy the file content is given:

        let doc = new PdfDocument({
            ownerPassword: 'abc',
            permissions: {
                copying: true
            }
        });

When only owner password is set, users are able to decrypt and open the document without providing any password, but their access is limited to the operations explicitly permitted by permissions settings. Users with owner password have full access to the document.

When both owner password and user password are set, users with user password are able to decrypt the file but only have limited access to the file according to permissions settings. Users with owner password have full access to the document.

The permissions property allows to specify following settings:

  • annotating: Whether annotating, form filling is allowed.
  • contentAccessibility: Whether copying text for accessibility is allowed.
  • copying: Whether copying text or graphics is allowed.
  • documentAssembly: Whether whether assembling document is allowed.
  • fillingForms: Whether form filling and signing is allowed.
  • modifying: Whether modifying the file is allowed.
  • printing: Whether printing is allowed.

All permissions' properties are Boolean, except the printing property, which is of type PdfPrintPermission and accepts the following values: NotAllowed, AllowLowResolution, AllowHighResolution.

By default, all operations are disallowed. You have to explicitly allow certain operations.

PDF File Version

The version property is of type PdfVersion and specifies PDF version of the file.

PDF file of version 1.4 is created here:

        let doc = new PdfDocument({
            version: PdfVersion.v1_4
        });

When the wijmo.pdf.security module is not imported the version property only affects the value of PDF version in the file header. When that module is imported and any of userPassword, ownerPassword properties are set, the version also specifies:

  1. Encryption algorithm and key length.
  2. Value limitations of userPassword and ownerPassword properties.

Here's a list of possible version values:

  • v1_3: PDF version 1.3, 40-bit RC4.
  • v1_4: PDF version 1.4, 128-bit RC4.
  • v1_5: PDF version 1.5, 128-bit RC4.
  • v1_6: PDF version 1.6, 128-bit AES.
  • v1_7: PDF version 1.7, 128-bit AES.
  • v1_7Ext3: PDF version 1.7 ExtensionLevel 3, 256-bit AES.

When PDF version 1.7 ExtensionLevel 3 is used, password is truncated to 127 bytes of its UTF-8 representation. In older versions, password is truncated to 32 bytes, and only Latin-1 characters are allowed.

The default version is PDF version 1.3.

Microsoft Internet Explorer compatibility

Microsoft Internet Explorer 11 is required. Older versions are not supported.

Microsoft Internet Explorer 11 doesn't have String.prototype.normalize method required by wijmo.pdf.security when PDF version 1.7 ExtensionLevel 3 is used. unorm can be used as a polyfill in this case.

Using with wijmo.grid.pdf

The wijmo.pdf.security module has to be added to page in the same way, the documentOptions property is to be used to pass security settings to FlexGridPdfConverter.export method:

        import { FlexGridPdfConverter } from '@grapecity/wijmo.grid.pdf';
        import '@grapecity/wijmo.pdf.security';

        FlexGridPdfConverter.export(grid, 'grid.pdf', {
            documentOptions: {
                userPassword: 'abc'
            }
        });
Chris Bannon - Global Product Manager

Chris Bannon

Global Product Manager of Wijmo
comments powered by Disqus