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

Password-Protect a PDF with AES-256 Encryption

See more PDF Protection Examples

Demonstrates the simplest way to encrypt a PDF so that it needs a password to open. The passwords come from the UserPassword and OwnerPassword properties, and Protection with the encrypt operation and an empty JSON uses the defaults: AES-256, everything encrypted, every permission granted. Protection only records the security the PDF will be saved with. Nothing is encrypted until WriteFile is called.

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

    const pdf = try chilkat.Pdf.init();
    defer pdf.deinit();
    pdf.loadFile("qa_data/pdf/two_page_memo.pdf") catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    // The passwords are taken from these two properties at the time Protection is called.
    // The user password opens the PDF.  The owner password opens it too, and is the one needed
    // later to change or remove the protection.
    pdf.setUserPassword("user-secret");
    pdf.setOwnerPassword("owner-secret");

    // An empty JSON asks for the defaults: AES-256, everything encrypted, every permission granted.
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    const json_out = try chilkat.JsonObject.init();
    defer json_out.deinit();
    pdf.protection("encrypt", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    // Nothing has been encrypted yet.  "pending" describes the security the PDF will be saved with.
    std.debug.print("algorithm: {s}\n", .{try json_out.stringOf(alloc, "pending.algorithm")});
    std.debug.print("PDF version: {s}\n", .{try json_out.stringOf(alloc, "pending.pdfVersion")});
    std.debug.print("password needed to open: {}\n", .{json_out.boolOf("pending.userPasswordRequired")});

    // Writing the file applies it.
    pdf.writeFile("qa_output/memo_encrypted.pdf") catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    // PreLoadCheck reports a file's encryption without loading it, and needs no password.
    const checker = try chilkat.Pdf.init();
    defer checker.deinit();
    const info = try chilkat.JsonObject.init();
    defer info.deinit();
    checker.preLoadCheck("qa_output/memo_encrypted.pdf", info) catch {
        std.debug.print("{s}\n", .{try checker.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("encrypted: {}\n", .{info.boolOf("encrypted")});
    std.debug.print("openPasswordRequired: {}\n", .{info.boolOf("openPasswordRequired")});

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

    std.debug.print("pages: {d}\n", .{reader.getNumPages()});

    // Sample output:

    // algorithm: aes-256
    // PDF version: 1.7
    // password needed to open: True
    // encrypted: True
    // openPasswordRequired: True
    // pages: 2
}