我正在尝试阅读 PDF 文件。下面的回调也打印消息,但我无法从 PDF 中获取任何信息。
let pdfBundlePath = Bundle.main.path(forResource: "sample", ofType: "pdf")
let pdfURL = URL.init(fileURLWithPath: pdfBundlePath!)
let pdf = CGPDFDocument(pdfURL as CFURL)
let operatorTableRef = CGPDFOperatorTableCreate()
CGPDFOperatorTableSetCallback(operatorTableRef!, "BT") { (scanner, info) in
print("Begin text object")
}
CGPDFOperatorTableSetCallback(operatorTableRef!, "ET") { (scanner, info) in
print("End text object")
}
CGPDFOperatorTableSetCallback(operatorTableRef!, "Tf") { (scanner, info) in
print("Select font")
}
CGPDFOperatorTableSetCallback(operatorTableRef!, "Tj") { (scanner, info) in
print("Show text")
}
CGPDFOperatorTableSetCallback(operatorTableRef!, "TJ") { (scanner, info) in
print("Show text, allowing individual glyph positioning")
}
let page = pdf!.page(at: 1)
let stream = CGPDFContentStreamCreateWithPage(page!)
let scanner = CGPDFScannerCreate(stream, operatorTableRef, nil)
CGPDFScannerScan(scanner)
CGPDFScannerRelease(scanner)
CGPDFContentStreamRelease(stream)
输出:
Begin text object
Select font
Show text, allowing individual glyph positioning
End text object
// the same output for at least 10 or more times.
但是我不确定如何从中获取实际的字符串?任何建议将不胜感激。