Encrypt PDF in Node.js

Encrypt PDF File using using User or Owner Password

If you are sending an email to someone with a PDF attachment that contains confidential information, you might wish to add some security to it first to hold it falling into the incorrect hands. The best way to limit unauthorized access to a PDF document is to defend it with a password. To easily and securely encrypt documents, you can use Aspose.PDF for Node.js via C++.

Please specify different user and owner passwords while encrypting the PDF file.

  • The User password, if set, is what you need to provide in order to open a PDF. Acrobat/Reader will prompt a user to enter the user password. If it’s not correct, the document will not open.
  • The Owner password, if set, controls permissions, such as printing, editing, extracting, commenting, etc. Acrobat/Reader will disallow these things based on the permission settings. Acrobat will require this password if you want to set/change permissions.

In case you want to encrypt PDF file, you can use AsposePdfEncrypt function. If you want to encrypt PDF file try the next code snippet:

CommonJS:

  1. Call require and import asposepdfnodejs module as AsposePdf variable.
  2. Specify the name of the PDF file that will be encrypted.
  3. Call AsposePdf as Promise and perform the operation for encrypting file. Receive the object if successful.
  4. Call the AsposePdfEncrypt function.
  5. Encrypt PDF file with passwords “user” and “owner”.
  6. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultEncrypt.pdf”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.

  const AsposePdf = require('asposepdfnodejs');
  const pdf_file = 'Aspose.pdf';
  AsposePdf().then(AsposePdfModule => {
      /*Encrypt a PDF-file with passwords "user" and "owner", and save the "ResultEncrypt.pdf"*/
      const json = AsposePdfModule.AsposePdfEncrypt(pdf_file, "user", "owner", AsposePdfModule.Permissions.PrintDocument, AsposePdfModule.CryptoAlgorithm.RC4x40, "ResultEncrypt.pdf");
      console.log("AsposePdfEncrypt => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
  });

There are different encrypt permissions:

  • Module.Permissions.PrintDocument
  • Module.Permissions.ModifyContent
  • Module.Permissions.ExtractContent
  • Module.Permissions.ModifyTextAnnotations
  • Module.Permissions.FillForm
  • Module.Permissions.ExtractContentWithDisabilities
  • Module.Permissions.AssembleDocument
  • Module.Permissions.PrintingQuality

There are various encrypt algorithms:

  • Module.CryptoAlgorithm.RC4x40
  • Module.CryptoAlgorithm.RC4x128
  • Module.CryptoAlgorithm.AESx128
  • Module.CryptoAlgorithm.AESx256

ECMAScript/ES6:

  1. Import the asposepdfnodejs module.
  2. Specify the name of the PDF file that will change the encrypted.
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the AsposePdfEncrypt function.
  5. Decrypt PDF file with passwords “user” and “owner”.
  6. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultEncrypt.pdf”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.

  import AsposePdf from 'asposepdfnodejs';
  const AsposePdfModule = await AsposePdf();
  const pdf_file = 'Aspose.pdf';
  /*Encrypt a PDF-file with passwords "user" and "owner", and save the "ResultEncrypt.pdf"*/
  const json = AsposePdfModule.AsposePdfEncrypt(pdf_file, "user", "owner", AsposePdfModule.Permissions.PrintDocument, AsposePdfModule.CryptoAlgorithm.RC4x40, "ResultEncrypt.pdf");
  console.log("AsposePdfEncrypt => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);