Swift Requires Chilkat v11.7.0+
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// 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
let pdf = CkoPdf()!
success = pdf.loadFile(filePath: "qa_data/pdf/mixed_text_image.pdf")
if success == false {
print("\(pdf.lastErrorText!)")
return
}
let json = CkoJsonObject()!
let jsonOut = CkoJsonObject()!
// "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.
success = pdf.extractText(op: "assess", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("\(pdf.lastErrorText!)")
return
}
// 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.
print("Rating: \(jsonOut.string(of: "quality.rating")!)")
// Is the PDF tagged (does it have a structure tree)?
print("Tagged: \(jsonOut.bool(of: "quality.tagged"))")
// The pages that are images only and would need OCR, such as "2" or "4,9".
print("Pages needing OCR: \(jsonOut.string(of: "quality.pagesNeedingOcr")!)")
// The per-page report.
var j: Int
var count_j: Int
var i: Int = 0
var count_i: Int = jsonOut.size(ofArray: "quality.pages").intValue
while i < count_i {
jsonOut.i = i
// textSource is "text" (real text), "ocrLayer" (invisible text over an image),
// "imageOnly" (needs OCR), "mixed", or "empty".
print("Page \(jsonOut.int(of: "quality.pages[i].page").intValue): rating \(jsonOut.string(of: "quality.pages[i].rating")!), textSource \(jsonOut.string(of: "quality.pages[i].textSource")!)")
// Each issue has a machine-readable code and a sentence.
j = 0
count_j = jsonOut.size(ofArray: "quality.pages[i].issues").intValue
while j < count_j {
jsonOut.j = j
print(" \(jsonOut.string(of: "quality.pages[i].issues[j].code")!): \(jsonOut.string(of: "quality.pages[i].issues[j].message")!)")
j = j + 1
}
i = i + 1
}
// Decide what to do with the document:
if jsonOut.string(of: "quality.rating") == "none" {
print("No extractable text; the whole document needs OCR.")
}
else {
if jsonOut.string(of: "quality.pagesNeedingOcr") != "" {
print("Some pages need OCR; the remaining pages can be extracted with the text or markdown operation.")
}
else {
print("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.
}