Create a table of contents from a pdf file

前端 未结 4 925
不思量自难忘°
不思量自难忘° 2020-12-02 18:02

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

相关标签:
4条回答
  • 2020-12-02 18:22

    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
        }    
    }
    
    0 讨论(0)
  • 2020-12-02 18:32

    You create a Table of Contents (AKA Outlines, Bookmarks) using PDFKit's PDFOutline object. You can also read existing Outlines within a PDF.

    CGPDFDocumentGetCatalog is a method for getting the Catalog of PDF data in the file, which is at a much lower-level than human concepts of 'Contents'.

    In the absence of existing Outlines, I'm not sure there's a way to get meaningful data from the PDF stream itself, e.g. trying to grok Chapter headings just from the PDF data.

    0 讨论(0)
  • 2020-12-02 18:37

    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.

    0 讨论(0)
  • 2020-12-02 18:38

    I did it with a freeware library for iOS FastPdfKit

    0 讨论(0)
提交回复
热议问题