2010-02-22 79 views
13

我已經設法在我的應用程序中實現了一個非常基本的PDF查看器,但想知道是否可以向PDF添加註釋。我瀏覽了SDK文檔,但沒有發現任何內容。我真的有兩個問題:在iPhone SDK中註釋PDF

  1. 是否可以這樣做?
  2. 什麼是最好的方法?
  3. 有沒有我可以包含的框架或庫來協助這個?

謝謝。

+0

嗨,傑克,你有註釋嗎? – 2014-11-12 17:34:35

回答

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); 
} 
+2

這是一個巨大的幫助。一旦我對此有了更深入的瞭解,我將創建一個github項目與社區分享。 – Jack 2011-06-17 20:46:53

+0

你做了一個GitHub項目@Jack嗎? – 2012-02-20 17:20:22

+0

@Metthew我試過你的例子,但我無法打開文檔。我收到了'Permission denied'錯誤。 – Justin 2012-06-18 11:36:54

1

我不認爲PDFAnnotation或PDFKit從桌面移植到iPhone ....可能是一個偉大的藉口file a radar。但是,Haru可能會在此期間得到您。

+0

目前,Haru只能在以下平臺上運行: 1. Cygwin + GCC(Microsoft Windows) 2. Cygwin + MinGW(Microsoft Windows) 3. MSYS + MinGW(Microsoft Windows) 3. Microsoft VC++ Microsoft Windows) 4. Borland C++(Microsoft Windows) 5. GCC(Linux,FreeBSD,NetBSD,Solaris ...) – 2015-12-08 07:25:28

3

我工作的東西,並創建了一個GitHub項目。請查看here

+0

但是,您的項目不會將註釋保存到pdf中。我檢查了源代碼,它將所有註釋信息歸檔爲獨立於實際的pdf文件。所以,如果我將PDF文件傳遞給另一臺機器,那麼PDF將不加任何更改地傳輸。 – 2015-02-27 07:25:20

+0

它正在開發中。請稍後退房:) – lazyprogram 2015-03-02 05:55:05

+0

@lazyprogram,善意地更新它的庫,因爲我得到的dylib錯誤和應用程序在從xcode 7運行時崩潰的設備。 – 2015-12-16 13:34:40