Sample code for 30+ languages & platforms
Unicode C 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 Unicode C Downloads

Unicode C
#include <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPdfW pdf;
    HCkJsonObjectW json;
    HCkJsonObjectW jsonOut;
    int i;
    int count_i;

    success = FALSE;

    //  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

    pdf = CkPdfW_Create();
    success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/search_context.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        return;
    }

    json = CkJsonObjectW_Create();
    jsonOut = CkJsonObjectW_Create();

    //  The string to find.
    CkJsonObjectW_UpdateString(json,L"query",L"chilkat");

    //  The number of characters returned before and after each match.  The default is 40.
    CkJsonObjectW_UpdateInt(json,L"context",30);

    success = CkPdfW_ExtractText(pdf,L"search",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        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.
    wprintf(L"Matches: %d\n",CkJsonObjectW_IntOf(jsonOut,L"count"));

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jsonOut,L"matches");
    while (i < count_i) {
        CkJsonObjectW_putI(jsonOut,i);
        wprintf(L"page %d: ...%s[%s]%s...\n",CkJsonObjectW_IntOf(jsonOut,L"matches[i].page"),CkJsonObjectW_stringOf(jsonOut,L"matches[i].before"),CkJsonObjectW_stringOf(jsonOut,L"matches[i].text")
            ,CkJsonObjectW_stringOf(jsonOut,L"matches[i].after"));
        i = i + 1;
    }

    //  The search ignores case by default.  Set caseSensitive to find only the exact spelling.
    CkJsonObjectW_UpdateBool(json,L"caseSensitive",TRUE);
    CkJsonObjectW_UpdateString(json,L"query",L"Chilkat");
    success = CkPdfW_ExtractText(pdf,L"search",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
        CkPdfW_Dispose(pdf);
        CkJsonObjectW_Dispose(json);
        CkJsonObjectW_Dispose(jsonOut);
        return;
    }

    wprintf(L"Case-sensitive matches for Chilkat: %d\n",CkJsonObjectW_IntOf(jsonOut,L"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


    CkPdfW_Dispose(pdf);
    CkJsonObjectW_Dispose(json);
    CkJsonObjectW_Dispose(jsonOut);

    }