Encrypt PDF File

Encrypt PDF File using Different Encryption Types and Algorithms

In order to encrypt a PDF file, you need to create PdfFileSecurity object and then call the EncryptFile method. You can pass user password, owner password and privileges to EncryptFile method. You also need to pass KeySize and Algorithm values to this method.

The following code snippet shows you how to encrypt PDF file.

    public static void EncryptPDFFile() {
        // Create PdfFileSecurity object
        PdfFileSecurity fileSecurity = new PdfFileSecurity();
        fileSecurity.bindPdf(_dataDir + "sample.pdf");
        // Encrypt file using 256-bit encryption
        fileSecurity.encryptFile("User_P@ssw0rd", "OwnerP@ssw0rd", DocumentPrivilege.getPrint(), KeySize.x256,
                Algorithm.AES);
        fileSecurity.save(_dataDir + "sample_encrypted.pdf");
    }