Change Password of a PDF File in Node.js
Contents
[
Hide
]
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:
- Call
require
and importasposepdfnodejs
module asAsposePdf
variable. - Specify the name of the PDF file that will change the password.
- Call
AsposePdf
as Promise and perform the operation for changing password. Receive the object if successful. - Call the function AsposePdfChangePassword.
- Change Password. The existing owner password is set to “owner,” and it is changed to “newowner” with the new user password “newuser”.
- 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:
- If the user password is empty - the PDF opens without asking for a password (but it is still encrypted).
- If the owner’s password is empty, the PDF opens with a user password request.
- If both are empty - the PDF opens without asking for a password (but it is still encrypted).
ECMAScript/ES6:
- Import the
asposepdfnodejs
module. - Specify the name of the PDF file that will change the password.
- Initialize the AsposePdf module. Receive the object if successful.
- Call the function AsposePdfChangePassword.
- Change Password. The existing owner password is set to “owner,” and it is changed to “newowner” with the new user password “newuser”.
- 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);