Can the text (i.e. contents
) of a FreeText
annotation be changed in PDFKit without deleting an annotation / building a new annotation?
The following snippet does not change an annotation's contents when viewing in 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
This works - but requires replacing an annotation (and thus having to copy over all the other details of the annotation):
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
Note: adding / removing the same annotation also does not help.
setContents
you are referencing is just the getter / settercontents
. My example is using that. – Lissa.freeText
otherwise changing thecontents
variable won't change anything. – Spasm.freeText
. Is the sample you are running on iOS or Mac? – Lissa