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;             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" ;         //------    }  
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

转载于:https://my.oschina.net/makeffort/blog/89349

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值