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

Get the Text Block Below a PDF Caption

See more PDF Text Extraction Examples

Demonstrates the "label" option with labelValue "below", which captures the block of text directly under a caption such as "Ship To", ending at the next gap or heading. Combined with layout "lines", the lines of an address block are kept separate.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    String sTemp1

    Move False To iSuccess

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  The packing slip used in this example is available at packing_slip.pdf

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/packing_slip.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 packing slip has the captions "Bill To" and "Ship To" side by side,
    //  each with an address block below it.
    Get ComUpdateString Of hoJson "label" "Ship To" To iSuccess

    //  "below" means the value is the block of text directly under the label.
    Get ComUpdateString Of hoJson "labelValue" "below" To iSuccess

    //  With the default "flow" layout the lines of the block would be joined into one paragraph.
    //  "lines" keeps each line of the address on its own line.
    Get ComUpdateString Of hoJson "layout" "lines" 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

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

    Get ComUpdateString Of hoJson "label" "Bill To" 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

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

    //  A caption over a short paragraph works the same way.  Here the flow layout is better,
    //  because the two lines are one piece of text.
    Get ComUpdateString Of hoJson "label" "Special Instructions" To iSuccess
    Get ComUpdateString Of hoJson "layout" "flow" 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

    Showln "Special Instructions:"
    Get ComStringOf Of hoJsonOut "text" To sTemp1
    Showln sTemp1

    //  Sample output:

    //  Ship To:
    //  Frank Delgado
    //  c/o Harbor Pet Boarding
    //  9 Ferry Lane
    //  Portside, WA 98001
    //  
    //  Bill To:
    //  Frank Delgado
    //  Apartment 3B
    //  41 Mill Street
    //  Portside, WA 98000
    //  
    //  Special Instructions:
    //  Leave the carton with the boarding office if no one answers. Do not leave it outside; the cat tree is not packed for rain.


End_Procedure