首页 > 解决方案 > 注释 PDF 文件并快速保存

问题描述

在我使用 swift 的 iOS 应用程序中,我注释了 pdf 字段并保存在文档目录中。该视图正确显示带注释的字段,并且该文件与文档目录中的注释一起保存,但是在调试区域中我收到了消息。两次运行的最后一种方法是好的,但随后也给出了错误消息,尽管显示的文件和保存在 doc 目录中的文件很好。任何想法?

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. 

我使用了各种技术来保存 pdf 文件,但都在调试区域给出了相同的消息`

//第一种删除和保存文件的方法 // 文件保存在调试区出错

        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)")

                }
            }

//删除和保存文件的第二种方法
// 文件保存在调试区出错

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)")
    }

//第三种删除和保存文件的方法
// 在调试区保存空白文件没有任何错误

    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)")
        }

// 第四种删除保存方法 // 文件保存调试区出错

    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)")

//第 5种保存方法
// 调试区出错的文件保存

    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)
    }

// 第 6 种方法

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)")

`

标签: swiftisopdfkit

解决方案


所以最后我解决了这个问题。我想更新,因为它可能会伤害别人。有两种方法用于生成 PDF 表单,在 word 中创建 ist 表单,然后将其转移到带有注释的 PDF 中,在第二种方法中,使用 Acro Form 生成 PDF 表单,两者都给出了上述问题。在第三次尝试中,我从头开始使用 Adob​​e Acrobat DC 创建了 PDF 表单并尝试过,这次保存带有注释的 PDF 没有问题,似乎这是格式转换的问题


推荐阅读