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

发布于 2024-12-08 03:12:01 字数 210 浏览 0 评论 0原文

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

这是否可能,如果可能的话我该怎么做...

更新

假设有一个文本文档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...

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?

Please help me to solve this problem...

Thanks in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

离不开的别离 2024-12-15 03:12:01

我可以通过创建原始 PDF 的副本并在构建过程中对其进行修改来做到这一点。

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

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

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

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

In 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()
}
临走之时 2024-12-15 03:12:01

我知道这个问题已经太晚了,但我想添加我的解决方案。

我正在使用 UIGraphicsBeginPDFContextToFile 生成 PDF 文件。我从基类 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();
}

我在应用程序委托文件中创建了此类的对象,因为它将保留该对象直到应用程序终止(这是我的要求)。

最初,我调用 beginContextWithFile: ,它将在文档目录中创建一个 pdf 文件以及我通过调用 createInitialPart 方法添加的数据。

在稍后的某个时刻,我需要更新该文件,因此我有另一个名为 secondPart 的方法,尽管我调用它是为了将更多数据附加到该文件中。

一旦我完成了创建,我需要调用 PDFCreator 类的 endContext ,这将完成 pdf 生成,是的,我将拥有更新的文件。

这有点棘手,尽管我有要求,但我没有执行任何内存检查,我找到了解决方案:)

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. I've created a class called PDFCreator from base class UIViewController, in that I have two functions like this:

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

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.

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.

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.

This is little tricky and I didn't perform any memory checks though I had the requirement, I found the solution :)

べ繥欢鉨o。 2024-12-15 03:12:01

您可以使用 Quartz2D 来操作 PDF、打开它们、进行转换等。

查看官方 文档

更新:

按照文档中的说明创建 pdf 上下文,然后您只需要写文字:

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

You can use Quartz2D to manipulate your PDF, open them, make transformation and so on.

Check out the official documentation

Update :

Create your pdf context as explained in the documentation, then you just have to write text :

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文