Sample code for 30+ languages & platforms
Java Requires Chilkat v11.7.0+

Password-Protect a PDF with AES-256 Encryption

See more PDF Protection Examples

Demonstrates the simplest way to encrypt a PDF so that it needs a password to open. The passwords come from the UserPassword and OwnerPassword properties, and Protection with the encrypt operation and an empty JSON uses the defaults: AES-256, everything encrypted, every permission granted. Protection only records the security the PDF will be saved with. Nothing is encrypted until WriteFile is called.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Pdf;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // The PDF used in this example is available at two_page_memo.pdf

        Pdf pdf = new Pdf();
        try {
            pdf.loadFile("qa_data/pdf/two_page_memo.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // The passwords are taken from these two properties at the time Protection is called.
        // The user password opens the PDF.  The owner password opens it too, and is the one needed
        // later to change or remove the protection.
        pdf.setUserPassword("user-secret");
        pdf.setOwnerPassword("owner-secret");

        // An empty JSON asks for the defaults: AES-256, everything encrypted, every permission granted.
        JsonObject json = new JsonObject();
        JsonObject jsonOut = new JsonObject();
        try {
            pdf.protection("encrypt", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Nothing has been encrypted yet.  "pending" describes the security the PDF will be saved with.
        System.out.println("algorithm: " + jsonOut.stringOf("pending.algorithm"));
        System.out.println("PDF version: " + jsonOut.stringOf("pending.pdfVersion"));
        System.out.println("password needed to open: " + jsonOut.boolOf("pending.userPasswordRequired"));

        // Writing the file applies it.
        try {
            pdf.writeFile("qa_output/memo_encrypted.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // PreLoadCheck reports a file's encryption without loading it, and needs no password.
        Pdf checker = new Pdf();
        JsonObject info = new JsonObject();
        try {
            checker.preLoadCheck("qa_output/memo_encrypted.pdf", info);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("encrypted: " + info.boolOf("encrypted"));
        System.out.println("openPasswordRequired: " + info.boolOf("openPasswordRequired"));

        // With the password, the PDF opens normally.
        Pdf reader = new Pdf();
        reader.setUserPassword("user-secret");
        try {
            reader.loadFile("qa_output/memo_encrypted.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("pages: " + reader.getNumPages());

        // Sample output:

        // algorithm: aes-256
        // PDF version: 1.7
        // password needed to open: True
        // encrypted: True
        // openPasswordRequired: True
        // pages: 2
    }
}