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

PDF Placeholders with Other Delimiters: [[name]]

See more PDF Text Replacement Examples

Demonstrates the "placeholderSyntax" option. Placeholders are {{name}} by default; a template written with other delimiters, such as [[name]] or <>, says so with placeholderSyntax, written around the word "name".

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();

    var success: bool = false;

    // 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 letter_template.pdf

    const pdf = try chilkat.Pdf.init();
    defer pdf.deinit();
    success = if (pdf.loadFile("qa_data/pdf/letter_template.pdf")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    }

    // The letter reads: "Dear [[name]]," ... "expires on [[expiry]]" ... "[[amount]] per year" ... "[[agent]]".
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("placeholderSyntax", "[[name]]") catch {};
    json.updateString("placeholders.date", "October 18, 2026") catch {};
    json.updateString("placeholders.name", "Ms. Rivera") catch {};
    json.updateString("placeholders.expiry", "December 31, 2026") catch {};
    json.updateString("placeholders.amount", "$299.00") catch {};
    json.updateString("placeholders.agent", "Dana Whitfield, Account Manager") catch {};

    const json_out = try chilkat.JsonObject.init();
    defer json_out.deinit();
    success = if (pdf.textReplace("apply", json, json_out)) true else |_| false;
    if (!success) {
        std.debug.print("Not applied: {s}\n", .{try json_out.emit(alloc)});
        if (!success) {
            std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
            return;
        }
    }

    var i: i32 = 0;
    const count_i = json_out.sizeOfArray("replacements");
    while (i < count_i) {
        json_out.setI(i);
        std.debug.print("[[{s}]] replaced\n", .{try json_out.stringOf(alloc, "replacements[i].field")});
        i = i + 1;
    }

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

    // Sample output:

    // [[date]] replaced
    // [[name]] replaced
    // [[expiry]] replaced
    // [[amount]] replaced
    // [[agent]] replaced
}