swift - 注释 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)")
`
解决方案
所以最后我解决了这个问题。我想更新,因为它可能会伤害别人。有两种方法用于生成 PDF 表单,在 word 中创建 ist 表单,然后将其转移到带有注释的 PDF 中,在第二种方法中,使用 Acro Form 生成 PDF 表单,两者都给出了上述问题。在第三次尝试中,我从头开始使用 Adobe Acrobat DC 创建了 PDF 表单并尝试过,这次保存带有注释的 PDF 没有问题,似乎这是格式转换的问题
推荐阅读
- unity3d - Unity 3D - 无法签署 APK - 虽然我提供了正确的密钥库和密码
- python - 从 obj+mtl 文件中命令行渲染一批 PNG 图像
- python - 从表单获取输入时如何解决django上的编码错误
- mysql - 如何查看表结构sql查询?
- node.js - 如何通过命令行在不同阶段部署无服务器代码?
- oracle-adf - 从任何组件重定向到 adf 中的另一个页面
- php - 无法在 PHP 中访问网络驱动器
- powershell - 如何获取特定对象的最新版本和某个先前版本之间的版本列表
- haskell - 将数字字符串转换为整数
- amazon-rekognition - 存储的视频分析时间过长