Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a table of contents from a pdf file

I'm using quartz to display pdf content, and I need to create a table of contents to navigate through the pdf. From reading Apple's documentation I think I am supposed to use CGPDFDocumentGetCatalog, but I can't find any examples on how to use this anywhere. Any ideas?

Update: Still haven't found a solution for this. I tired Alex' solution but the output I get looks like this:

2011-07-27 09:16:19.359 LDS Scriptures App-iPad[624:707] key: Pages
2011-07-27 09:16:19.361 LDS Scriptures App-iPad[624:707] key: Count
2011-07-27 09:16:19.362 LDS Scriptures App-iPad[624:707] pdf integer value: 238
2011-07-27 09:16:19.363 LDS Scriptures App-iPad[624:707] key: Kids
2011-07-27 09:16:19.366 LDS Scriptures App-iPad[624:707] key: Type
2011-07-27 09:16:19.368 LDS Scriptures App-iPad[624:707] key: Outlines
2011-07-27 09:16:19.370 LDS Scriptures App-iPad[624:707] key: Count
2011-07-27 09:16:19.371 LDS Scriptures App-iPad[624:707] pdf integer value: 7
2011-07-27 09:16:19.372 LDS Scriptures App-iPad[624:707] key: First
2011-07-27 09:16:19.374 LDS Scriptures App-iPad[624:707] key: Parent
2011-07-27 09:16:19.375 LDS Scriptures App-iPad[624:707] key: Count
2011-07-27 09:16:19.376 LDS Scriptures App-iPad[624:707] pdf integer value: 7

No idea yet how to turn that into a usable table of contents. Ideally I would like to get to an array of NSDictionary objects with a title and matching page number.

like image 388
ryan Avatar asked Mar 31 '10 20:03

ryan


People also ask

Can you create table of contents in PDF?

Select the "General" tab in the "Table Of Contents Settings" dialog. Choose to create either internal or external table of contents. Select the "Insert table of contents as …" option to insert table of contents into existing PDF document (internal TOC) and specify a page number where to insert new pages.

How do I extract the contents of a PDF?

Once you've opened the file, click on the "Edit" tab, and then click on the "edit" icon. Now you can right-click on the text and select "Copy" to extract the text you need.


2 Answers

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
    }    
}
like image 50
Alex Reynolds Avatar answered Oct 19 '22 21:10

Alex Reynolds


Here is my approach.
1. Download this framework vfr reader
2. Use this lines of code to get all PDF chapters

NSString *path = [[NSBundle mainBundle] pathForResource:@"Reader" ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSArray *arr = [ReaderDocumentOutline outlineFromFileURL:targetURL password:@""];

Hope it will help you.

like image 29
Ned Avatar answered Oct 19 '22 22:10

Ned