我不想创建新的 PDF 文件,我已经完成了,但想通过代码在 iOS 中显示和编辑现有的 pdf 文件..
这可能与否,如果可能,我该怎么做...
更新
假设有一个文本文档pdf,我们想在该文件中输入一些其他文本并保存它..那么如何通过编程来做到这一点?
请帮我解决这个问题...
提前致谢...
最佳答案
我能够通过创建原始 PDF 的副本并在构建过程中修改它来做到这一点。
PS:在我的解决方案中,我需要编辑新 PDF 的文档信息,所以我使用了 执行此操作的主题参数。
在 Swift 3 中
func createPDF(on path: String?, from templateURL: URL?, with subject: String?) {
guard let newPDFPath = path,
let pdfURL = templateURL else { return }
let options = [(kCGPDFContextSubject as String): subject ?? ""] as CFDictionary
UIGraphicsBeginPDFContextToFile(newPDFPath, .zero, options as? [AnyHashable : Any])
let templateDocument = CGPDFDocument(pdfURL as CFURL)
let pageCount = templateDocument?.numberOfPages ?? 0
for i in 1...pageCount {
//get bounds of template page
if let templatePage = templateDocument?.page(at: i) {
let templatePageBounds = templatePage.getBoxRect(.cropBox)
//create empty page with corresponding bounds in new document
UIGraphicsBeginPDFPageWithInfo(templatePageBounds, nil)
let context = UIGraphicsGetCurrentContext()
//flip context due to different origins
context?.translateBy(x: 0.0, y: templatePageBounds.height)
context?.scaleBy(x: 1.0, y: -1.0)
//copy content of template page on the corresponding page in new file
context?.drawPDFPage(templatePage)
//flip context back
context?.translateBy(x: 0.0, y: templatePageBounds.height)
context?.scaleBy(x: 1.0, y: -1.0)
// -->>>> Do your change here <<<<--
/* Here you can do any drawings */
}
}
UIGraphicsEndPDFContext()
}
关于iphone - 如何在 iOS 应用程序中显示和编辑现有的 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7644340/