【问题标题】:PdfKit highlight annotationPdfKit 高亮注释
【发布时间】:2017-10-20 20:20:42
【问题描述】:

我正在尝试在 iOS 上使用 PDFKit 为文档添加高亮注释。

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

highlight.color = color

page.addAnnotation(highlight)
page.displaysAnnotations = true

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

this screen capture

使用此处提供的 sn-p 以相同的方式添加了顶部和底部高光。顶部的已保存到pdf文档中,重新打开时按预期显示,底部的刚刚添加。

有谁知道如何在不保存并重新打开文件的情况下正确显示它们(即像顶部一样)?

【问题讨论】:

  • 我们有完全相同的问题
  • 我在 Apple 上记录了一个错误,我建议您也这样做openradar.appspot.com/34784917。您可以在bugreport.apple.com 记录错误
  • 我也通过bugreport.apple.com打开了一个bug,目前还没有答案。
  • 如果您需要解决方案,我们会为 PDFKit 提供一个不会出现此错误的商业替代品。 pspdfkit.com/blog/2017/introducing-pdfxkit
  • @guco 我一直在尝试添加类似的亮点,但似乎无法在文本选择中获得正确的选择。长按一些pdf文本后,有没有办法通过pdfkit获取选择属性?

标签: ios swift pdf pdfkit


【解决方案1】:

所以这是 10.13 中的一个已知错误。有一个解决方法是滚动页面然后回到突出显示

您可以使用以下代码创建高亮:

let page = self.pdfDocument?.page(at: 10)
let bounds = CGRect(x: 85.8660965, y: 786.8891167, width: 298.41, height: 12.1485)
let annotation = PDFAnnotation(bounds: bounds, forType: .highlight, withProperties: nil)
annotation.color = NSColor.blue
page?.addAnnotation(annotation)

然后你需要滚动离开页面并回到突出显示

func annotationScrollHack(page: PDFPage) {
    guard let pdfDocument = self.pdfDocument else { return }

    //When adding highlights to macOS 10.13 it seems like 2 highlights are added.
    //If you scroll to a different page and back the "extra" highlight is removed
    //This function scrolls to the first/last page in the book and then back to the current page
    //rdar://34784917
    let bookScrollView = self.pdfView.documentView?.enclosingScrollView
    let currentVisibleRect = bookScrollView?.contentView.documentVisibleRect
    if (0 ... 3).contains(pdfDocument.index(for: page)) {
        if self.pdfView.canGoToLastPage {
            self.pdfView.goToLastPage(self)
        }
    } else {
        if self.pdfView.canGoToFirstPage {
            self.pdfView.goToFirstPage(self)
        }
    }

    if let currentVisibleRect = currentVisibleRect {
        bookScrollView?.contentView.scroll(to: CGPoint(x: currentVisibleRect.origin.x, y: currentVisibleRect.origin.y))
    }
}

Apple 的回应:

除了他们描述的“hack”之外没有其他解决方法:滚动 离开然后回来是最好的选择。更改缩放系数和还原 它可能会以相同的方式修复它,但不能保证。

【讨论】:

  • macOS 10.13.2 已修复此问题
【解决方案2】:

我最终采用了这种 hack 方法,它考虑了我认为的所有情况。希望这会有所帮助。

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)
    }
}

【讨论】:

    猜你喜欢
    • 2018-08-06
    • 2020-02-25
    • 1970-01-01
    • 2018-04-06
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 2016-05-19
    相关资源
    最近更新 更多