Zig Requires Chilkat v11.7.0+
Zig
Get Every Occurrence of a Label in a PDF
See more PDF Text Extraction Examples
Demonstrates the "labelOccurrence" option. When the same label appears more than once (for example "Total:" on each of several receipts), "first" (the default) uses the first occurrence, "all" returns every occurrence with its page, and a number selects one occurrence.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 (three receipts, one per page) is available at three_receipts.pdf
const pdf = try chilkat.Pdf.init();
defer pdf.deinit();
pdf.loadFile("qa_data/pdf/three_receipts.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();
// Each of the three pages is a receipt with a "Total:" line.
json.updateString("label", "Total") catch {};
json.updateString("labelValue", "right") catch {};
// By default only the first occurrence (in reading order) is used.
pdf.extractText("text", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("First total: {s}\n", .{try json_out.stringOf(alloc, "text")});
// labelOccurrence "all" returns every occurrence. The "text" member holds the values joined
// by line breaks, and labelMatches has one entry per occurrence with its page.
json.updateString("labelOccurrence", "all") catch {};
pdf.extractText("text", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
var i: i32 = 0;
const count_i = json_out.sizeOfArray("labelMatches");
while (i < count_i) {
json_out.setI(i);
std.debug.print("Page {d} total: {s}\n", .{ json_out.intOf("labelMatches[i].page"), try json_out.stringOf(alloc, "labelMatches[i].text") });
i = i + 1;
}
// A number selects one occurrence: 2 is the second.
json.updateInt("labelOccurrence", 2) catch {};
pdf.extractText("text", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("Second total: {s}\n", .{try json_out.stringOf(alloc, "text")});
// The occurrences are counted over the selected pages, so combining "pages" with the label
// is another way to pick one receipt:
json.updateString("labelOccurrence", "first") catch {};
json.updateString("pages", "3") catch {};
json.updateString("label", "Receipt No.") catch {};
pdf.extractText("text", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("Receipt number on page 3: {s}\n", .{try json_out.stringOf(alloc, "text")});
// Sample output:
// First total: $19.86
// Page 1 total: $19.86
// Page 2 total: $17.34
// Page 3 total: $23.62
// Second total: $17.34
// Receipt number on page 3: 1003
}