2011-08-26 92 views

回答

3

訪問pdf輪廓不是太棘手。我的outline parser有大約420 LOC。我會張貼一些片段,這樣你就會明白。我不能發佈完整的代碼,因爲它是一個商業庫。

基本上,你這樣開始:

CGPDFDictionaryRef outlineRef; 
if(CGPDFDictionaryGetDictionary(pdfDocDictionary, "Outlines", &outlineRef)) { 

下降到

NSArray *outlineElements = nil; 
CGPDFDictionaryRef firstEntry; 
if (CGPDFDictionaryGetDictionary(outlineRef, "First", &firstEntry)) { 
    NSMutableArray *pageCache = [NSMutableArray arrayWithCapacity:CGPDFDocumentGetNumberOfPages(documentRef)]; 
    outlineElements = [self parseOutlineElements:firstEntry level:0 error:&error documentRef:documentRef cache:pageCache]; 
}else { 
    PSPDFLogWarning(@"Error while parsing outline. First entry not found!"); 
} 

你解析單個項目是這樣的:

// parse title 
NSString *outlineTitle = stringFromCGPDFDictionary(outlineElementRef, @"Title"); 
PSPDFLogVerbose(@"outline title: %@", outlineTitle); 
if (!outlineTitle) { 
    if (error_) { 
     *error_ = [NSError errorWithDomain:kPSPDFOutlineParserErrorDomain code:1 userInfo:nil]; 
    } 
    return nil; 
} 

NSString *namedDestination = nil; 
CGPDFObjectRef destinationRef; 
if (CGPDFDictionaryGetObject(outlineElementRef, "Dest", &destinationRef)) { 
    CGPDFObjectType destinationType = CGPDFObjectGetType(destinationRef); 

最討厭的事情是,你必須Named Destinations在大多數pdf文檔中,需要額外的步驟來解決。我將它們保存在一個數組中,稍後解決。

「正確地對待」花了很長時間,因爲PDF中存在大量差異,即使您按照PDF參考實現了所有內容,但某些文件在您應用之後纔會生效進一步調整。 (PDF是一團糟!)

相關問題