Change Password of a PDF File in Node.js

Change Password of a PDF File

In case you want to change password of PDF, you can use AsposePdfChangePassword function. It changes the user password and owner password by owner password, keeping the original security settings. If you want to change the password of a PDF file from “owner” to “newowner” or “newuser” 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 change the password.
  3. Call AsposePdf as Promise and perform the operation for changing password. Receive the object if successful.
  4. Call the function AsposePdfChangePassword.
  5. Change Password. The existing owner password is set to “owner,” and it is changed to “newowner” with the new user password “newuser”.
  6. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfChangePassword.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_encrypt_file = 'ResultEncrypt.pdf';
  AsposePdf().then(AsposePdfModule => {
      /*Change passwords of the PDF-file from "owner" to "newowner" and save the "ResultPdfChangePassword.pdf"*/
      const json = AsposePdfModule.AsposePdfChangePassword(pdf_encrypt_file, "owner", "newuser", "newowner", "ResultPdfChangePassword.pdf");
      console.log("AsposePdfChangePassword => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
  });

Please note that if the password is an empty string:

  1. If the user password is empty - the PDF opens without asking for a password (but it is still encrypted).
  2. If the owner’s password is empty, the PDF opens with a user password request.
  3. If both are empty - the PDF opens without asking for a password (but it is still encrypted).

ECMAScript/ES6:

  1. Import the asposepdfnodejs module.
  2. Specify the name of the PDF file that will change the password.
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfChangePassword.
  5. Change Password. The existing owner password is set to “owner,” and it is changed to “newowner” with the new user password “newuser”.
  6. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfChangePassword.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_encrypt_file = 'ResultEncrypt.pdf';
  /*Change passwords of the PDF-file from "owner" to "newowner" and save the "ResultPdfChangePassword.pdf"*/
  const json = AsposePdfModule.AsposePdfChangePassword(pdf_encrypt_file, "owner", "newuser", "newowner", "ResultPdfChangePassword.pdf");
  console.log("AsposePdfChangePassword => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);