代码之家  ›  专栏  ›  技术社区  ›  Jack

在iPhone SDK中注释PDF

  •  13
  • Jack  · 技术社区  · 16 年前

    1. 有可能这样做吗?
    2. 最好的方法是什么?
    3. 是否有一个框架或库,我可以包括,以协助这一点?

    谢谢

    3 回复  |  直到 16 年前
        1
  •  18
  •   Obliquely    15 年前

    您可以通过阅读PDF页面,将其绘制到新的PDF图形上下文中,然后在该图形上下文中绘制额外的内容来进行注释。下面是一些将位置(100.0100.0)处的“示例注释”添加到现有PDF的代码。getPdfielName方法返回原始PD的路径。getTempPDFFileName返回新PDF的路径,即原始PDF加上注释的路径。

    要改变注释,只需添加更多的绘图代码来代替drawInRect:withFont:method。有关如何执行此操作的详细信息,请参阅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);
    }
    
        2
  •  3
  •   lazyprogram    11 年前

    我在stuff工作,创建了一个GitHub项目。请检查一下 here .

        3
  •  1
  •   slf    16 年前

    我不这么认为 PDFAnnotation 或者PDFKit从桌面移植到iPhone。。。。也许是一个很好的借口 file a radar . 然而, Haru