【发布时间】:2018-12-31 11:37:02
【问题描述】:
可以在不删除注释/构建新注释的情况下在 PDFKit 中更改 FreeText 注释的文本(即 contents)吗?
在PDFView 中查看时,以下 sn-p 不会更改注释的内容:
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
注意:添加/删除相同的注释也无济于事。
【问题讨论】:
-
developer.apple.com/documentation/pdfkit/pdfannotation/… ,它有一个函数setContent,试试看!
-
在开发人员文档中,您引用的
setContents只是 getter / settercontents。我的例子就是使用它。 -
我无法重现您的问题,因为第一个代码 sn-p 对我有用:将已创建的注释替换为已替换的内容。
-
您可能需要检查页面的注释确实都是
.freeText否则更改contents变量不会改变任何东西。 -
@PranavKasetti 确认它是
.freeText。您是在 iOS 还是 Mac 上运行的示例?
标签: ios swift apple-pdfkit