开发者

PDF Table of Contents Parsing with iOS Quartz 2D

This question开发者_高级运维 has been asked before, I know. However, nobody has answered it well. I'm wondering how to parse a PDF's "table of contents" on the iPhone. The docs tell me to use CGPDFDocumentGetCatalog but not how to use it. All they say is that it returns a dictionary. Also, I can't find any example code. Any suggestions?


looks like the closest thing seen on SO is Create a table of contents from a pdf file


It's basically just parsing the CGPDFDictionary called "Outline" in the CGPDFPage.

    // get outline & loop through dictionary...
    CGPDFDictionaryRef outlineRef;
    if(CGPDFDictionaryGetDictionary(pdfDocDictionary, "Outlines", &outlineRef)) {
    }

then you start with the First element and parse your way through.

    CGPDFDictionaryGetDictionary(outlineRef, "First", &firstEntry)

You want to get the Title and the Destination.

   NSString *outlineTitle = PSPDFStringFromPDFDict(outlineElementRef, @"Title");
   CGPDFDictionaryGetObject(outlineElementRef, "Dest", &destinationRef)

The tricky thing starts with getting the correct destination, because there are (horray, PDF!) several ways to store it, plus several ways that are not defined in the PDF Reference but still out in the wild. Plus several variants that are just broken and you have to deal with it.

For example, you could get the Count of the outline dictionary using

CGPDFInteger elements;
if(CGPDFDictionaryGetInteger(outlineRef, "Count", &elements)) {
    PSPDFLog(@"parsing outline: %ld elements. (Count will be ignored anyway)", (long int)elements);
}else {
    PSPDFLogError(@"Error while parsing outline. No outlineRef?");
}

But note that Count sometimes is invalid due to broken PDF creation tools. See PDF as HTML. Even if it's broken, parsers will do their best to display as much data as they can. So my advice is to ignore Count and parse the dictionary anyway. (A few weeks ago I encountered a document that had Count = -10. Go figure)

I can't post the full code, as it's from my commercial PDF library PSPDFKit, and I need to make a living out of it ;) But this should get you started.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜