Zig Requires Chilkat v11.7.0+
Zig
Check a PDF Text Replacement Before Applying It
See more PDF Text Replacement Examples
Demonstrates the "check" operation, which finds every placeholder and label and applies the font rule but changes nothing, and how to read its report: "ok" says whether "apply" would succeed, "replacements" lists what would be replaced, and "problems" lists what prevents it, each with a reason. Here the first check fails because a placeholder on the page was given no value; the value is added and the check passes, and only then is "apply" called.Chilkat Zig Downloads
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 most of the template's placeholders -- but not {{po}}, {{qty2}}, {{desc2}}, {{price2}} or {{amount2}}.
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.subtotal", "$920.00") catch {};
json.updateString("placeholders.taxRate", "7.35%") catch {};
json.updateString("placeholders.tax", "$67.62") catch {};
json.updateString("placeholders.total", "$987.62") catch {};
// "check" reports what "apply" would do, without changing the 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("ok: {}\n", .{json_out.boolOf("ok")});
std.debug.print("would replace: {d} placeholders\n", .{json_out.sizeOfArray("replacements")});
// Each problem names the placeholder (field) or label, and a reason:
// notFound - the placeholder or label is not on the selected pages
// unusedPlaceholder - a placeholder on the page was given no value
// missingGlyph, notInEncoding, nonStandardUnembeddedFont, unsupportedFont - the font rule
// placeholderSpansFonts - the text is drawn with more than one font or size
var i: i32 = 0;
const count_i = json_out.sizeOfArray("problems");
while (i < count_i) {
json_out.setI(i);
std.debug.print("problem: {s}{s} - {s} (page {d})\n", .{ try json_out.stringOf(alloc, "problems[i].field"), try json_out.stringOf(alloc, "problems[i].label"), try json_out.stringOf(alloc, "problems[i].reason"), json_out.intOf("problems[i].page") });
i = i + 1;
}
// Give the missing placeholders their values and check again.
json.updateString("placeholders.po", "PO-88213") catch {};
json.updateString("placeholders.qty2", "0") catch {};
json.updateString("placeholders.desc2", "(no second item)") catch {};
json.updateString("placeholders.price2", "") catch {};
json.updateString("placeholders.amount2", "") catch {};
pdf.textReplace("check", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("ok after adding the values: {}\n", .{json_out.boolOf("ok")});
// Now apply.
if (json_out.boolOf("ok")) {
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")});
pdf.writeFile("qa_output/invoice_checked.pdf") catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
}
// Sample output:
// ok: false
// would replace: 10 placeholders
// problem: po - unusedPlaceholder (page 1)
// problem: qty2 - unusedPlaceholder (page 1)
// problem: desc2 - unusedPlaceholder (page 1)
// problem: price2 - unusedPlaceholder (page 1)
// problem: amount2 - unusedPlaceholder (page 1)
// ok after adding the values: true
// applied: true
}