如何在ios应用程序中显示和编辑现有PDF文件 [英] How to show and edit existing PDF files in ios application

查看:168
本文介绍了如何在ios应用程序中显示和编辑现有PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想创建新的PDF文件,我已经完成但希望通过代码在iOS中显示和编辑现有的pdf文件..

I do not want to create new PDF file,that I had already done but want to show and edit existing pdf file in iOS through code..

这是可能与否,如果可能,我该怎么办...

Is this possible or not and if possible than how can I do this...

更新

假设有文本文档pdf,我们想在该文件中输入一些其他文本并保存..如何通过编程来完成?

Suppose there will be text document pdf and we want to enter some other text in that file and save it..so how to do this by programming?

请帮我解决这个问题...

Please help me to solve this problem...

提前致谢...

推荐答案

我能够通过创建原始PDF的副本并在构建过程中对其进行修改来实现这一目的。

I was able to do this by creating a copy of the original PDF and modifying it during the build process.

PS:在我的解决方案中,我需要编辑新PDF的文档信息,因此我使用
主题参数来执行此操作。

PS: In my solution I needed to edit the document info of the new PDF, so I used the subject parameter to do this.

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()
}

这篇关于如何在ios应用程序中显示和编辑现有PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆