Zig Requires Chilkat v11.7.0+
Zig
What PDF Text Replacement Does Not Change: Metadata, Links, Form Fields
See more PDF Text Replacement Examples
TextReplace changes the page content only. A placeholder that also appears in the document's metadata (the Title, Author, Subject, Keywords, Creator or Producer), in a link's target URL, or in a form field's value is left as it is and reported in "notReplaced" with "where" saying which. The invoice template's title is "Invoice for {{customer}}" and its "View this invoice online" link points at a URL that holds {{customer}}.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;
};
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("placeholders.customer", "Acme Corp") catch {};
// (The other placeholders are left as they are for this example.)
json.updateString("unusedPlaceholders", "keep") catch {};
const json_out = try chilkat.JsonObject.init();
defer json_out.deinit();
pdf.textReplace("apply", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("In the page content, customer was replaced {d} times.\n", .{json_out.intOf("replacements[0].count")});
// Where else the placeholder occurs, untouched:
var i: i32 = 0;
const count_i = json_out.sizeOfArray("notReplaced");
while (i < count_i) {
json_out.setI(i);
std.debug.print("not replaced: {{{{{s}}}}} in {s}\n", .{ try json_out.stringOf(alloc, "notReplaced[i].field"), try json_out.stringOf(alloc, "notReplaced[i].where") });
i = i + 1;
}
pdf.writeFile("qa_output/invoice_customer.pdf") catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
// Sample output:
// In the page content, customer was replaced 2 times.
// not replaced: {{customer}} in metadata
// not replaced: {{customer}} in linkTarget
}