Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.7.0+

Extract the OCR Text Layer of a Scanned PDF

See more PDF Text Extraction Examples

Demonstrates a scanned page that carries an invisible OCR text layer over the page image. The quality report identifies the page's textSource as "ocrLayer". The "invisibleText" filter is "auto" by default, which uses the invisible text only on pages that have no visible text; "exclude" ignores it.

Chilkat Zig Downloads

Zig
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 (a page image with an invisible text layer, as an OCR program produces) is available at scanned_with_ocr.pdf

    const pdf = try chilkat.Pdf.init();
    defer pdf.deinit();
    pdf.loadFile("qa_data/pdf/scanned_with_ocr.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 page is a picture of a handwritten log with the OCR'd words drawn invisibly over it.
    // "auto" (the default) includes invisible text exactly in this case: on a page with no visible text.
    pdf.extractText("text", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("textSource: {s}\n", .{try json_out.stringOf(alloc, "quality.pages[0].textSource")});
    std.debug.print("rating: {s}\n", .{try json_out.stringOf(alloc, "quality.pages[0].rating")});
    std.debug.print("{s}\n", .{try json_out.stringOf(alloc, "text")});

    // The quality report flags the page, because OCR text is only as good as the OCR was.
    std.debug.print("issue: {s} - {s}\n", .{ try json_out.stringOf(alloc, "quality.pages[0].issues[0].code"), try json_out.stringOf(alloc, "quality.pages[0].issues[0].message") });

    // "exclude" ignores invisible text altogether: the page then has no text at all.
    json.updateString("invisibleText", "exclude") catch {};
    pdf.extractText("text", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("With invisibleText excluded, textSource: {s}, text: [{s}]\n", .{ try json_out.stringOf(alloc, "quality.pages[0].textSource"), try json_out.stringOf(alloc, "text") });

    // Sample output:

    // textSource: ocrLayer
    // rating: good
    // Keeper's Log, 12 October 1902
    //
    // Wind north-west, fresh. Lamp lit at 5.40 p.m. and burning well all night.
    //
    // Schooner Mary Ann passed inbound at 9 p.m. showing proper lights.
    //
    // Oil on hand: forty-one gallons. Cleaned the lens at dawn.
    // issue: ocrLayer - The text is an invisible OCR layer over an image
    // With invisibleText excluded, textSource: ocrLayer, text: []
}