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

Change the Passwords of an Encrypted PDF

See more PDF Protection Examples

Demonstrates the changePasswords operation, which gives an encrypted PDF new user and owner passwords and keeps its algorithm and permissions. The PDF is loaded with its current passwords, the two properties are then set to the new ones, and Protection reads them at the time of the call. Both passwords are replaced, so set both: a property left empty becomes an empty password.

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 encrypted_user_password.pdf

        Pdf pdf = new Pdf();

        // Load with the current passwords.
        pdf.setUserPassword("secret");
        pdf.setOwnerPassword("owner-secret");
        try {
            pdf.loadFile("qa_data/pdf/encrypted_user_password.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // The new passwords.  Protection reads the properties as they are when it is called.
        pdf.setUserPassword("new-user-secret");
        pdf.setOwnerPassword("new-owner-secret");

        JsonObject json = new JsonObject();
        JsonObject jsonOut = new JsonObject();
        try {
            pdf.protection("changePasswords", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // The algorithm and the permissions are kept.
        System.out.println("algorithm: " + jsonOut.stringOf("pending.algorithm"));
        System.out.println("password needed to open: " + jsonOut.boolOf("pending.userPasswordRequired"));

        try {
            pdf.writeFile("qa_output/minutes_new_passwords.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // The new user password opens the result.
        Pdf reader = new Pdf();
        reader.setUserPassword("new-user-secret");
        try {
            reader.loadFile("qa_output/minutes_new_passwords.pdf");

            reader.protection("getInfo", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("with the new user password, opened with: " + jsonOut.stringOf("loaded.openedWith"));

        // And so does the new owner password.
        Pdf owner = new Pdf();
        owner.setOwnerPassword("new-owner-secret");
        try {
            owner.loadFile("qa_output/minutes_new_passwords.pdf");

            owner.protection("getInfo", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("with the new owner password, opened with: " + jsonOut.stringOf("loaded.openedWith"));

        // Sample output:

        // algorithm: rc4-128
        // password needed to open: True
        // with the new user password, opened with: user
        // with the new owner password, opened with: owner
    }
}