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

PureBasic
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  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

    pdf.i = CkPdf::ckCreate()
    If pdf.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/sectioned_doc.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        ProcedureReturn
    EndIf

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    jsonOut.i = CkJsonObject::ckCreate()
    If jsonOut.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  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.
    CkJsonObject::ckUpdateString(json,"label","Terms and Conditions")
    CkJsonObject::ckUpdateString(json,"labelValue","section")

    success = CkPdf::ckExtractText(pdf,"markdown",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug CkJsonObject::ckStringOf(jsonOut,"markdown")

    ;  The same with the "text" operation gives the section as plain text.
    success = CkPdf::ckExtractText(pdf,"text",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "---- as text ----"
    Debug CkJsonObject::ckStringOf(jsonOut,"text")

    ;  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.


    CkPdf::ckDispose(pdf)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jsonOut)


    ProcedureReturn
EndProcedure