Create a table of contents from a pdf file

Something like this might help you get started:

NSURL *documentURL = ...; // URL to file or http resource etc.
CGPDFDocumentRef pdfDocument = CGPDFDocumentCreateWithURL((CFURLRef)documentURL);
CGPDFDictionaryRef pdfDocDictionary = CGPDFDocumentGetCatalog(pdfDocument);
// loop through dictionary...
CGPDFDictionaryApplyFunction(pdfDocDictionary, ListDictionaryObjects, NULL);
CGPDFDocumentRelease(pdfDocument);

...

void ListDictionaryObjects (const char *key, CGPDFObjectRef object, void *info) {
    NSLog("key: %s", key);
    CGPDFObjectType type = CGPDFObjectGetType(object);
    switch (type) { 
        case kCGPDFObjectTypeDictionary: {
            CGPDFDictionaryRef objectDictionary;
            if (CGPDFObjectGetValue(object, kCGPDFObjectTypeDictionary, &objectDictionary)) {
                CGPDFDictionaryApplyFunction(objectDictionary, ListDictionaryObjects, NULL);
            }
        }
        case kCGPDFObjectTypeInteger: {
            CGPDFInteger objectInteger;
            if (CGPDFObjectGetValue(object, kCGPDFObjectTypeInteger, &objectInteger)) {
                NSLog("pdf integer value: %ld", (long int)objectInteger); 
            }
        }
        // test other object type cases here
        // cf. http://developer.apple.com/mac/library/documentation/GraphicsImaging/Reference/CGPDFObject/Reference/reference.html#//apple_ref/doc/uid/TP30001117-CH3g-SW1
    }    
}

Leave a Comment