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

    // Load with the current passwords.
    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;
    };

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

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

    // The algorithm and the permissions are kept.
    std.debug.print("algorithm: {s}\n", .{try json_out.stringOf(alloc, "pending.algorithm")});
    std.debug.print("password needed to open: {}\n", .{json_out.boolOf("pending.userPasswordRequired")});

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

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

    reader.protection("getInfo", json, json_out) catch {
        std.debug.print("{s}\n", .{try reader.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("with the new user password, opened with: {s}\n", .{try json_out.stringOf(alloc, "loaded.openedWith")});

    // And so does the new owner password.
    const owner = try chilkat.Pdf.init();
    defer owner.deinit();
    owner.setOwnerPassword("new-owner-secret");
    owner.loadFile("qa_output/minutes_new_passwords.pdf") catch {
        std.debug.print("{s}\n", .{try owner.getLastErrorText(alloc)});
        return;
    };

    owner.protection("getInfo", json, json_out) catch {
        std.debug.print("{s}\n", .{try owner.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("with the new owner password, opened with: {s}\n", .{try json_out.stringOf(alloc, "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
}