[英]Update PDF View in Swift with PDFKit using existing PDF file
[英]Edit and save existing pdf documents in iOS using Swift 5 - PDFKit
我一直在嘗試編輯現有的 pdf 文件並將其保存到設備。 我曾嘗試將編輯過的文件保存到與未編輯過的文件相同的位置。 但是文件無法保存。 相反,我收到了類似的錯誤
這是我的代碼:
pdfView.document?.write(to: pdfView.document!.documentURL!)
如何使用PDFKit
在 pdf 文件中保存已編輯的更改?
這是保存PDF的代碼:
pdfView.document?.write(toFile: "Path To Save the File.")
如果您仍然無法保存更改,請重新檢查保存 pdf 的路徑。
在同一個文件夾中,我認為不能創建兩個同名的文件。
在這種情況下,這是因為舊文件正在被新文件替換。
我想通了,我的 PDF 表單也有類似的問題。 該表格最初是用 Word 創建的,然后轉換為 PDF 格式。 當我從頭開始使用 Adobe 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.