在iPhone SDK中注释PDF [英] Annotate PDF within iPhone SDK

查看:135
本文介绍了在iPhone SDK中注释PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设法在我的应用程序中实现了一个非常基本的PDF查看器,但是想知道是否可以向PDF添加注释。我查看了SDK文档,但没有找到任何内容。我真的有两个问题:

I have managed to implement a very basic PDF viewer within my application, but was wondering if it was possible to add annotations to the PDF. I have looked through the SDK docs, but not found anything. I have 2 questions really:


  1. 是否可以这样做?

  2. 什么是最好的采取的方法?

  3. 我可以提供一个框架或库来帮助解决这个问题吗?

谢谢。

推荐答案

您可以通过阅读PDF页面,将其绘制到新的PDF图形上下文来进行注释,然后在该图形上下文中绘制额外的内容。下面是一些代码,它们将位置(100.0,100.0)的单词Example annotation添加到现有PDF中。 getPDFFileName方法返回原始PD的路径。 getTempPDFFileName返回新PDF的路径,即原始路径和注释。

You can do annotation by reading in a PDF page, drawing it onto a new PDF graphics context, then drawing extra content onto that graphic context. Here is some code that adds the words 'Example annotation' at position (100.0,100.0) to an existing PDF. The method getPDFFileName returns the path of the original PD. getTempPDFFileName returns the path of the new PDF, the one that is the original plus the annotation.

要更改注释,只需添加更多绘图代码来代替drawInRect:withFont:方法。有关如何操作的更多信息,请参阅适用于iOS的绘图和打印指南。

To vary the annotations, just add in more drawing code in place of the drawInRect:withFont: method. See the Drawing and Printing Guide for iOS for more on how to do that.

- (void) exampleAnnotation;
{
    NSURL* url = [NSURL fileURLWithPath:[self getPDFFileName]];

    CGPDFDocumentRef document = CGPDFDocumentCreateWithURL ((CFURLRef) url);// 2
    size_t count = CGPDFDocumentGetNumberOfPages (document);// 3

    if (count == 0)
    {
        NSLog(@"PDF needs at least one page");
        return;
    }

    CGRect paperSize = CGRectMake(0.0,0.0,595.28,841.89);

    UIGraphicsBeginPDFContextToFile([self getTempPDFFileName], paperSize, nil);

    UIGraphicsBeginPDFPageWithInfo(paperSize, nil);

    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    // flip context so page is right way up
    CGContextTranslateCTM(currentContext, 0, paperSize.size.height);
    CGContextScaleCTM(currentContext, 1.0, -1.0); 

    CGPDFPageRef page = CGPDFDocumentGetPage (document, 1); // grab page 1 of the PDF 

    CGContextDrawPDFPage (currentContext, page); // draw page 1 into graphics context

     // flip context so annotations are right way up
    CGContextScaleCTM(currentContext, 1.0, -1.0);
    CGContextTranslateCTM(currentContext, 0, -paperSize.size.height);

    [@"Example annotation" drawInRect:CGRectMake(100.0, 100.0, 200.0, 40.0) withFont:[UIFont systemFontOfSize:18.0]];

    UIGraphicsEndPDFContext();

    CGPDFDocumentRelease (document);
}

这篇关于在iPhone SDK中注释PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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