簡體   English   中英

使用 Swift 5 - PDFKit 在 iOS 中編輯和保存現有的 pdf 文檔

[英]Edit and save existing pdf documents in iOS using Swift 5 - PDFKit

我一直在嘗試編輯現有的 pdf 文件並將其保存到設備。 我曾嘗試將編輯過的文件保存到與未編輯過的文件相同的位置。 但是文件無法保存。 相反,我收到了類似的錯誤

  • 試圖用 key: 保存字典。 字典鍵必須是字符串類型。
  • 無法為鍵創建字典值:/DR。 無效值。
  • 無法保存注釋鍵的值:/DR。 無效類型。
  • 試圖用 key: 保存字典。 字典鍵必須是字符串類型。

這是我的代碼:

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

如何使用PDFKit在 pdf 文件中保存已編輯的更改?

這是保存PDF的代碼:

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

如果您仍然無法保存更改,請重新檢查保存 pdf 的路徑。

在同一個文件夾中,我認為不能創建兩個同名的文件。

在這種情況下,這是因為舊文件正在被新文件替換。

我想通了,我的 PDF 表單也有類似的問題。 該表格最初是用 Word 創建的,然后轉換為 PDF 格式。 當我從頭開始使用 Adob​​e Acrobat DC 創建 PDF 表單時,它在 PDF 視圖保存中正確注釋 PDF 沒有上述問題

試試這個,可能會解決你的問題

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)

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM