使用PDFKit提取PDF高亮

问题描述 投票:1回答:1

[我正在尝试在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的正常现象?

swift cocoa pdfkit
1个回答
0
投票

使用PDFAnnotationSubtype.highlight.rawValue获取突出显示的键。如果打印该值,您将看到它是/Highlight。即使我们现在知道密钥,您仍然应该使用枚举值,以防PDFKit中发生任何更改。

enter image description here

所以就您而言...

if annotation.type == PDFAnnotationSubtype.highlight.rawValue {

如果让您感到困惑,请熟悉Enums and Raw Values.

© www.soinside.com 2019 - 2024. All rights reserved.