iphone - 如何在 iOS 应用程序中显示和编辑现有的 PDF 文件

标签 iphone objective-c ios ipad pdf

我不想创建新的 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/

相关文章:

iphone - Storyboard 中的 Root View Controller

iphone - UIPopover 委托(delegate) - 无论协议(protocol)/声明如何都无法分配

ios - 将 UIImage 从 ImagePicker 解包到 Firebase.Storage 时致命

ios - UISwitch - 检测动画结束

ios - 我可以在后台观察来电和去电吗?

php - 使用php输出一个mp4视频

ios - 所有 View 的监听器

iphone - 使用 OCunit 在 iOS 上测试异步代码

iphone - MKMapView 忽略 iOS 4 中 centerOffset 的更新

iOS - NSArray indexOfObject 不调用自定义的 isEqual