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

Optimizing a Signed PDF (PreserveSignatures)

See more PDF Optimization Examples

Demonstrates what happens when a signed PDF is optimized. Optimizing rewrites the document, and a rewrite breaks every existing digital signature, so with PreserveSignatures true (the default) Optimize refuses and LastErrorText says why. Setting PreserveSignatures to false allows it, and the signature that was in the file no longer verifies afterwards. "audit" is always allowed, because it changes nothing.

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

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

    std.debug.print("signatures: {d}\n", .{pdf.getNumSignatures()});

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    const json_out = try chilkat.JsonObject.init();
    defer json_out.deinit();

    // An audit changes nothing, so it is allowed on a signed PDF.
    pdf.optimize("audit", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("audit ok, estimated savings: {d}\n", .{json_out.intOf("estimatedSavings.total")});

    // Optimizing is refused while PreserveSignatures is true.
    pdf.optimize("optimize", json, json_out) catch {
        std.debug.print("Refused while PreserveSignatures is true.\n", .{});
    };

    // Allow the rewrite.  Do this only when the signatures no longer matter -- an invoice being
    // archived, say, where the signed original is kept elsewhere.
    pdf.setPreserveSignatures(false);
    pdf.optimize("optimize", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

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

    std.debug.print("optimized, bytesSaved: {d}\n", .{json_out.intOf("bytesSaved")});

    // The signature is still in the file, but it no longer verifies: the bytes it covered have changed.
    const pdf2 = try chilkat.Pdf.init();
    defer pdf2.deinit();
    pdf2.loadFile("qa_output/invoice_optimized.pdf") catch {
        std.debug.print("{s}\n", .{try pdf2.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("signatures after: {d}\n", .{pdf2.getNumSignatures()});

    const sig_info = try chilkat.JsonObject.init();
    defer sig_info.deinit();
    const verified = if (pdf2.verifySignature(0, sig_info)) true else |_| false;
    std.debug.print("verifies after optimizing: {}\n", .{verified});
}