Zig Requires Chilkat v11.7.0+
Zig
Get a PDF Field Value by its Label (Value to the Right)
See more PDF Text Extraction Examples
Demonstrates the "label" option, which captures the text that belongs to a caption such as "Invoice Number:" without needing coordinates. With labelValue "right", the value is the rest of the label's line. Label matching ignores case, extra spaces and a trailing colon.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 invoice used in this example is available at invoice_labels.pdf
const pdf = try chilkat.Pdf.init();
defer pdf.deinit();
pdf.loadFile("qa_data/pdf/invoice_labels.pdf") catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
const json = try chilkat.JsonObject.init();
defer json.deinit();
const json_out = try chilkat.JsonObject.init();
defer json_out.deinit();
// The invoice has lines such as "Invoice Number: INV-2026-0412".
// The label option restricts the "text" operation to the text belonging to the label.
json.updateString("label", "Invoice Number") catch {};
// "right" means the value is the rest of the label's line.
// (The default, "auto", uses the rest of the line if there is text there, otherwise the block below.)
json.updateString("labelValue", "right") catch {};
pdf.extractText("text", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
// The value is returned as the text.
std.debug.print("Invoice Number: {s}\n", .{try json_out.stringOf(alloc, "text")});
// The labelMatches array tells where the label was found and how it appeared in the PDF.
std.debug.print("Found on page {d} as \"{s}\"\n", .{ json_out.intOf("labelMatches[0].page"), try json_out.stringOf(alloc, "labelMatches[0].label") });
// Matching ignores case and the trailing colon, so these all work:
json.updateString("label", "CUSTOMER:") catch {};
pdf.extractText("text", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("Customer: {s}\n", .{try json_out.stringOf(alloc, "text")});
json.updateString("label", "due date") catch {};
pdf.extractText("text", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("Due Date: {s}\n", .{try json_out.stringOf(alloc, "text")});
// The totals at the foot of the invoice are right-aligned labels with the amount beside them.
json.updateString("label", "Total Due") catch {};
pdf.extractText("text", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("Total Due: {s}\n", .{try json_out.stringOf(alloc, "text")});
// A label that is not found is not an error. The text is empty, labelMatches is empty,
// and the quality report has an issue with the code "labelNotFound".
json.updateString("label", "Tracking Number") catch {};
pdf.extractText("text", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
if (json_out.sizeOfArray("labelMatches") == 0) {
std.debug.print("Tracking Number: (not found) {s}\n", .{try json_out.stringOf(alloc, "quality.pages[0].issues[0].code")});
}
// Sample output:
// Invoice Number: INV-2026-0412
// Found on page 1 as "Invoice Number:"
// Customer: Northwind Traders
// Due Date: April 13, 2026
// Total Due: $1,234.53
// Tracking Number: (not found) labelNotFound
}