Swift Requires Chilkat v11.7.0+
Swift
Clipped, Off-Page and White-on-White Text in PDF Extraction
See more PDF Text Extraction Examples
Demonstrates the "clippedText", "offPageText" and "obscuredText" filters. Text that a reader never sees - hidden by a clipping path, outside the crop box, or drawn in the same color as the box beneath it - is excluded by default, and the quality report notes what was skipped. Each kind can be included.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 is available at hidden_text.pdf
let pdf = CkoPdf()!
success = pdf.loadFile(filePath: "qa_data/pdf/hidden_text.pdf")
if success == false {
print("\(pdf.lastErrorText!)")
return
}
let json = CkoJsonObject()!
let jsonOut = CkoJsonObject()!
// The page has a visible paragraph plus three hidden lines: one drawn outside its clipping
// rectangle, one below the crop box, and one in white on a white box.
success = pdf.extractText(op: "text", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("\(pdf.lastErrorText!)")
return
}
print("---- default ----")
print("\(jsonOut.string(of: "text")!)")
// The quality report says what was left out.
var i: Int = 0
var count_i: Int = jsonOut.size(ofArray: "quality.pages[0].issues").intValue
while i < count_i {
jsonOut.i = i
print("issue: \(jsonOut.string(of: "quality.pages[0].issues[i].code")!)")
i = i + 1
}
// Include everything.
json.updateString(jsonPath: "clippedText", value: "include")
json.updateString(jsonPath: "offPageText", value: "include")
json.updateString(jsonPath: "obscuredText", value: "include")
success = pdf.extractText(op: "text", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("\(pdf.lastErrorText!)")
return
}
print("---- clippedText, offPageText, obscuredText: include ----")
print("\(jsonOut.string(of: "text")!)")
// Sample output:
// ---- default ----
// Visible Text
//
// This paragraph is visible. The page also carries three kinds of text a reader never sees.
//
// End of the visible text.
// issue: obscuredTextSkipped
// issue: clippedTextSkipped
// ---- clippedText, offPageText, obscuredText: include ----
// Visible Text
//
// This paragraph is visible. The page also carries three kinds of text a reader never sees.
//
// CLIPPED: this line is drawn outside its clipping rectangle.
//
// OBSCURED: this line is white on a white box.
//
// End of the visible text.
//
// OFFPAGE: this line sits below the crop box.
}