Change Password of PDF File

Change Password of a PDF File

In order to change password of a PDF file, you need to create PdfFileSecurity object and then call the ChangePassword method. You need to pass existing owner password and new user and owner passwords to the ChangePassword method.

The following code snippet shows you how to change passwords of a PDF file.

    public static void ChangePassword() {
        PdfFileInfo pdfFileInfo = new PdfFileInfo(_dataDir + "sample_encrypted.pdf");
        // Create PdfFileSecurity object
        if (pdfFileInfo.isEncrypted()) {
            PdfFileSecurity fileSecurity = new PdfFileSecurity();
            fileSecurity.bindPdf(_dataDir + "sample_encrypted.pdf");
            fileSecurity.changePassword("OwnerP@ssw0rd", "Pa$$w0rd1", "Pa$$w0rd2", DocumentPrivilege.getPrint(),
                    KeySize.x256);
            fileSecurity.save(_dataDir + "sample_encrtypted1.pdf");
        }
    }