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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Integer i
    Integer iCount_i
    String sTemp1
    String sTemp2
    String sTemp3
    Integer iTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/search_context.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End

    //  The string to find.
    Get ComUpdateString Of hoJson "query" "chilkat" To iSuccess

    //  The number of characters returned before and after each match.  The default is 40.
    Get ComUpdateInt Of hoJson "context" 30 To iSuccess

    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComExtractText Of hoPdf "search" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  "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.
    Get ComIntOf Of hoJsonOut "count" To iTemp1
    Showln "Matches: " iTemp1

    Move 0 To i
    Get ComSizeOfArray Of hoJsonOut "matches" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonOut To i
        Get ComIntOf Of hoJsonOut "matches[i].page" To iTemp1
        Get ComStringOf Of hoJsonOut "matches[i].before" To sTemp1
        Get ComStringOf Of hoJsonOut "matches[i].text" To sTemp2
        Get ComStringOf Of hoJsonOut "matches[i].after" To sTemp3
        Showln "page " iTemp1 ": ..." sTemp1 "[" sTemp2 "]" sTemp3 "..."
        Move (i + 1) To i
    Loop

    //  The search ignores case by default.  Set caseSensitive to find only the exact spelling.
    Get ComUpdateBool Of hoJson "caseSensitive" True To iSuccess
    Get ComUpdateString Of hoJson "query" "Chilkat" To iSuccess
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComExtractText Of hoPdf "search" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComIntOf Of hoJsonOut "count" To iTemp1
    Showln "Case-sensitive matches for Chilkat: " iTemp1

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


End_Procedure