Zig Requires Chilkat v11.7.0+
Zig
Assess PDF Text Quality Before Extracting
See more PDF Text Extraction Examples
Demonstrates the "assess" operation, which returns only the quality report: whether the text can be trusted, which pages are scanned images that need OCR, and how each page's text was found. Use it to triage documents before extracting. (Chilkat does not perform OCR.)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 (page 2 is a scanned image with no text) is available at mixed_text_image.pdf
const pdf = try chilkat.Pdf.init();
defer pdf.deinit();
pdf.loadFile("qa_data/pdf/mixed_text_image.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();
// "assess" computes the quality report without producing any text.
// It is fast, and it is always allowed, even for an encrypted PDF that forbids copying.
pdf.extractText("assess", json, json_out) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
// The overall rating is the worst rating among the pages that have text:
// "good", "fair", "poor", or "none" when there is no text at all.
std.debug.print("Rating: {s}\n", .{try json_out.stringOf(alloc, "quality.rating")});
// Is the PDF tagged (does it have a structure tree)?
std.debug.print("Tagged: {}\n", .{json_out.boolOf("quality.tagged")});
// The pages that are images only and would need OCR, such as "2" or "4,9".
std.debug.print("Pages needing OCR: {s}\n", .{try json_out.stringOf(alloc, "quality.pagesNeedingOcr")});
// The per-page report.
var j: i32 = 0;
var count_j: i32 = 0;
var i: i32 = 0;
const count_i = json_out.sizeOfArray("quality.pages");
while (i < count_i) {
json_out.setI(i);
// textSource is "text" (real text), "ocrLayer" (invisible text over an image),
// "imageOnly" (needs OCR), "mixed", or "empty".
std.debug.print("Page {d}: rating {s}, textSource {s}\n", .{ json_out.intOf("quality.pages[i].page"), try json_out.stringOf(alloc, "quality.pages[i].rating"), try json_out.stringOf(alloc, "quality.pages[i].textSource") });
// Each issue has a machine-readable code and a sentence.
j = 0;
count_j = json_out.sizeOfArray("quality.pages[i].issues");
while (j < count_j) {
json_out.setJ(j);
std.debug.print(" {s}: {s}\n", .{ try json_out.stringOf(alloc, "quality.pages[i].issues[j].code"), try json_out.stringOf(alloc, "quality.pages[i].issues[j].message") });
j = j + 1;
}
i = i + 1;
}
// Decide what to do with the document:
if (std.mem.eql(u8, try json_out.stringOf(alloc, "quality.rating"), "none")) {
std.debug.print("No extractable text; the whole document needs OCR.\n", .{});
} else {
if (!std.mem.eql(u8, try json_out.stringOf(alloc, "quality.pagesNeedingOcr"), "")) {
std.debug.print("Some pages need OCR; the remaining pages can be extracted with the text or markdown operation.\n", .{});
} else {
std.debug.print("All pages can be extracted.\n", .{});
}
}
// Sample output:
// Rating: good
// Tagged: false
// Pages needing OCR: 2
// Page 1: rating good, textSource text
// Page 2: rating none, textSource imageOnly
// imageOnly: No text; page is a scanned image
// Page 3: rating good, textSource text
// Some pages need OCR; the remaining pages can be extracted with the text or markdown operation.
}