我想在 pdf 中找到一个特定的单词并在其上绘制一个边界框。 为此,我使用
func findString(_ string: String,
withOptions options: NSString.CompareOptions = []) -> [PDFSelection]
PDFSelection 的坐标系是什么? 我想在 pdfview 的顶部绘制边界框(pdfselection 的属性)并在一个尴尬的地方获得矩形。
有人知道如何转换这个坐标吗?
这是我的代码
let searchResult = document.findString("VAT", withOptions: NSString.CompareOptions.caseInsensitive)
for result in searchResult {
let box = result(for: document.page(at: 0)!)
let convertedBox = pdf.convert(box, from: document.page(at: 0)!)
let boxView = UIView(frame: convertedBox)
boxView.backgroundColor = UIColor(red: 0.3, green: 0.6, blue: 0.1, alpha: 0.5)
view.addSubview(boxView)
print(box)
}
谢谢
最佳答案
PDFSelection
是页面中找到匹配项的位置
这个选择的边界是相对于页面本身的。
这是获取边界然后使用它添加注释的示例
let searchResult = document.findString("VAT", withOptions: .caseInsensitive)
for result in searchResult {
let bounds = result.pages.map { result.bounds(for: $0)) }
let annotations = bounds.map { PDFAnnotation(bounds: $0, forType: .highlight, withProperties: nil) }
annotations.forEach { $0.page?.addAnnotation($0) }
}
请注意,如果文本很长并持续到下一页,则可能会在两页或更多页中找到匹配项
在上面的示例中,可以通过以下方式设置注释的颜色:PDFAnnotation.color
属性
PDFView
包含一个 UIScrollView
,您可以通过此扩展获取它:
extension PDFView {
var scrollView: UIScrollView? {
return subviews.first(where: { $0 is UIScrollView }) as? UIScrollView
}
}
然后尝试计算实际框架,我添加前几页的大小,然后这应该是添加到 ScrollView 中的框架
关于ios - PDFKit PDFSelection边界框坐标系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51273347/