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

Replace Text in a Signed PDF (PreserveSignatures)

See more PDF Text Replacement Examples

Applying a text replacement rewrites the page content, which invalidates any digital signature on the PDF. While the PreserveSignatures property is true (the default), "apply" is refused on a signed PDF; "check" only reads and is always allowed. Setting PreserveSignatures to false allows the replacement; the signature field stays in the written PDF but no longer verifies, so the document should be signed again afterwards if a signature is wanted.

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();
    json.updateString("labels[0].label", "Invoice Number:") catch {};
    json.updateString("labels[0].value", "INV-2026-0500") catch {};

    // "check" reads only, so it works on a signed PDF.
    const json_out = try chilkat.JsonObject.init();
    defer json_out.deinit();
    pdf.textReplace("check", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("check: ok = {}\n", .{json_out.boolOf("ok")});

    // "apply" is refused while PreserveSignatures is true (the default).
    pdf.textReplace("apply", json, json_out) catch {
        std.debug.print("apply refused: {s}\n", .{try pdf.getLastErrorText(alloc)});
    };

    // Allowing the signature to be broken:
    pdf.setPreserveSignatures(false);
    pdf.textReplace("apply", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("applied: {}\n", .{json_out.boolOf("applied")});

    // The written PDF is a full rewrite with the replacement.  The signature field is still in the document,
    // but the bytes it signed are gone, so it no longer verifies.
    pdf.writeFile("qa_output/invoice_reissued_broken_signature.pdf") catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Signatures after writing: {d}\n", .{pdf.getNumSignatures()});
    const sig_info = try chilkat.JsonObject.init();
    defer sig_info.deinit();
    const validated = if (pdf.verifySignature(0, sig_info)) true else |_| false;
    std.debug.print("Signature 0 verifies: {}\n", .{validated});

    // Sample output:

    // Signatures: 1
    // check: ok = true
    // apply refused: ChilkatLog:
    //   TextReplace:
    //     DllDate: Nov  7 2026
    //     ChilkatVersion: 11.7.0
    //     UnlockPrefix: CHLKAT
    //     UnlockStatus: 2
    //     Architecture: Little Endian; 64-bit
    //     Language: Visual C++ 11.0 / x64
    //     VerboseLogging: 0
    //     replaceTextOp:
    //       op: apply
    //       The PDF is signed: replacing text rewrites the content and invalidates the signatures (set PreserveSignatures = false to allow it).
    //     --replaceTextOp
    //     Failed.
    //   --TextReplace
    // --ChilkatLog
    //
    // applied: true
    // Signatures after writing: 1
    // Signature 0 verifies: false
}