Sample code for 30+ languages & platforms
Zig 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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // 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

    const pdf = try chilkat.Pdf.init();
    defer pdf.deinit();

    // 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");
    pdf.loadFile("qa_data/pdf/encrypted_user_password.pdf") catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    // No printing and no copying.  Everything not mentioned is granted.
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("permissions.print", "none") catch {};
    json.updateBool("permissions.copy", false) catch {};

    const json_out = try chilkat.JsonObject.init();
    defer json_out.deinit();
    pdf.protection("changePermissions", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("print: {s}\n", .{try json_out.stringOf(alloc, "pending.permissions.print")});
    std.debug.print("copy: {}\n", .{json_out.boolOf("pending.permissions.copy")});
    std.debug.print("modify: {}\n", .{json_out.boolOf("pending.permissions.modify")});
    std.debug.print("password still needed to open: {}\n", .{json_out.boolOf("pending.userPasswordRequired")});

    pdf.writeFile("qa_output/minutes_no_print.pdf") catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    // The same user password opens the result, now with the new permissions.
    const reader = try chilkat.Pdf.init();
    defer reader.deinit();
    reader.setUserPassword("secret");
    reader.loadFile("qa_output/minutes_no_print.pdf") catch {
        std.debug.print("{s}\n", .{try reader.getLastErrorText(alloc)});
        return;
    };

    const empty = try chilkat.JsonObject.init();
    defer empty.deinit();
    const info = try chilkat.JsonObject.init();
    defer info.deinit();
    reader.protection("getInfo", empty, info) catch {
        std.debug.print("{s}\n", .{try reader.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("saved file, print: {s}\n", .{try info.stringOf(alloc, "loaded.permissions.print")});
    std.debug.print("saved file, copy: {}\n", .{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
}