Codicil
Codicil

Reputation: 306

Annotating PDF file and saving in swift

In my iOS app using swift, I annotated the pdf field and saved in the document directory. The view correctly display annotated field and the file is saved with annotation in document directory, however in the debugging area I am getting messages. The last method for two run was Ok but then also giving error message although displayed file and saved file in doc directory was fine. Any idea?

2020-02-14 21:51:23.245079-0800 PDFSaving[4092:1084625] [Unknown process name] Failed to load /System/Library/PrivateFrameworks/CorePDF.framework/Versions/A/CorePDF


2020-02-14 21:51:23.262963-0800 PDFSaving[4092:1084625] Error: Attempting to save dictionary with key: <CGPDFNameRef (/Encoding)>. Dictionary keys must be of type string.


2020-02-14 21:51:23.263062-0800 PDFSaving[4092:1084625] Error: Attempting to save dictionary with key: <CGPDFNameRef (/Font)>. Dictionary keys must be of type string.


2020-02-14 21:51:23.263113-0800 PDFSaving[4092:1084625] Error: Could not create dictionary value for key: /DR. Invalid value.


2020-02-14 21:51:23.263161-0800 PDFSaving[4092:1084625] Error: Cannot save value for annotation key: /DR. Invalid type. 

I have used various techniques to save the pdf file but all are giving same message in the debugging area`

// 1st method of deleting and saving file // File saving with errors in debugging area

        let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let fileNameWithPath = "\(path)/editedFile.pdf"

        if FileManager.default.fileExists(atPath: getFilePath()){
            do{
                try FileManager.default.removeItem(atPath: getFilePath())
                print("File path for deleted file: \(path)")
                print("File is deleted successfully, path & filename of deleted file: \(fileNameWithPath)")
            }catch{
                print("error while deleting item from docDir :: \(error.localizedDescription)")
            }
        }

        if let data = pdfDocument.dataRepresentation(){

            let url = URL(fileURLWithPath: fileNameWithPath)
            do{
               try data.write(to: url)

                print("pdf saved successfully \(fileNameWithPath)")
            }catch{
                print("error while saving file \(error.localizedDescription)")

                }
            }

// 2nd method of deleting and saving file
// File saving with errors in debugging area

let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let fileNameWithPath = "\(path)/editedFile.pdf"

if FileManager.default.fileExists(atPath: getFilePath()){
    do{
        try FileManager.default.removeItem(atPath: getFilePath())
        print("File path for deleted file: \(path)")
        print("File is deleted successfully, path & filename of deleted file: \(fileNameWithPath)")
    }catch{
        print("error while deleting item from docDir :: \(error.localizedDescription)")
    }
}

    guard let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first,
        let data = pdfView.document?.dataRepresentation() else {return}
    let fileURL = url.appendingPathComponent(filename)

    do {
        try data.write(to: fileURL)
        print("File path of saved file : \(path)")
        print("pdf saved successfully, path and filename of saved file: \(fileNameWithPath)")
    } catch {
        print("error while saving file \(error.localizedDescription)")
    }

// 3rd method of deleting and saving file
// Blank file is saving without any error in debugging area

    let bundlePath = Bundle.main.path(forResource: "QW484A_2017R8", ofType: ".pdf")
    let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
    let fileManager = FileManager.default
    let fullDestPath = URL(fileURLWithPath: destPath).appendingPathComponent("editedFile.pdf")

    if fileManager.fileExists(atPath: fullDestPath.path){

               do {

                   try fileManager.removeItem(at: fullDestPath)
                   print("File path for removed file: \(destPath)")
                   print("File path & filename of file removed: \(fullDestPath)")
               }
                   catch{
                       print("error while removing modified file: \(error.localizedDescription)")
                   }
               }
    do{
        try fileManager.copyItem(atPath: bundlePath!, toPath: fullDestPath.path)
        print("File path of saved file : \(destPath)")
        print("database saved successfully, path and filename of saved file: \(fullDestPath.path)")

    }catch{
        print("error while saving modified file: \(error.localizedDescription)")
        }

// 4th method of deleting and saving // File saving with errors in debugging area

    let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
    let fileManager = FileManager.default
    let fullDestPath = URL(fileURLWithPath: destPath).appendingPathComponent("editedFile.pdf")

    if fileManager.fileExists(atPath: fullDestPath.path){

               do {

                   try fileManager.removeItem(at: fullDestPath)
                   print("File path for removed file: \(destPath)")
                   print("File path & filename of file removed: \(fullDestPath)")
               }
                   catch{
                       print("error while removing modified file: \(error.localizedDescription)")
                   }
               }

    self.pdfDocument.write(to: fullDestPath)
    print("The save filed path and name: \(fullDestPath)")

// 5th method of saving
// File saving with errors in debugging area

    guard let url2 = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first,
        let data = pdfView.document?.dataRepresentation() else {return}
    let fileURL = url2.appendingPathComponent("editedfile.pdf")



    do {
        try data.write(to: fileURL)
        print("print path: \(fileURL)")
    } catch {
        print(error.localizedDescription)
    }

// 6th method

let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
       let fileManager = FileManager.default
       let fullDestPath = URL(fileURLWithPath: destPath).appendingPathComponent("editedFile.pdf")

       if fileManager.fileExists(atPath: fullDestPath.path){

                  do {

                      try fileManager.removeItem(at: fullDestPath)
                      print("File path for removed file: \(destPath)")
                      print("File path & filename of file removed: \(fullDestPath)")
                  }
                      catch{
                          print("error while removing modified file: \(error.localizedDescription)")
                      }
                  }

        self.pdfView.document?.write(to: fullDestPath)

        print("The save filed path and name: \(fullDestPath)")

`

Upvotes: 1

Views: 1688

Answers (1)

Codicil
Codicil

Reputation: 306

So finally I work around this issue. I thought to update as it may be hurting others. There two methods used for PDF form generation, ist form was created in word and than transferred to PDF with annotations, in 2nd method PDF form was generated using Acro Form, both were giving above problem. In 3rd attempt I created the the PDF form using Adobe Acrobat DC from scratch and tried, this time no problem in saving the PDF with annotations, seems this is the issue with format transformation

Upvotes: 1

Related Questions