3

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.

Rengers
  • 14,911
  • 1
  • 36
  • 54
Vegepilot
  • 98
  • 1
  • 8
  • looks similar to my issue on a Mac. Did you find a solution? – Wizard of Kneup Sep 20 '19 at 20:08
  • I tried further. My problem isn't about saving annotations. I cannot retrieve the dataRepresentation of a PDF. Immediate conversation back to data throws an issue. So, this is my current test: `let doc = PDFDocument(data: pdfData) guard let data1 = doc?.dataRepresentation(options: [:]) else { throw ExportError.couldntSavePDF }` – Wizard of Kneup Sep 22 '19 at 20:25
  • Turns out my problem wasn't actually a problem. The file got lost in the sandbox and I was looking in the wrong place for it. @WizardofKneup have you tried `let data = self.pdfView.document?.dataRepresentation()` or maybe I'm mis-understanding the problem. What error is it throwing? – Vegepilot Sep 24 '19 at 16:12
  • Ok, thanks. I don't use a pdfView. I just turned a View into PDF data (`open func dataWithPDF(inside rect: NSRect) -> Data` ), after that I created a PDFDocument with the data (`let doc = PDFDocument(data: pdfData)` ), and then try to retrieve the data again from the very same doc (which is valid) via (`doc?.dataRepresentation(options: [:])` ). And that crashes without a good explanation. (`cxa_throws`) or similar. – Wizard of Kneup Sep 24 '19 at 16:36
  • Actually, it suddenly seem to work. Not sure why. I did do a clean build folder in between, so it might have been an unrelated issue. Thanks, @Vegepilot – Wizard of Kneup Sep 24 '19 at 16:45

0 Answers0