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

Extract One Section of a PDF by its Heading

See more PDF Text Extraction Examples

Demonstrates "labelValue": "section", which returns everything from a heading up to the next heading of the same or higher level. With the "markdown" operation the section keeps its own sub-headings.

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

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/sectioned_doc.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 document has the sections Introduction, Opening Hours, Terms and Conditions (with the
    //  sub-sections Admission, Safety and Liability) and Contact.  Get the Terms and Conditions:
    //  everything after that heading up to the next heading of the same level (Contact).  The heading
    //  itself is not part of the value; labelMatches[0].label has it.
    Get ComUpdateString Of hoJson "label" "Terms and Conditions" To iSuccess
    Get ComUpdateString Of hoJson "labelValue" "section" To iSuccess

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

    Get ComStringOf Of hoJsonOut "markdown" To sTemp1
    Showln sTemp1

    //  The same with the "text" operation gives the section as plain text.
    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 "---- as text ----"
    Get ComStringOf Of hoJsonOut "text" To sTemp1
    Showln sTemp1

    //  Sample output:

    //  ### Admission
    //  
    //  Admission is free. Groups of more than ten should book in advance so that a volunteer can be assigned to them.
    //  
    //  ### Safety
    //  
    //  No more than eight people may be on the stair at one time. Children under twelve must be accompanied by an adult, and the lantern room is closed in high winds.
    //  
    //  ### Liability
    //  
    //  Visitors climb the tower at their own risk. The society carries public liability insurance but accepts no responsibility for personal property.
    //  ---- as text ----
    //  Admission
    //  
    //  Admission is free. Groups of more than ten should book in advance so that a volunteer can be assigned to them.
    //  
    //  Safety
    //  
    //  No more than eight people may be on the stair at one time. Children under twelve must be accompanied by an adult, and the lantern room is closed in high winds.
    //  
    //  Liability
    //  
    //  Visitors climb the tower at their own risk. The society carries public liability insurance but accepts no responsibility for personal property.


End_Procedure