gpt4 book ai didi

ios - 如何使用 Swift 解析 PDF 页面中的内容

转载 作者:搜寻专家 更新时间:2023-10-30 23:09:45 26 4
gpt4 key购买 nike

文档对我来说不是很清楚。到目前为止,我认为我需要设置一个 CGPDFOperatorTable,然后为每个 PDF 页面创建一个 CGPDFContentStreamCreateWithPage 和 CGPDFScannerCreate。

文档提到了设置回调,但我不清楚如何设置。如何从页面中实际获取内容?

到目前为止,这是我的代码。

    let pdfURL = NSBundle.mainBundle().URLForResource("titleofdocument", withExtension: "pdf")

// Create pdf document
let pdfDoc = CGPDFDocumentCreateWithURL(pdfURL)

// Nr of pages in this PF
let numberOfPages = CGPDFDocumentGetNumberOfPages(pdfDoc) as Int

if numberOfPages <= 0 {
// The number of pages is zero
return
}

let myTable = CGPDFOperatorTableCreate()

// lets go through every page
for pageNr in 1...numberOfPages {

let thisPage = CGPDFDocumentGetPage(pdfDoc, pageNr)
let myContentStream = CGPDFContentStreamCreateWithPage(thisPage)
let myScanner = CGPDFScannerCreate(myContentStream, myTable, nil)

CGPDFScannerScan(myScanner)

// Search for Content here?
// ??

CGPDFScannerRelease(myScanner)
CGPDFContentStreamRelease(myContentStream)

}

// Release Table
CGPDFOperatorTableRelease(myTable)

这是一个类似的问题:PDF Parsing with SWIFT但还没有答案。

最佳答案

这是一个在 Swift 中实现的回调示例:

    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 numPages = CGPDFDocumentGetNumberOfPages(pdfDocument)
for pageNum in 1...numPages {
let page = CGPDFDocumentGetPage(pdfDocument, pageNum)
let stream = CGPDFContentStreamCreateWithPage(page)
let scanner = CGPDFScannerCreate(stream, operatorTableRef, nil)
CGPDFScannerScan(scanner)
CGPDFScannerRelease(scanner)
CGPDFContentStreamRelease(stream)
}

关于ios - 如何使用 Swift 解析 PDF 页面中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36115512/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com