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

Assess PDF Text Quality Before Extracting

See more PDF Text Extraction Examples

Demonstrates the "assess" operation, which returns only the quality report: whether the text can be trusted, which pages are scanned images that need OCR, and how each page's text was found. Use it to triage documents before extracting. (Chilkat does not perform OCR.)

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Integer j
    Integer iCount_j
    Integer i
    Integer iCount_i
    String sTemp1
    String sTemp2
    Integer iTemp1
    Boolean bTemp1

    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 (page 2 is a scanned image with no text) is available at mixed_text_image.pdf

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/mixed_text_image.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

    //  "assess" computes the quality report without producing any text.
    //  It is fast, and it is always allowed, even for an encrypted PDF that forbids copying.
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComExtractText Of hoPdf "assess" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  The overall rating is the worst rating among the pages that have text:
    //  "good", "fair", "poor", or "none" when there is no text at all.
    Get ComStringOf Of hoJsonOut "quality.rating" To sTemp1
    Showln "Rating: " sTemp1

    //  Is the PDF tagged (does it have a structure tree)?
    Get ComBoolOf Of hoJsonOut "quality.tagged" To bTemp1
    Showln "Tagged: " bTemp1

    //  The pages that are images only and would need OCR, such as "2" or "4,9".
    Get ComStringOf Of hoJsonOut "quality.pagesNeedingOcr" To sTemp1
    Showln "Pages needing OCR: " sTemp1

    //  The per-page report.

    Move 0 To i
    Get ComSizeOfArray Of hoJsonOut "quality.pages" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonOut To i
        //  textSource is "text" (real text), "ocrLayer" (invisible text over an image),
        //  "imageOnly" (needs OCR), "mixed", or "empty".
        Get ComIntOf Of hoJsonOut "quality.pages[i].page" To iTemp1
        Get ComStringOf Of hoJsonOut "quality.pages[i].rating" To sTemp1
        Get ComStringOf Of hoJsonOut "quality.pages[i].textSource" To sTemp2
        Showln "Page " iTemp1 ": rating " sTemp1 ", textSource " sTemp2
        //  Each issue has a machine-readable code and a sentence.
        Move 0 To j
        Get ComSizeOfArray Of hoJsonOut "quality.pages[i].issues" To iCount_j
        While (j < iCount_j)
            Set ComJ Of hoJsonOut To j
            Get ComStringOf Of hoJsonOut "quality.pages[i].issues[j].code" To sTemp1
            Get ComStringOf Of hoJsonOut "quality.pages[i].issues[j].message" To sTemp2
            Showln "    " sTemp1 ": " sTemp2
            Move (j + 1) To j
        Loop

        Move (i + 1) To i
    Loop

    //  Decide what to do with the document:
    Get ComStringOf Of hoJsonOut "quality.rating" To sTemp1
    If (sTemp1 = "none") Begin
        Showln "No extractable text; the whole document needs OCR."
    End
    Else Begin
        Get ComStringOf Of hoJsonOut "quality.pagesNeedingOcr" To sTemp1
        If (sTemp1 <> "") Begin
            Showln "Some pages need OCR; the remaining pages can be extracted with the text or markdown operation."
        End
        Else Begin
            Showln "All pages can be extracted."
        End

    End

    //  Sample output:

    //  Rating: good
    //  Tagged: false
    //  Pages needing OCR: 2
    //  Page 1: rating good, textSource text
    //  Page 2: rating none, textSource imageOnly
    //      imageOnly: No text; page is a scanned image
    //  Page 3: rating good, textSource text
    //  Some pages need OCR; the remaining pages can be extracted with the text or markdown operation.


End_Procedure