Zig Requires Chilkat v11.7.0+
Zig
Replace the Value After a Label in a PDF (Re-issue an Invoice)
See more PDF Text Replacement Examples
Demonstrates replacing the values that follow labels such as "Invoice Number:" on a PDF that has no placeholders -- an ordinary invoice. Each entry of the "labels" array names a label as it appears on the page and the new value; the value replaces the rest of the label's line. Label matching ignores case, extra spaces and a trailing colon. The replaced text is read back with ExtractText to confirm it.Chilkat Zig Downloads
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 invoice_labels.pdf
const pdf = try chilkat.Pdf.init();
defer pdf.deinit();
success = if (pdf.loadFile("qa_data/pdf/invoice_labels.pdf")) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
}
// The invoice has lines such as "Invoice Number: INV-2026-0412" and "Total Due: $1,234.53".
// Each label's new value replaces whatever follows the label on its line.
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("labels[0].label", "Invoice Number:") catch {};
json.updateString("labels[0].value", "INV-2026-0500") catch {};
json.updateString("labels[1].label", "Invoice Date:") catch {};
json.updateString("labels[1].value", "April 2, 2026") catch {};
json.updateString("labels[2].label", "customer") catch {};
json.updateString("labels[2].value", "Contoso Ltd") catch {};
json.updateString("labels[3].label", "Total Due:") catch {};
json.updateString("labels[3].value", "$1,999.99") 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("Replaced the value of \"{s}\" on page {s}\n", .{ try json_out.stringOf(alloc, "replacements[i].label"), try json_out.stringOf(alloc, "replacements[i].pages") });
i = i + 1;
}
pdf.writeFile("qa_output/invoice_reissued.pdf") catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
// Read the new values back with ExtractText's label option.
const json_read = try chilkat.JsonObject.init();
defer json_read.deinit();
const json_text = try chilkat.JsonObject.init();
defer json_text.deinit();
json_read.updateString("label", "Total Due") catch {};
json_read.updateString("labelValue", "right") catch {};
pdf.extractText("text", json_read, json_text) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("Total Due is now: {s}\n", .{try json_text.stringOf(alloc, "text")});
// Sample output:
// Replaced the value of "Invoice Number:" on page 1
// Replaced the value of "Invoice Date:" on page 1
// Replaced the value of "customer" on page 1
// Replaced the value of "Total Due:" on page 1
// Total Due is now: $1,999.99
}