【问题标题】:Edit and save existing pdf documents in iOS using Swift 5 - PDFKit使用 Swift 5 - PDFKit 在 iOS 中编辑和保存现有的 pdf 文档
【发布时间】:2020-05-16 19:56:21
【问题描述】:

我一直在尝试编辑现有的 pdf 文件并将其保存到设备。我尝试将编辑后的文件保存到与未编辑文件相同的位置。但无法保存文件。相反,我得到了像

这样的错误
  • 尝试使用键保存字典:。字典键必须是字符串类型。
  • 无法为键创建字典值:/DR。值无效。
  • 无法保存注释键的值:/DR。类型无效。
  • 尝试使用键保存字典:。字典键必须是字符串类型。

这是我的代码:

pdfView.document?.write(to: pdfView.document!.documentURL!)

如何使用PDFKit保存pdf文件中的修改?

【问题讨论】:

  • 你好 Kaushik,你找到解决方案了吗,在我的情况下,我可以将数据保存在新文件中,但仍然在调试区域中收到错误消息
  • 不,我没有。但我想我发现了一些东西。而不是覆盖文件,尝试删除旧文件并将新文件保存在该位置。同时我们的要求发生了变化,现在我只是使用“共享表”(UIActivityViewController)保存 pdf 文件(从服务器下载)。所以,基本上我还不知道如何解决这个问题。

标签: ios swift swift5 pdfkit apple-pdfkit


【解决方案1】:

我想通了,我的 PDF 表单也有类似的问题。该表格最初是用word创建的,然后转换为PDF格式。 当我从头开始使用 Adob​​e Acrobat DC 创建 PDF 表单时,它在 PDF 视图保存中正确注释 PDF 而没有上述问题

【讨论】:

    【解决方案2】:

    这是保存 PDF 的代码:

    pdfView.document?.write(toFile: "Path To Save the File.")
    

    如果仍然无法保存更改,请重新检查保存 pdf 的路径。

    我认为,在同一个文件夹中,你不能创建两个同名的文件。

    在这种情况下,这是因为旧文件正在被新文件替换。

    【讨论】:

      【解决方案3】:

      试试这个,也许能解决你的问题

      if let data = pdfDocument?.dataRepresentation(){
      
          
          let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
          let documentsDirectory = paths[0] // Get documents folder
          let folderPathUrl = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("samplePdf.pdf")
          if FileManager.default.fileExists(atPath: folderPathUrl.path){
              try? FileManager.default.removeItem(at: folderPathUrl)
      
          }
          try? data.write(to: folderPathUrl)
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-03-10
        • 2021-01-30
        • 2022-11-02
        • 1970-01-01
        • 2016-02-03
        • 2020-04-11
        • 2013-04-30
        相关资源
        最近更新 更多