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

Clipped, Off-Page and White-on-White Text in PDF Extraction

See more PDF Text Extraction Examples

Demonstrates the "clippedText", "offPageText" and "obscuredText" filters. Text that a reader never sees - hidden by a clipping path, outside the crop box, or drawn in the same color as the box beneath it - is excluded by default, and the quality report notes what was skipped. Each kind can be included.

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 hidden_text.pdf

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

    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/hidden_text.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 page has a visible paragraph plus three hidden lines: one drawn outside its clipping
    ;  rectangle, one below the crop box, and one in white on a white box.
    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 "---- default ----"
    Debug CkJsonObject::ckStringOf(jsonOut,"text")

    ;  The quality report says what was left out.
    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jsonOut,"quality.pages[0].issues")
    While i < count_i
        CkJsonObject::setCkI(jsonOut, i)
        Debug "issue: " + CkJsonObject::ckStringOf(jsonOut,"quality.pages[0].issues[i].code")
        i = i + 1
    Wend

    ;  Include everything.
    CkJsonObject::ckUpdateString(json,"clippedText","include")
    CkJsonObject::ckUpdateString(json,"offPageText","include")
    CkJsonObject::ckUpdateString(json,"obscuredText","include")
    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 "---- clippedText, offPageText, obscuredText: include ----"
    Debug CkJsonObject::ckStringOf(jsonOut,"text")

    ;  Sample output:

    ;  ---- default ----
    ;  Visible Text
    ;  
    ;  This paragraph is visible. The page also carries three kinds of text a reader never sees.
    ;  
    ;  End of the visible text.
    ;  issue: obscuredTextSkipped
    ;  issue: clippedTextSkipped
    ;  ---- clippedText, offPageText, obscuredText: include ----
    ;  Visible Text
    ;  
    ;  This paragraph is visible. The page also carries three kinds of text a reader never sees.
    ;  
    ;  CLIPPED: this line is drawn outside its clipping rectangle.
    ;  
    ;  OBSCURED: this line is white on a white box.
    ;  
    ;  End of the visible text.
    ;  
    ;  OFFPAGE: this line sits below the crop box.


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


    ProcedureReturn
EndProcedure