Visual FoxPro Requires Chilkat v11.7.0+
Visual FoxPro
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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loPdf
LOCAL loJson
LOCAL loJsonOut
LOCAL j
LOCAL lnCount_j
LOCAL i
LOCAL lnCount_i
lnSuccess = 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
loPdf = CreateObject('Chilkat.Pdf')
lnSuccess = loPdf.LoadFile("qa_data/pdf/mixed_text_image.pdf")
IF (lnSuccess = 0) THEN
? loPdf.LastErrorText
RELEASE loPdf
CANCEL
ENDIF
loJson = CreateObject('Chilkat.JsonObject')
loJsonOut = CreateObject('Chilkat.JsonObject')
* "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.
lnSuccess = loPdf.ExtractText("assess",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
? loPdf.LastErrorText
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
CANCEL
ENDIF
* 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.
? "Rating: " + loJsonOut.StringOf("quality.rating")
* Is the PDF tagged (does it have a structure tree)?
? "Tagged: " + STR(loJsonOut.BoolOf("quality.tagged"))
* The pages that are images only and would need OCR, such as "2" or "4,9".
? "Pages needing OCR: " + loJsonOut.StringOf("quality.pagesNeedingOcr")
* The per-page report.
i = 0
lnCount_i = loJsonOut.SizeOfArray("quality.pages")
DO WHILE i < lnCount_i
loJsonOut.I = i
* textSource is "text" (real text), "ocrLayer" (invisible text over an image),
* "imageOnly" (needs OCR), "mixed", or "empty".
? "Page " + STR(loJsonOut.IntOf("quality.pages[i].page")) + ": rating " + loJsonOut.StringOf("quality.pages[i].rating") + ", textSource " + loJsonOut.StringOf("quality.pages[i].textSource")
* Each issue has a machine-readable code and a sentence.
j = 0
lnCount_j = loJsonOut.SizeOfArray("quality.pages[i].issues")
DO WHILE j < lnCount_j
loJsonOut.J = j
? " " + loJsonOut.StringOf("quality.pages[i].issues[j].code") + ": " + loJsonOut.StringOf("quality.pages[i].issues[j].message")
j = j + 1
ENDDO
i = i + 1
ENDDO
* Decide what to do with the document:
IF (loJsonOut.StringOf("quality.rating") = "none") THEN
? "No extractable text; the whole document needs OCR."
ELSE
IF (loJsonOut.StringOf("quality.pagesNeedingOcr") <> "") THEN
? "Some pages need OCR; the remaining pages can be extracted with the text or markdown operation."
ELSE
? "All pages can be extracted."
ENDIF
ENDIF
* 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.
RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut