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

Clipped, Off-Page and White-on-White Text in PDF Extraction

See more PDF Text Extraction Examples

Demonstrates the "clippedText", "offPageText" and "obscuredText" filters. Text that a reader never sees - hidden by a clipping path, outside the crop box, or drawn in the same color as the box beneath it - is excluded by default, and the quality report notes what was skipped. Each kind can be included.

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 is available at hidden_text.pdf

    const pdf = try chilkat.Pdf.init();
    defer pdf.deinit();
    pdf.loadFile("qa_data/pdf/hidden_text.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 has a visible paragraph plus three hidden lines: one drawn outside its clipping
    // rectangle, one below the crop box, and one in white on a white box.
    pdf.extractText("text", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("---- default ----\n", .{});
    std.debug.print("{s}\n", .{try json_out.stringOf(alloc, "text")});

    // The quality report says what was left out.
    var i: i32 = 0;
    const count_i = json_out.sizeOfArray("quality.pages[0].issues");
    while (i < count_i) {
        json_out.setI(i);
        std.debug.print("issue: {s}\n", .{try json_out.stringOf(alloc, "quality.pages[0].issues[i].code")});
        i = i + 1;
    }

    // Include everything.
    json.updateString("clippedText", "include") catch {};
    json.updateString("offPageText", "include") catch {};
    json.updateString("obscuredText", "include") catch {};
    pdf.extractText("text", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("---- clippedText, offPageText, obscuredText: include ----\n", .{});
    std.debug.print("{s}\n", .{try json_out.stringOf(alloc, "text")});

    // Sample output:

    // ---- default ----
    // Visible Text
    //
    // This paragraph is visible. The page also carries three kinds of text a reader never sees.
    //
    // End of the visible text.
    // issue: obscuredTextSkipped
    // issue: clippedTextSkipped
    // ---- clippedText, offPageText, obscuredText: include ----
    // Visible Text
    //
    // This paragraph is visible. The page also carries three kinds of text a reader never sees.
    //
    // CLIPPED: this line is drawn outside its clipping rectangle.
    //
    // OBSCURED: this line is white on a white box.
    //
    // End of the visible text.
    //
    // OFFPAGE: this line sits below the crop box.
}