我的任务似乎很简单。更改现有注释的文本值并将其保存在新文件名下(即使是相同的文件名也会进步)。我已经成功地更改了注释(通过在更改后立即打印到日志来验证)但到目前为止我还无法保存它。
我研究了社区建议保存 PDF 数据的各种方法,它们都向日志中抛出了相同的错误:
“[未知进程名称] 无法加载 /System/Library/PrivateFrameworks/CorePDF.framework/Versions/A/CorePDF”
这会在调用 data.write 后显示。我也尝试pdfView.document?.write(to: docpath!)
过相关的代码,使其工作,我得到了同样的错误。
它不会崩溃,但是当我重新启动应用程序以加载“新”PDF 时,注释仍然具有旧值。
我错过了一些明显的东西吗?
@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)
}
}
}
我希望能够保存新注释,以便我可以查看和打印带有修订注释的文档。到目前为止,我得到的只是原始值。