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

查看:31
本文介绍了在 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) 处的示例注释"字样添加到现有 PDF 中.getPDFFileName 方法返回原始 PD 的路径.getTempPDFFileName 返回新 PDF 的路径,即原始 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天全站免登陆