IOS PDF 获取目录 及跳转 DEMO

  CGPDFDocumentRef  thePDFDocRef = NULL;   
  •     thePDFDocRef = CGPDFDocumentCreateWithURL( pdfDocument.fileURL );   
  •   
  •     NSMutableArray *cataLogTitles = [NSMutableArray array];   
  •   
  •     CGPDFDictionaryRef catalogDictionary = CGPDFDocumentGetCatalog(thePDFDocRef);   
  •   
  •     CGPDFDictionaryRef namesDictionary = NULL;   
  •     if (CGPDFDictionaryGetDictionary(catalogDictionary, "Outlines", &namesDictionary)) {   
  •   
  •         long int myCount;   
  •         if (CGPDFDictionaryGetInteger(namesDictionary, "Count", &myCount)) {   
  •             NSLog(@"destinationName:%ld", myCount);   
  •         }   
  •   
  •         CGPDFDictionaryRef myDic;   
  •         ifCGPDFDictionaryGetDictionary(namesDictionary, "First", &myDic) )   
  •         {   
  •             CGPDFStringRef myTitle;   
  •             ifCGPDFDictionaryGetString(myDic, "Title", &myTitle) )   
  •             {   
  •   
  •                 NSString *tempStr = (NSString *)CGPDFStringCopyTextString(myTitle);   
  •   
  •                 NSLog(@"myTitle===:%@", tempStr);   
  •                 NSString *num = [self returnCatalogListNumber:myDic PDFDoc:thePDFDocRef];   
  •                 NSDictionary *_MyDic = [NSDictionary dictionaryWithObjectsAndKeys:   
  •                                         tempStr, @"title",   
  •                                         num, @"link",   
  •                                         nil];   
  •   
  •                 [cataLogTitles addObject:_MyDic];   
  •   
  •                 NSLog(@"%@===", num);   
  •                 CGPDFDictionaryRef tempDic;   
  •                 tempDic = myDic;   
  •                 int i = 0;   
  •                 while ( i < myCount ) {   
  •                     ifCGPDFDictionaryGetDictionary( tempDic , "Next", &tempDic) )   
  •                     {   
  •                         CGPDFStringRef tempTitle;   
  •                         ifCGPDFDictionaryGetString(tempDic, "Title", &tempTitle) )   
  •                         {   
  •                             NSString *tempStr = (NSString *)CGPDFStringCopyTextString(tempTitle);   
  •                             NSLog(@"myTitle:%@", tempStr);   
  •   
  •                             NSString *num = [self returnCatalogListNumber:tempDic PDFDoc:thePDFDocRef];   
  •                             NSLog(@"%@", num);   
  •   
  •                             NSDictionary *_MyDic = [NSDictionary dictionaryWithObjectsAndKeys:   
  •                                                     tempStr, @"title",   
  •                                                     num, @"link",   
  •                                                     nil];   
  •                             [cataLogTitles addObject:_MyDic];   
  •   
  •                         }   
  •                     }   
  •   
  •                     i++;   
  •                 }   
  •   
  •             }   
  •         }   
  •   
  •     }   
  •   
  •     NSLog(@"%@", cataLogTitles);   
  •   
  • //=============   
  • -(NSString *)returnCatalogListNumber:(CGPDFDictionaryRef)tempCGPDFDic PDFDoc:(CGPDFDocumentRef)tempCGPDFDoc   
  • {   
  •     //------   
  •     CGPDFDictionaryRef destDic;   
  •     ifCGPDFDictionaryGetDictionary(tempCGPDFDic, "A", &destDic ))   
  •     {   
  •         CGPDFArrayRef destArray;   
  •         if( CGPDFDictionaryGetArray(destDic, "D", &destArray) )   
  •         {   
  •             NSInteger targetPageNumber = 0; // The target page number   
  •   
  •             CGPDFDictionaryRef pageDictionaryFromDestArray = NULL; // Target reference   
  •   
  •             if (CGPDFArrayGetDictionary(destArray, 0, &pageDictionaryFromDestArray) == true)   
  •             {   
  •                 NSInteger pageCount = CGPDFDocumentGetNumberOfPages(tempCGPDFDoc);   
  •   
  •                 for (NSInteger pageNumber = 1; pageNumber <= pageCount; pageNumber++)   
  •                 {   
  •                     CGPDFPageRef pageRef = CGPDFDocumentGetPage(tempCGPDFDoc, pageNumber);   
  •   
  •                     CGPDFDictionaryRef pageDictionaryFromPage = CGPDFPageGetDictionary(pageRef);   
  •   
  •                     if (pageDictionaryFromPage == pageDictionaryFromDestArray) // Found it   
  •                     {   
  •                         targetPageNumber = pageNumber; break;   
  •                     }   
  •                 }   
  •             }   
  •             else // Try page number from array possibility   
  •             {   
  •                 CGPDFInteger pageNumber = 0; // Page number in array   
  •   
  •                 if (CGPDFArrayGetInteger(destArray, 0, &pageNumber) == true)   
  •                 {   
  •                     targetPageNumber = (pageNumber + 1); // 1-based   
  •                 }   
  •             }   
  •   
  •             //NSLog(@"%d====", targetPageNumber);   
  •   
  •             if (targetPageNumber > 0) // We have a target page number   
  •             {   
  •                 return [NSString stringWithFormat:@"%d", targetPageNumber];   
  •             }   
  •         }   
  •   
  •     }   
  •   
  •     return @"0";   
  •     //------   
  • }  
  • CGPDFDocumentRef thePDFDocRef = NULL;
        thePDFDocRef = CGPDFDocumentCreateWithURL( pdfDocument.fileURL );
    
        NSMutableArray *cataLogTitles = [NSMutableArray array];
    
        CGPDFDictionaryRef catalogDictionary = CGPDFDocumentGetCatalog(thePDFDocRef);
    
        CGPDFDictionaryRef namesDictionary = NULL;
        if (CGPDFDictionaryGetDictionary(catalogDictionary, "Outlines", &namesDictionary)) {
    
            long int myCount;
            if (CGPDFDictionaryGetInteger(namesDictionary, "Count", &myCount)) {
                NSLog(@"destinationName:%ld", myCount);
            }
    
            CGPDFDictionaryRef myDic;
            if( CGPDFDictionaryGetDictionary(namesDictionary, "First", &myDic) )
            {
                CGPDFStringRef myTitle;
                if( CGPDFDictionaryGetString(myDic, "Title", &myTitle) )
                {
    
                    NSString *tempStr = (NSString *)CGPDFStringCopyTextString(myTitle);
    
                    NSLog(@"myTitle===:%@", tempStr);
                    NSString *num = [self returnCatalogListNumber:myDic PDFDoc:thePDFDocRef];
                    NSDictionary *_MyDic = [NSDictionary dictionaryWithObjectsAndKeys:
                                            tempStr, @"title",
                                            num, @"link",
                                            nil];
    
                    [cataLogTitles addObject:_MyDic];
    
                    NSLog(@"%@===", num);
                    CGPDFDictionaryRef tempDic;
                    tempDic = myDic;
                    int i = 0;
                    while ( i < myCount ) {
                        if( CGPDFDictionaryGetDictionary( tempDic , "Next", &tempDic) )
                        {
                            CGPDFStringRef tempTitle;
                            if( CGPDFDictionaryGetString(tempDic, "Title", &tempTitle) )
                            {
                                NSString *tempStr = (NSString *)CGPDFStringCopyTextString(tempTitle);
                                NSLog(@"myTitle:%@", tempStr);
    
                                NSString *num = [self returnCatalogListNumber:tempDic PDFDoc:thePDFDocRef];
                                NSLog(@"%@", num);
    
                                NSDictionary *_MyDic = [NSDictionary dictionaryWithObjectsAndKeys:
                                                        tempStr, @"title",
                                                        num, @"link",
                                                        nil];
                                [cataLogTitles addObject:_MyDic];
    
                            }
                        }
    
                        i++;
                    }
    
                }
            }
    
        }
    
        NSLog(@"%@", cataLogTitles);
    
    //=============
    -(NSString *)returnCatalogListNumber:(CGPDFDictionaryRef)tempCGPDFDic PDFDoc:(CGPDFDocumentRef)tempCGPDFDoc
    {
        //------
        CGPDFDictionaryRef destDic;
        if( CGPDFDictionaryGetDictionary(tempCGPDFDic, "A", &destDic ))
        {
            CGPDFArrayRef destArray;
            if( CGPDFDictionaryGetArray(destDic, "D", &destArray) )
            {
                NSInteger targetPageNumber = 0; // The target page number
    
                CGPDFDictionaryRef pageDictionaryFromDestArray = NULL; // Target reference
    
                if (CGPDFArrayGetDictionary(destArray, 0, &pageDictionaryFromDestArray) == true)
                {
                    NSInteger pageCount = CGPDFDocumentGetNumberOfPages(tempCGPDFDoc);
    
                    for (NSInteger pageNumber = 1; pageNumber <= pageCount; pageNumber++)
                    {
                        CGPDFPageRef pageRef = CGPDFDocumentGetPage(tempCGPDFDoc, pageNumber);
    
                        CGPDFDictionaryRef pageDictionaryFromPage = CGPDFPageGetDictionary(pageRef);
    
                        if (pageDictionaryFromPage == pageDictionaryFromDestArray) // Found it
                        {
                            targetPageNumber = pageNumber; break;
                        }
                    }
                }
                else // Try page number from array possibility
                {
                    CGPDFInteger pageNumber = 0; // Page number in array
    
                    if (CGPDFArrayGetInteger(destArray, 0, &pageNumber) == true)
                    {
                        targetPageNumber = (pageNumber + 1); // 1-based
                    }
                }
    
                //NSLog(@"%d====", targetPageNumber);
    
                if (targetPageNumber > 0) // We have a target page number
                {
                    return [NSString stringWithFormat:@"%d", targetPageNumber];
                }
            }
    
        }
    
        return @"0";
        //------
    }

    附带一个查看pdf数据结构的工具
    https://github.com/below/PDF-Voyeur

    你可能感兴趣的:(IOS PDF 获取目录 及跳转 DEMO)