PdfKit突出显示注释

guc*_*uco 7 pdf pdfkit ios swift

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

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

highlight.color = color

page.addAnnotation(highlight)
page.displaysAnnotations = true
Run Code Online (Sandbox Code Playgroud)

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

这个屏幕截图中

在此输入图像描述

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

有没有人知道如何正确显示它们(即像顶部一样)而无需保存并重新打开文件?

Joh*_*erk 2

所以这是 10.13 中的一个已知错误。有一个解决方法:将页面滚动离开,然后返回到突出显示的位置

\n\n

您可以使用以下代码创建突出显示:

\n\n
let page = self.pdfDocument?.page(at: 10)\nlet bounds = CGRect(x: 85.8660965, y: 786.8891167, width: 298.41, height: 12.1485)\nlet annotation = PDFAnnotation(bounds: bounds, forType: .highlight, withProperties: nil)\nannotation.color = NSColor.blue\npage?.addAnnotation(annotation)\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后您需要滚动离开页面并返回到突出显示的位置

\n\n
func annotationScrollHack(page: PDFPage) {\n    guard let pdfDocument = self.pdfDocument else { return }\n\n    //When adding highlights to macOS 10.13 it seems like 2 highlights are added.\n    //If you scroll to a different page and back the "extra" highlight is removed\n    //This function scrolls to the first/last page in the book and then back to the current page\n    //rdar://34784917\n    let bookScrollView = self.pdfView.documentView?.enclosingScrollView\n    let currentVisibleRect = bookScrollView?.contentView.documentVisibleRect\n    if (0 ... 3).contains(pdfDocument.index(for: page)) {\n        if self.pdfView.canGoToLastPage {\n            self.pdfView.goToLastPage(self)\n        }\n    } else {\n        if self.pdfView.canGoToFirstPage {\n            self.pdfView.goToFirstPage(self)\n        }\n    }\n\n    if let currentVisibleRect = currentVisibleRect {\n        bookScrollView?.contentView.scroll(to: CGPoint(x: currentVisibleRect.origin.x, y: currentVisibleRect.origin.y))\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

苹果的回应:

\n\n
\n

除了他们描述的\xe2\x80\x9cack”之外,没有其他解决方法:滚动\n然后返回是最好的选择。更改缩放系数并恢复\n可能会以相同的方式修复它,但不能保证。

\n
\n