PdfKit高亮注释 [英] PdfKit highlight annotation

查看:121
本文介绍了PdfKit高亮注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在iOS上使用PDFKit向文档添加突出显示注释.

I'm trying to add highlight annotations to documents using PDFKit on iOS.

let highlight = PDFAnnotation(bounds: selection.bounds(for: page),
                              forType: PDFAnnotationSubtype.highlight,
                              withProperties: nil)

highlight.color = color

page.addAnnotation(highlight)
page.displaysAnnotations = true

使用上面的代码添加它们时,它们显示为两个不同形状的层.将它们保存到PDF文件并重新打开时,它们将正确显示.

When adding them using the code above, they appear as two differently shaped layers. When saving them to the PDF file and reopening it they display correctly.

此屏幕截图

使用此处提供的代码段以相同的方式添加了顶部和底部的突出显示.顶部的文件已保存到pdf文档中,并在重新打开时按预期显示,底部的文件已被添加.

The top and bottom highlights have been added the same way using the snippet provided here. The top one has been saved to the pdf document and appears as expected when reopening it, the bottom one has just been added.

有没有人知道如何正确显示它们(例如最上面的一个),而又不求助于保存并重新打开文件?

Does anyone know how to have them display correctly (i.e. like the top one) without resorting to saving and reopening the file?

推荐答案

我最终使用了这种hack方法,该方法在我看来考虑了所有情况.希望对您有所帮助.

I've ended up with this hack method which takes into consideration all cases in my opinion. Hope that will help.

private func hackScroll(to page: PDFPage) {

    switch (pdfView.canGoToFirstPage(), pdfView.canGoToLastPage()) {
    case (true, false):
        pdfView.goToFirstPage(nil)
        pdfView.go(to: page)
    case (false, true):
        pdfView.goToLastPage(nil)
        pdfView.go(to: page)
    case (false, false):
        guard let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
        let file = "temp.pdf"

        /// Save to temp file
        let fileURL = dir.appendingPathComponent(file)
        pdfView.document?.write(to: fileURL)

        /// Read from temp file
        guard let document = PDFDocument(url: fileURL) else { return }
        pdfView.document = document
        pdfView.autoScales = true
    default:
        pdfView.goToFirstPage(nil)
        pdfView.go(to: page)
    }
}

这篇关于PdfKit高亮注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆