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

Search a PDF with a Regular Expression

See more PDF Text Extraction Examples

Demonstrates searching a PDF with a regular expression (regex: true), limiting the number of results (maxResults), and matching whole words only (wholeWord).

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkPdfW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  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 order_records.pdf

    CkPdfW pdf;
    success = pdf.LoadFile(L"qa_data/pdf/order_records.pdf");
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    CkJsonObjectW json;
    CkJsonObjectW jsonOut;

    //  Find every order number.  With regex set to true, the query is a regular expression.
    json.UpdateString(L"query",L"ORD-[0-9]{4}-[0-9]{4}");
    json.UpdateBool(L"regex",true);
    //  We only want the matched text, so no context is needed.
    json.UpdateInt(L"context",0);

    success = pdf.ExtractText(L"search",json,jsonOut);
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    wprintf(L"Order numbers found: %d\n",jsonOut.IntOf(L"count"));
    int i = 0;
    int count_i = jsonOut.SizeOfArray(L"matches");
    while (i < count_i) {
        jsonOut.put_I(i);
        wprintf(L"%s\n",jsonOut.stringOf(L"matches[i].text"));
        i = i + 1;
    }

    //  Find the e-mail addresses, but stop after the first three.
    json.UpdateString(L"query",L"[a-z0-9._]+@[a-z]+[.][a-z]+");
    json.UpdateInt(L"maxResults",3);
    success = pdf.ExtractText(L"search",json,jsonOut);
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    wprintf(L"First three e-mail addresses:\n");
    i = 0;
    count_i = jsonOut.SizeOfArray(L"matches");
    while (i < count_i) {
        jsonOut.put_I(i);
        wprintf(L"%s\n",jsonOut.stringOf(L"matches[i].text"));
        i = i + 1;
    }

    //  wholeWord: true matches only whole words.  The word "cat" appears once in the PDF,
    //  but it is also inside "catalog" and "category".
    json.Delete(L"maxResults");
    json.UpdateBool(L"regex",false);
    json.UpdateString(L"query",L"cat");
    success = pdf.ExtractText(L"search",json,jsonOut);
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    wprintf(L"Occurrences of cat (any): %d\n",jsonOut.IntOf(L"count"));

    json.UpdateBool(L"wholeWord",true);
    success = pdf.ExtractText(L"search",json,jsonOut);
    if (success == false) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    wprintf(L"Occurrences of cat (whole word): %d\n",jsonOut.IntOf(L"count"));

    //  Sample output:

    //  Order numbers found: 9
    //  ORD-2026-0301
    //  ORD-2026-0302
    //  ORD-2026-0303
    //  ORD-2026-0304
    //  ORD-2026-0305
    //  ORD-2026-0306
    //  ORD-2026-0307
    //  ORD-2026-0308
    //  ORD-2026-0307
    //  First three e-mail addresses:
    //  alice.moore@example.com
    //  bkumar@example.org
    //  carla@example.net
    //  Occurrences of cat (any): 3
    //  Occurrences of cat (whole word): 1
    }