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

PDF Owner Password vs. User Password

See more PDF Protection Examples

Demonstrates the difference between the two passwords of an encrypted PDF. Either one opens the file. The user password gives the rights the permissions grant. The owner password gives owner rights: no restriction applies, and it is the only one that lets the security be changed or removed. Every Protection operation except getInfo needs owner rights, and Chilkat does not offer a way around that.

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

        JsonObject json = new JsonObject();
        JsonObject jsonOut = new JsonObject();

        // Opened with the user password.
        Pdf user = new Pdf();
        user.setUserPassword("secret");
        try {
            user.loadFile("qa_data/pdf/encrypted_user_password.pdf");

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

        System.out.println("opened with: " + jsonOut.stringOf("loaded.openedWith"));

        // Changing the security needs owner rights, so this is refused.
        try {
            user.protection("removeEncryption", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println("removeEncryption was refused: the PDF was not opened with its owner password.");
        }

        // Opened with the owner password.
        Pdf owner = new Pdf();
        owner.setOwnerPassword("owner-secret");
        try {
            owner.loadFile("qa_data/pdf/encrypted_user_password.pdf");

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

        System.out.println("opened with: " + jsonOut.stringOf("loaded.openedWith"));

        // Now it is allowed.
        try {
            owner.protection("removeEncryption", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("removeEncryption succeeded; will be saved encrypted: " + jsonOut.boolOf("pending.encrypted"));

        // Sample output:

        // opened with: user
        // removeEncryption was refused: the PDF was not opened with its owner password.
        // opened with: owner
        // removeEncryption succeeded; will be saved encrypted: False
    }
}