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

Tcl

load ./chilkat.dll

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

set pdf [new_CkPdf]

set success [CkPdf_LoadFile $pdf "qa_data/pdf/mixed_text_image.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    exit
}

set json [new_CkJsonObject]

set jsonOut [new_CkJsonObject]

#  "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.
set success [CkPdf_ExtractText $pdf "assess" $json $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    exit
}

#  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.
puts "Rating: [CkJsonObject_stringOf $jsonOut quality.rating]"

#  Is the PDF tagged (does it have a structure tree)?
puts "Tagged: [CkJsonObject_BoolOf $jsonOut quality.tagged]"

#  The pages that are images only and would need OCR, such as "2" or "4,9".
puts "Pages needing OCR: [CkJsonObject_stringOf $jsonOut quality.pagesNeedingOcr]"

#  The per-page report.

set i 0
set count_i [CkJsonObject_SizeOfArray $jsonOut "quality.pages"]
while {$i < $count_i} {
    CkJsonObject_put_I $jsonOut $i
    #  textSource is "text" (real text), "ocrLayer" (invisible text over an image),
    #  "imageOnly" (needs OCR), "mixed", or "empty".
    puts "Page [CkJsonObject_IntOf $jsonOut {quality.pages[i].page}]: rating [CkJsonObject_stringOf $jsonOut {quality.pages[i].rating}], textSource [CkJsonObject_stringOf $jsonOut {quality.pages[i].textSource}]"
    #  Each issue has a machine-readable code and a sentence.
    set j 0
    set count_j [CkJsonObject_SizeOfArray $jsonOut "quality.pages[i].issues"]
    while {$j < $count_j} {
        CkJsonObject_put_J $jsonOut $j
        puts "    [CkJsonObject_stringOf $jsonOut {quality.pages[i].issues[j].code}]: [CkJsonObject_stringOf $jsonOut {quality.pages[i].issues[j].message}]"
        set j [expr $j + 1]
    }
    set i [expr $i + 1]
}

#  Decide what to do with the document:
if {[CkJsonObject_stringOf $jsonOut "quality.rating"] == "none"} then {
    puts "No extractable text; the whole document needs OCR."
} else {
    if {[CkJsonObject_stringOf $jsonOut "quality.pagesNeedingOcr"] != ""} then {
        puts "Some pages need OCR; the remaining pages can be extracted with the text or markdown operation."
    }     else {
        puts "All pages can be extracted."
    }

}

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

delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut