My mission seems simple. Change the text value of an existing annotation and save it under a new file name (even the same file name would be progress). I've successfully changed the annotation (verified by printing to the log immediately after changing it) but as of yet I haven't been able to save it.
I've researched the various ways the community has suggested for saving PDF data and they all throw the same error to the log:
"[Unknown process name] Failed to load /System/Library/PrivateFrameworks/CorePDF.framework/Versions/A/CorePDF"
This shows up after data.write is called. I've also tried pdfView.document?.write(to: docpath!)
and the associated code that would make it work and I get the same error.
It doesn't crash but when I re-launch the app to load the "new" PDF the annotation still has the old value.
Am I missing something obvious?
@IBAction func updatePressed(_ sender: Any) {
if let path = Bundle.main.path(forResource: "SOME_FILENAME", ofType: "pdf") {
let url = URL(fileURLWithPath: path)
if let pdfDocument = PDFDocument(url: url){
let page = pdfDocument.page(at: 0)
let annotations = page?.annotations
for annotation in annotations! {
if annotation.fieldName == "REMARKS 1" {
annotation.setValue("WHY ISN'T THIS WORKING", forAnnotationKey: .widgetValue)
page?.removeAnnotation(annotation)
page?.addAnnotation(annotation)
}
}
}
guard let url2 = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first,
let data = pdfView.document?.dataRepresentation() else {return}
let fileURL = url2.appendingPathComponent("SOME_OTHER_FILENAME")
do {
try data.write(to: fileURL)
} catch {
print(error.localizedDescription)
}
}
}
I'd like to be able to save the new annotation so that I can both view and print the document with the revised annotations. So far all I get is the original value.