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

Extract Text from a Two-Column PDF

See more PDF Text Extraction Examples

Demonstrates how multi-column pages are read. By default the columns are detected and each column is read top to bottom before the next ("columns": "auto"); "columns": "none" reads the full page width line by line, which interleaves the columns. The quality report tells how many columns were found.

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

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

    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/two_column_article.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

    ;  With the default options the columns are detected, and the text of the left column
    ;  comes before the text of the right column.
    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 "Columns detected: " + Str(CkJsonObject::ckIntOf(jsonOut,"quality.pages[0].columns"))
    Debug "Reading order: " + CkJsonObject::ckStringOf(jsonOut,"quality.pages[0].readingOrderSource")
    Debug CkJsonObject::ckStringOf(jsonOut,"text")

    ;  "none" switches column detection off: the page is read across its full width, line by line,
    ;  so each output line holds a line of the left column followed by a line of the right column.
    ;  This is rarely wanted, but it shows what the detection does.
    CkJsonObject::ckUpdateString(json,"columns","none")
    CkJsonObject::ckUpdateString(json,"layout","lines")
    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 "---- columns: none ----"
    Debug CkJsonObject::ckStringOf(jsonOut,"text")

    ;  Sample output (abbreviated):

    ;  Columns detected: 2
    ;  Reading order: visual
    ;  The Harbor Lighthouse, Season Two
    ;  
    ;  The harbor lighthouse was built in 1874 to mark the entrance to the inner channel. It operated continuously until 1968, ...
    ;  
    ;  The restoration society took ownership in 2021 with three goals: ...
    ;  ...
    ;  The lantern room frame was stripped, primed and repainted in the original dark green. ...
    ;  ...
    ;  ---- columns: none ----
    ;  The Harbor Lighthouse, Season Two
    ;  The harbor lighthouse was built in 1874 to The lantern room frame was stripped, primed
    ;  mark the entrance to the inner channel. It and repainted in the original dark green. New
    ;  ...


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


    ProcedureReturn
EndProcedure