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

Change the Permissions of an Encrypted PDF

See more PDF Protection Examples

Demonstrates the changePermissions operation, which changes what an encrypted PDF permits and keeps its passwords, algorithm and scope. The JSON takes the same permissions member as the encrypt operation, and a permission that is not mentioned is granted. The operation needs owner rights. For an AES-256 PDF, load with both passwords, because the user password cannot be recovered from the owner password alone.

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();

        // Changing the permissions needs owner rights.  Both passwords are given here, which is what
        // an AES-256 PDF requires for this operation.
        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;
        }

        // No printing and no copying.  Everything not mentioned is granted.
        JsonObject json = new JsonObject();
        json.updateString("permissions.print", "none");
        json.updateBool("permissions.copy", false);

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

        System.out.println("print: " + jsonOut.stringOf("pending.permissions.print"));
        System.out.println("copy: " + jsonOut.boolOf("pending.permissions.copy"));
        System.out.println("modify: " + jsonOut.boolOf("pending.permissions.modify"));
        System.out.println("password still needed to open: " + jsonOut.boolOf("pending.userPasswordRequired"));

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

        // The same user password opens the result, now with the new permissions.
        Pdf reader = new Pdf();
        reader.setUserPassword("secret");
        try {
            reader.loadFile("qa_output/minutes_no_print.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        JsonObject empty = new JsonObject();
        JsonObject info = new JsonObject();
        try {
            reader.protection("getInfo", empty, info);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("saved file, print: " + info.stringOf("loaded.permissions.print"));
        System.out.println("saved file, copy: " + info.boolOf("loaded.permissions.copy"));

        // Sample output:

        // print: none
        // copy: False
        // modify: True
        // password still needed to open: True
        // saved file, print: none
        // saved file, copy: False
    }
}