0

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?

Sam Stewart
  • 1,470
  • 3
  • 14
  • 14

2 Answers2

0

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.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
steipete
  • 7,581
  • 5
  • 47
  • 81
  • Negative Count value means the outline is displayed collapsed in the viewer. Positive Count value means the outline is displayed expanded in the viewer. – iPDFdev Jul 16 '12 at 14:06
  • Damn, thanks for that! You never stop learning on PDF related stuff. Although this seems quite obscure. (Why not an extra property for that?) Thanks! – steipete Jul 16 '12 at 14:28
0

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

Community
  • 1
  • 1
shawnwall
  • 4,549
  • 1
  • 27
  • 38