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

Extract Text from Specific PDF Pages

See more PDF Text Extraction Examples

Demonstrates the "pages" option, which selects the pages to extract (a list of pages and ranges, or "first", "last", "odd", "even"), the "pageSeparator" option, and how to get the text of each page separately.

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

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/four_pages.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

    //  Pages are numbered from 1.  The "pages" option can be "all" (the default), "first", "last",
    //  "odd", "even", or a list such as "1,3,5-9,12-".  Here we extract pages 2 and 3.
    Get ComUpdateString Of hoJson "pages" "2-3" To iSuccess

    //  By default the pages in the joined text are separated by a form feed character.
    //  Use any string instead:
    Get ComUpdateString Of hoJson "pageSeparator" (character(10)) + "----- next page -----" + (character(10)) To iSuccess

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

    Get ComStringOf Of hoJsonOut "text" To sTemp1
    Showln sTemp1
    Showln ""

    //  The "pages" array has the text of each extracted page with its page number.
    //  This is the way to keep the text of each page separate.
    Get ComUpdateString Of hoJson "pages" "1,4" To iSuccess
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComExtractText Of hoPdf "text" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

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

    //  Sample output:

    //  Chapter Two: The Crossing
    //  
    //  This is page two of four. The crossing takes forty minutes in calm weather. Today the sea was flat and the gulls followed the boat the whole way.
    //  
    //  ----- next page -----
    //  Chapter Three: Arrival
    //  
    //  This is page three of four. The island harbor was busy with the morning catch being unloaded. The potatoes went straight up the hill to the store on a hand cart.
    //  
    //  
    //  Page 1: Chapter One: Departure
    //  
    //  This is page one of four. The ferry left the north pier at seven in the morning with eleven passengers, two dogs and a crate of seed potatoes for the island store.
    //  
    //  Page 4: Chapter Four: Return
    //  
    //  This is page four of four. The ferry turned round within the hour and sailed back with the mail, three passengers and an empty crate.


End_Procedure