我想在pdf中找到一个特定的单词并在上面画一个边框。
我用它来做

func findString(_ string: String,
withOptions options: NSString.CompareOptions = []) -> [PDFSelection]

PDF选举的坐标系是什么?
我想在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
    }
}

然后尝试计算实际的框架我加上前几页的大小,然后这应该是要在滚动视图中添加的框架

关于ios - PDFKit PDFSelecction边界框坐标系,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51273347/

10-15 11:57