可以在不删除注释/构建新注释的情况下在 PDFKit 中更改注释的文本(即contents
)吗?FreeText
在 a 中查看时,以下代码段不会更改注释的内容PDFView
:
let url = Bundle.main.url(forResource: "Test", withExtension: "pdf")!
let document = PDFDocument(url: url)!
for index in 0..<document.pageCount {
let page: PDFPage = document.page(at: index)!
let annotations = page.annotations
for annotation in annotations {
annotation.contents = "[REPLACED]"
}
}
mainPDFView.document = document
这可行 - 但需要替换注释(因此必须复制注释的所有其他细节):
let url = Bundle.main.url(forResource: "Test", withExtension: "pdf")!
let document = PDFDocument(url: url)!
for index in 0..<document.pageCount {
let page: PDFPage = document.page(at: index)!
let annotations = page.annotations
for annotation in annotations {
print(annotation)
page.removeAnnotation(annotation)
let replacement = PDFAnnotation(bounds: annotation.bounds,
forType: .freeText,
withProperties: nil)
replacement.contents = "[REPLACED]"
page.addAnnotation(replacement)
}
}
mainPDFView.document = document
注意:添加/删除相同的注释也无济于事。