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

PDF Placeholders Without a Value: Error, Blank or Keep

See more PDF Text Replacement Examples

Demonstrates the "unusedPlaceholders" option, which decides what happens to a placeholder found on the page that has no value in "placeholders": "error" (the default) makes it a problem with the reason unusedPlaceholder, so nothing is applied; "blank" replaces it with nothing; "keep" leaves it on the page as it is.

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

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

    // Values for every placeholder except {{po}} (the purchase order number, which this customer has none of).
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("placeholders.customer", "Acme Corp") catch {};
    json.updateString("placeholders.date", "2026-10-18") catch {};
    json.updateString("placeholders.qty1", "2") catch {};
    json.updateString("placeholders.desc1", "Cordless drill, 18 V") catch {};
    json.updateString("placeholders.price1", "$460.00") catch {};
    json.updateString("placeholders.amount1", "$920.00") catch {};
    json.updateString("placeholders.qty2", "6") catch {};
    json.updateString("placeholders.desc2", "Galvanized hinge, 4 inch") catch {};
    json.updateString("placeholders.price2", "$12.50") catch {};
    json.updateString("placeholders.amount2", "$75.00") catch {};
    json.updateString("placeholders.subtotal", "$995.00") catch {};
    json.updateString("placeholders.taxRate", "7.35%") catch {};
    json.updateString("placeholders.tax", "$73.13") catch {};
    json.updateString("placeholders.total", "$1,068.13") catch {};

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

    // The default, "error": the unfilled {{po}} is a problem and nothing would be applied.
    pdf.textReplace("check", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

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

    // "keep": {{po}} stays on the page, literally.
    json.updateString("unusedPlaceholders", "keep") catch {};
    pdf.textReplace("check", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

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

    // "blank": {{po}} is replaced by nothing, leaving "PO Number:" with an empty value.
    json.updateString("unusedPlaceholders", "blank") catch {};
    pdf.textReplace("apply", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

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

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

    // Sample output:

    // error (default): ok = false, problem: po unusedPlaceholder
    // keep: ok = true
    // blank: applied = true
}