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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Integer i
    Integer iCount_i
    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 hidden_text.pdf

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/hidden_text.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 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.
    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 "---- default ----"
    Get ComStringOf Of hoJsonOut "text" To sTemp1
    Showln sTemp1

    //  The quality report says what was left out.
    Move 0 To i
    Get ComSizeOfArray Of hoJsonOut "quality.pages[0].issues" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonOut To i
        Get ComStringOf Of hoJsonOut "quality.pages[0].issues[i].code" To sTemp1
        Showln "issue: " sTemp1
        Move (i + 1) To i
    Loop

    //  Include everything.
    Get ComUpdateString Of hoJson "clippedText" "include" To iSuccess
    Get ComUpdateString Of hoJson "offPageText" "include" To iSuccess
    Get ComUpdateString Of hoJson "obscuredText" "include" 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 "---- clippedText, offPageText, obscuredText: include ----"
    Get ComStringOf Of hoJsonOut "text" To sTemp1
    Showln sTemp1

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


End_Procedure