简体   繁体   English

如何在ios应用程序中显示和编辑现有PDF文件

[英]How to show and edit existing PDF files in ios application

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.. 我不想创建新的PDF文件,我已经完成但希望通过代码在iOS中显示和编辑现有的pdf文件。

Is this possible or not and if possible than how can I do this... 这是否可能,如果可能,我怎么能这样做......

Update 更新

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? 假设将有文本文档pdf,我们想在该文件中输入一些其他文本并保存它。那么如何通过编程来完成?

Please help me to solve this problem... 请帮我解决这个问题......

Thanks in advance... 提前致谢...

I know its too late for this question, however I'd like to add my solution. 我知道这个问题为时已晚,但我想补充一下我的解决方案。

I am using UIGraphicsBeginPDFContextToFile to generate a PDF file. 我正在使用UIGraphicsBeginPDFContextToFile生成PDF文件。 I've created a class called PDFCreator from base class UIViewController , in that I have two functions like this: 我从基类UIViewController创建了一个名为PDFCreator类,因为我有两个这样的函数:

- (void) beginContextWithFile:(NSString *)filename
{
    pageSize = CGSizeMake(1024, 1424);

    UIGraphicsBeginPDFContextToFile(filename, CGRectZero, nil);

    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);

    [self createInitialPart];
}

- (void) endContext
{
    UIGraphicsEndPDFContext();
}

I've created an object of this class in the app delegate file, as it will retain the object until the app terminates (as was my requirement). 我在app委托文件中创建了这个类的对象,因为它将保留对象,直到应用程序终止(这是我的要求)。

Initially, I am calling beginContextWithFile: and it will create a pdf file in the document directory along with data I am adding through a call to the createInitialPart method. 最初,我调用beginContextWithFile:它将在文档目录中创建一个pdf文件以及我通过调用createInitialPart方法添加的数据。

At some point later I need to update that file, so I have another method named secondPart though I called it for some more data to append to that file. 稍后我需要更新该文件,所以我有另一个名为secondPart方法,虽然我调用了一些方法来附加到该文件。

Once I fill I've done with the creation, I need to call endContext of PDFCreator class, that will complete the pdf generation and yes, I will have the updated file. 一旦我完成了创作,我需要调用PDFCreator类的endContext ,这将完成pdf生成,是的,我将拥有更新的文件。

This is little tricky and I didn't perform any memory checks though I had the requirement, I found the solution :) 这有点棘手,我没有执行任何内存检查虽然我有要求,我找到了解决方案:)

I was able to do this by creating a copy of the original PDF and modifying it during the build process. 我能够通过创建原始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. PS:在我的解决方案中,我需要编辑新PDF的文档信息,因此我使用subject参数来执行此操作。

In Swift 3 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()
}

You can use Quartz2D to manipulate your PDF, open them, make transformation and so on. 您可以使用Quartz2D来操作PDF,打开它们,进行转换等等。

Check out the official documentation 查看官方文档

Update : 更新:

Create your pdf context as explained in the documentation, then you just have to write text : 按照文档中的说明创建pdf上下文,然后您只需编写文本:

http://developer.apple.com/library/ios/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_text/dq_text.html#//apple_ref/doc/uid/TP30001066-CH213-TPXREF101 http://developer.apple.com/library/ios/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_text/dq_text.html#//apple_ref/doc/uid/TP30001066-CH213-TPXREF101

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM