![](/img/trans.png)
[英]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.