在iPhone SDK中注释PDF

Jac*_*ack 13 pdf iphone annotate

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

  1. 是否有可能做到这一点?
  2. 最好的方法是什么?
  3. 我可以包含一个框架或库来帮助解决这个问题吗?

谢谢.

Obl*_*ely 18

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

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

- (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);
}
Run Code Online (Sandbox Code Playgroud)

  • 这是一个巨大的帮助.一旦我对此有了更深入的了解,我将创建一个与社区分享的github项目. (2认同)