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

Search for Text in a PDF

See more PDF Text Extraction Examples

Demonstrates the "search" operation, which finds every occurrence of a string in a PDF and returns each match with its page number and the text around it. Matching ignores case unless caseSensitive is true.

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 three-page PDF used in this example is available at search_context.pdf

    const pdf = try chilkat.Pdf.init();
    defer pdf.deinit();
    pdf.loadFile("qa_data/pdf/search_context.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 string to find.
    json.updateString("query", "chilkat") catch {};

    // The number of characters returned before and after each match.  The default is 40.
    json.updateInt("context", 30) catch {};

    pdf.extractText("search", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    // "count" is the number of matches.  Each match has the page, the matched text as it appears
    // in the PDF, and the text before and after it.
    std.debug.print("Matches: {d}\n", .{json_out.intOf("count")});

    var i: i32 = 0;
    const count_i = json_out.sizeOfArray("matches");
    while (i < count_i) {
        json_out.setI(i);
        std.debug.print("page {d}: ...{s}[{s}]{s}...\n", .{ json_out.intOf("matches[i].page"), try json_out.stringOf(alloc, "matches[i].before"), try json_out.stringOf(alloc, "matches[i].text"), try json_out.stringOf(alloc, "matches[i].after") });
        i = i + 1;
    }

    // The search ignores case by default.  Set caseSensitive to find only the exact spelling.
    json.updateBool("caseSensitive", true) catch {};
    json.updateString("query", "Chilkat") catch {};
    pdf.extractText("search", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Case-sensitive matches for Chilkat: {d}\n", .{json_out.intOf("count")});

    // Sample output:

    // Matches: 5
    // page 1: ...[Chilkat] River Field Notes The Chilkat...
    // page 1: ...Chilkat River Field Notes The [Chilkat] River runs for about fifty mi...
    // page 2: ...DAY ONE: UPPER [CHILKAT] FLATS Clear and cold, minus e...
    // page 2: ...he river is sometimes spelled [chilkat] in older records, and the val...
    // page 3: ...y because the fish run on the [Chilkat] was thinning. Two tagged bird...
    // Case-sensitive matches for Chilkat: 3
}