Zig Requires Chilkat v11.7.0+
Zig
Get the Text Block Below a PDF Caption
See more PDF Text Extraction Examples
Demonstrates the "label" option with labelValue "below", which captures the block of text directly under a caption such as "Ship To", ending at the next gap or heading. Combined with layout "lines", the lines of an address block are kept separate.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 packing slip used in this example is available at packing_slip.pdf
const pdf = try chilkat.Pdf.init();
defer pdf.deinit();
pdf.loadFile("qa_data/pdf/packing_slip.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 packing slip has the captions "Bill To" and "Ship To" side by side,
// each with an address block below it.
json.updateString("label", "Ship To") catch {};
// "below" means the value is the block of text directly under the label.
json.updateString("labelValue", "below") catch {};
// With the default "flow" layout the lines of the block would be joined into one paragraph.
// "lines" keeps each line of the address on its own line.
json.updateString("layout", "lines") catch {};
pdf.extractText("text", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("Ship To:\n", .{});
std.debug.print("{s}\n", .{try json_out.stringOf(alloc, "text")});
std.debug.print("\n", .{});
json.updateString("label", "Bill To") catch {};
pdf.extractText("text", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("Bill To:\n", .{});
std.debug.print("{s}\n", .{try json_out.stringOf(alloc, "text")});
std.debug.print("\n", .{});
// A caption over a short paragraph works the same way. Here the flow layout is better,
// because the two lines are one piece of text.
json.updateString("label", "Special Instructions") catch {};
json.updateString("layout", "flow") catch {};
pdf.extractText("text", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
std.debug.print("Special Instructions:\n", .{});
std.debug.print("{s}\n", .{try json_out.stringOf(alloc, "text")});
// Sample output:
// Ship To:
// Frank Delgado
// c/o Harbor Pet Boarding
// 9 Ferry Lane
// Portside, WA 98001
//
// Bill To:
// Frank Delgado
// Apartment 3B
// 41 Mill Street
// Portside, WA 98000
//
// Special Instructions:
// Leave the carton with the boarding office if no one answers. Do not leave it outside; the cat tree is not packed for rain.
}