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

A PDF Label That Is Not on the Page: notFound or Ignore

See more PDF Text Replacement Examples

Demonstrates the "unmatchedValues" option. A label or placeholder given a value but not found on the selected pages is a problem with the reason notFound, which stops "apply", because a missing label usually means the wrong document or a typo in the label. With unmatchedValues "ignore" such a value is skipped and the rest is applied -- the right choice when one set of values serves several document layouts.

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

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

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

    // "Tracking Number:" is not on this invoice.
    json.updateString("labels[0].label", "Invoice Number:") catch {};
    json.updateString("labels[0].value", "INV-2026-0500") catch {};
    json.updateString("labels[1].label", "Tracking Number:") catch {};
    json.updateString("labels[1].value", "1Z999AA10123456784") catch {};

    pdf.textReplace("check", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("default: ok = {}, problem: \"{s}\" {s}\n", .{ json_out.boolOf("ok"), try json_out.stringOf(alloc, "problems[0].label"), try json_out.stringOf(alloc, "problems[0].reason") });

    // With "ignore", the label that is not there is skipped.
    json.updateString("unmatchedValues", "ignore") catch {};
    pdf.textReplace("apply", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("ignore: applied = {}, replacements: {d}\n", .{ json_out.boolOf("applied"), json_out.sizeOfArray("replacements") });

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

    // Sample output:

    // default: ok = false, problem: "Tracking Number:" notFound
    // ignore: applied = true, replacements: 1
}