DataFlex Requires Chilkat v11.7.0+
DataFlex
Extract the OCR Text Layer of a Scanned PDF
See more PDF Text Extraction Examples
Demonstrates a scanned page that carries an invisible OCR text layer over the page image. The quality report identifies the page's textSource as "ocrLayer". The "invisibleText" filter is "auto" by default, which uses the invisible text only on pages that have no visible text; "exclude" ignores it.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPdf
Variant vJson
Handle hoJson
Variant vJsonOut
Handle hoJsonOut
String sTemp1
String sTemp2
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 (a page image with an invisible text layer, as an OCR program produces) is available at scanned_with_ocr.pdf
Get Create (RefClass(cComChilkatPdf)) To hoPdf
If (Not(IsComObjectCreated(hoPdf))) Begin
Send CreateComObject of hoPdf
End
Get ComLoadFile Of hoPdf "qa_data/pdf/scanned_with_ocr.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 is a picture of a handwritten log with the OCR'd words drawn invisibly over it.
// "auto" (the default) includes invisible text exactly in this case: on a page with no visible text.
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
Get ComStringOf Of hoJsonOut "quality.pages[0].textSource" To sTemp1
Showln "textSource: " sTemp1
Get ComStringOf Of hoJsonOut "quality.pages[0].rating" To sTemp1
Showln "rating: " sTemp1
Get ComStringOf Of hoJsonOut "text" To sTemp1
Showln sTemp1
// The quality report flags the page, because OCR text is only as good as the OCR was.
Get ComStringOf Of hoJsonOut "quality.pages[0].issues[0].code" To sTemp1
Get ComStringOf Of hoJsonOut "quality.pages[0].issues[0].message" To sTemp2
Showln "issue: " sTemp1 " - " sTemp2
// "exclude" ignores invisible text altogether: the page then has no text at all.
Get ComUpdateString Of hoJson "invisibleText" "exclude" 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
Get ComStringOf Of hoJsonOut "quality.pages[0].textSource" To sTemp1
Get ComStringOf Of hoJsonOut "text" To sTemp2
Showln "With invisibleText excluded, textSource: " sTemp1 ", text: [" sTemp2 "]"
// Sample output:
// textSource: ocrLayer
// rating: good
// Keeper's Log, 12 October 1902
//
// Wind north-west, fresh. Lamp lit at 5.40 p.m. and burning well all night.
//
// Schooner Mary Ann passed inbound at 9 p.m. showing proper lights.
//
// Oil on hand: forty-one gallons. Cleaned the lens at dawn.
// issue: ocrLayer - The text is an invisible OCR layer over an image
// With invisibleText excluded, textSource: ocrLayer, text: []
End_Procedure