我正在尝试在一个macOS应用程序中使用PDFkit提取所有的亮点。下面是我使用的代码:
guard let path = item.filePath else { return }
let document = PDFDocument(url: URL(fileURLWithPath: path))
guard let numberOfPage = document?.pageCount else { return }
for i in 0...numberOfPage - 1 {
let pages = document?.page(at: i)
guard let annotations = pages?.annotations else { continue }
for annotation in annotations {
if annotation.type == "Highlight" {
print(annotation.contents)
self.annotations.append(annotation)
}
}
}问题是print(annotation.contents)总是返回"Optional("")“。我试了几个pdf,结果总是一样的。问题是,如果我执行print(annotation.color),它将返回给定高光的正确颜色。
是不是我的代码有什么地方出了问题,我还没弄清楚?或者这是PDFKit的正常行为?
发布于 2019-10-03 15:19:33
使用PDFAnnotationSubtype.highlight.rawValue获取高光的关键点。如果您打印该值,您将看到它是/Highlight。即使我们现在知道了关键字,您仍然应该使用枚举值,以防PDFKit中发生任何变化。

所以在你的情况下...
if annotation.type == PDFAnnotationSubtype.highlight.rawValue {如果您对此感到困惑,请熟悉一下Enums and Raw Values.
https://stackoverflow.com/questions/58193213
复制相似问题