I need to remove all annotations from PDF document using PDFKit. Here is my solution:
This solution doesn't work for me, because in one case im getting exception when mutating array while iteration on it.
func removeAllAnnotations() {
guard let documentCheck = document else { return }
for i in (0..<documentCheck.pageCount) {
if let page = documentCheck.page(at: i) {
for annotation in page.annotations {
page.removeAnnotation(annotation)
}
}
}
}
If you want to avoid the “mutate while iterating” problem, just create your own local copy of the array, and iterate through that:
func removeAllAnnotations() {
guard let document = document else { return }
for i in 0..<document.pageCount {
if let page = document.page(at: i) {
let annotations = page.annotations
for annotation in annotations {
page.removeAnnotation(annotation)
}
}
}
}
But, no, I don’t know of any better way to remove all annotations.