现在的位置: 首页 > 综合 > 正文

ios中解析获取pdf目录及相对应的页数

2018年02月14日 ⁄ 综合 ⁄ 共 21074字 ⁄ 字号 评论关闭

 //目录下的名字字典
    if (CGPDFDictionaryGetDictionary(pdfDic, "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:pdf];
                NSDictionary *_MyDic = [NSDictionary dictionaryWithObjectsAndKeys:
                                        tempStr, @"title",
                                        num, @"link",
                                        nil];
                [tempStr release];
                [cataLogTitles addObject:_MyDic];
                //这是第一层目录第一个是否有2级目录 是否有是3及目录
                //第二层目录
                long int SecondCount;
                if (CGPDFDictionaryGetInteger(myDic, "Count", &SecondCount))
                {
//                    NSLog(@"---------第二层目录债市nName:%ld", SecondCount);
                    //有时候secondcount、为负数
                    if (SecondCount <0)
                    {
                        SecondCount = SecondCount*(-1);
                    }
                }
 
                CGPDFDictionaryRef secondMunu;
                //5
                if (CGPDFDictionaryGetDictionary(myDic, "First", &secondMunu))
                {
                    
                    NSLog(@"2级目录");
                    //目录标题
                    CGPDFStringRef tempSecondTitle;
                    if( CGPDFDictionaryGetString(secondMunu, "Title", &tempSecondTitle) )
                    {
                        NSString *tempSecondStr1 = (NSString *)CGPDFStringCopyTextString(tempSecondTitle);
                        NSString *tempSecondStr=[NSString stringWithFormat:@"  %@",tempSecondStr1];
                        NSLog(@"mySecondTitle:%@", tempSecondStr);
                        NSString *secondNum = [self returnCatalogListNumber:secondMunu PDFDoc:pdf];
                        NSLog(@"secondeNum===adadadad======%@",secondNum);
                        NSDictionary *secondDiction=[NSDictionary dictionaryWithObjectsAndKeys:tempSecondStr,@"title",secondNum,@"link",nil];
                        [cataLogTitles addObject:secondDiction];
//                        NSLog(@"catalog=%@" ,cataLogTitles);
                    }
                    CGPDFDictionaryRef secondDic;
                    secondDic=secondMunu;
                    int j=0;
                    //4
                    while ( j<SecondCount)
                    {
                        //3
                        NSLog(@"第二层目录所有的遍历 ");
                        if (CGPDFDictionaryGetDictionary(secondDic, "Next", &secondDic))
                        {
                            CGPDFStringRef secondTempTitle;
                            //2
                            if (CGPDFDictionaryGetString(secondDic, "Title", &secondTempTitle))
                            {
                                NSString *tempSecondStr1=(NSString *)CGPDFStringCopyTextString(secondTempTitle);
                                NSString *tempSecondStr = [NSString stringWithFormat:@"  %@",tempSecondStr1];
                                NSLog(@"第二层目录的titile=======……%@",tempSecondStr);
                                NSString *secondNum = [self returnCatalogListNumber:secondDic PDFDoc:pdf];
                                NSLog(@"secondeNum===awo lgequ ======%@",secondNum);
                                NSDictionary *secondDictionary=[NSDictionary dictionaryWithObjectsAndKeys:tempSecondStr,@"title",secondNum,@"link",nil];
                                [cataLogTitles addObject:secondDictionary];
              
                                //第三层目录
                                long int ThirdCount;
                                if (CGPDFDictionaryGetInteger(secondDic, "Count", &ThirdCount))
                                {
                                    NSLog(@"---------第三层目录债市nName:%ld", ThirdCount);
                                    if (ThirdCount<0)
                                    {
                                        ThirdCount = ThirdCount *(-1);
                                    }
                                }
                                //1
                                CGPDFDictionaryRef thirdMunu;
                                if (CGPDFDictionaryGetDictionary(secondDic, "First", &thirdMunu))
                                {
                                    
                                    //目录标题
                                    CGPDFStringRef tempThirdTitle;
                                    if( CGPDFDictionaryGetString(thirdMunu, "Title", &tempThirdTitle) )
                                    {
                                        NSString *tempThirdStr1 = (NSString *)CGPDFStringCopyTextString(tempThirdTitle);
                                        NSString *tempThirdStr = [NSString stringWithFormat:@"      %@",tempThirdStr1];
                                        NSString *thirdNum = [self returnCatalogListNumber:thirdMunu PDFDoc:pdf];
                                        NSLog(@"secondeNum==、、、、、=======%@",secondNum);
                                        NSDictionary *thirdDiction=[NSDictionary dictionaryWithObjectsAndKeys:tempThirdStr,@"title",thirdNum,@"link",nil];
                                        [cataLogTitles addObject:thirdDiction];  
                                        
                                    }
                                    CGPDFDictionaryRef thirdDic;
                                    thirdDic=thirdMunu;
                                    int third=0;
                                    while (third<ThirdCount)
                                    {
                                        if (CGPDFDictionaryGetDictionary(thirdDic, "Next", &thirdDic))
                                        {
                                            CGPDFStringRef thirdTempTitle;
                                            if (CGPDFDictionaryGetString(thirdDic, "Title", &thirdTempTitle))
                                            {
                                                NSString *tempThirdStr1=(NSString *)CGPDFStringCopyTextString(thirdTempTitle);
                                                
                                                NSString *tempThirdStr = [NSString stringWithFormat:@"      %@",tempThirdStr1];
                                                
                                                NSString *thirdNum = [self returnCatalogListNumber:thirdDic PDFDoc:pdf];
                                                NSLog(@"thirddeNum=========%@",thirdNum);
                                                NSDictionary *thirdDictionary=[NSDictionary dictionaryWithObjectsAndKeys:tempThirdStr,@"title",thirdNum,@"link",nil];
                                                [cataLogTitles addObject:thirdDictionary];//    [secondLogTitles addObject:thirdDictionary]
                                            }
                                        }
                                        third++;
                                    }
                                    
                                } //1
                                
                            }//2
                            
                        }//3
                        j++;
                    }//4
                }//5
                
                //一级目录第二个往下的while循环
                
//                NSLog(@"目录对应的文章页数====%@===", num);
                CGPDFDictionaryRef tempDic;
                tempDic = myDic;
                int i = 0;
                while ( i < myCount )
                {
                    
                    if( CGPDFDictionaryGetDictionary( tempDic , "Next", &tempDic) )
                    {
                        CGPDFStringRef tempTitle;
                        //titile tempTitle
                        if( CGPDFDictionaryGetString(tempDic, "Title", &tempTitle) )
                        {
                            NSString *tempStr= (NSString *)CGPDFStringCopyTextString(tempTitle);
                            NSString *num = [self returnCatalogListNumber:tempDic PDFDoc:pdf];
                            NSDictionary *_MyDic = [NSDictionary dictionaryWithObjectsAndKeys:
                                                    tempStr, @"title",
                                                    num, @"link",
                                                    nil];
                            [tempStr release];
                            [cataLogTitles addObject:_MyDic];
//                            NSLog(@"catalog==================%@",cataLogTitles);

                            //第二层目录
                            long int SecondCount;
                            if (CGPDFDictionaryGetInteger(tempDic, "Count", &SecondCount))
                            {
                                NSLog(@"------目录债市nName:%ld", SecondCount);
                                if (SecondCount <0)
                                {
                                    SecondCount = SecondCount *(-1);
                                }
                            }
                            //第二层 走起
                            CGPDFDictionaryRef secondMunu;
                            if (CGPDFDictionaryGetDictionary(tempDic, "First", &secondMunu))
                            {
//                            ----------
                                //目录标题
                                CGPDFStringRef tempSecondTitle;
                                if( CGPDFDictionaryGetString(secondMunu, "Title", &tempSecondTitle) )
                                {
                                    NSString *tempSecondStr1 = (NSString *)CGPDFStringCopyTextString(tempSecondTitle);
                                    NSString *tempSecondStr = [NSString stringWithFormat:@"  %@",tempSecondStr1];
                                    NSString *secondNum = [self returnCatalogListNumber:secondMunu PDFDoc:pdf];
                                    NSDictionary *secondDiction=[NSDictionary dictionaryWithObjectsAndKeys:tempSecondStr,@"title",secondNum,@"link",nil];
                                    [cataLogTitles addObject:secondDiction];
                                 //  NSLog(@"catal………………………………………………………………………………………………………………………………%@",cataLogTitles);
                                    //第二层目录的首个判断是否有三级目录
                                    //第三层目录
                                    long int ThirdCount;
                                    if (CGPDFDictionaryGetInteger(secondMunu, "Count", &ThirdCount))
                                    {
                                        if (ThirdCount<0)
                                        {
                                            ThirdCount = ThirdCount *(-1);
                                        }
                                    }  
                                    //////////////////////////
                                    CGPDFDictionaryRef thirdMunu;
                                    if (CGPDFDictionaryGetDictionary(secondMunu, "First", &thirdMunu))
                                    {
                                        //目录标题
                                        CGPDFStringRef tempThirdTitle;
                                        if( CGPDFDictionaryGetString(thirdMunu, "Title", &tempThirdTitle) )
                                        {
                                            NSString *tempThirdStr1= (NSString *)CGPDFStringCopyTextString(tempThirdTitle);
                                            NSString *tempThirdStr = [NSString stringWithFormat:@"      %@",tempThirdStr1];
                                            NSString *thirdNum = [self returnCatalogListNumber:thirdMunu PDFDoc:pdf];
                                            NSDictionary *thirdDiction=[NSDictionary dictionaryWithObjectsAndKeys:tempThirdStr,@"title",thirdNum,@"link",nil];
                                            [cataLogTitles addObject:thirdDiction];
                                        }
                                        CGPDFDictionaryRef thirdDic;
                                        thirdDic=thirdMunu;
                                        int third=0;
                                        while (third<ThirdCount)
                                        {
                                            if (CGPDFDictionaryGetDictionary(thirdDic, "Next", &thirdDic))
                                            {
                                                CGPDFStringRef thirdTempTitle;
                                                if (CGPDFDictionaryGetString(thirdDic, "Title", &thirdTempTitle))
                                                {
                                                    NSString *tempThirdStr1=(NSString *)CGPDFStringCopyTextString(thirdTempTitle);
                                                    NSString *tempThirdStr=[NSString stringWithFormat:@"      %@",tempThirdStr1];
                                                    NSLog(@"第san层目录的titile=======……%@",tempThirdStr);
                                                    NSString *thirdNum = [self returnCatalogListNumber:thirdDic PDFDoc:pdf];
                                                    NSLog(@"thirddeNum=========%@",thirdNum);
                                                    NSDictionary *thirdDictionary=[NSDictionary dictionaryWithObjectsAndKeys:tempThirdStr,@"title",thirdNum,@"link",nil];
                                                    [cataLogTitles addObject:thirdDictionary];
                                                }
                                            }
                                            third++;   
                                        }   
                                    }
                                    /////////////////////////////////////////
                                }
//                              ------------
                                //第二层目录第二个以后的while循环
                                CGPDFDictionaryRef secondDic;
                                secondDic=secondMunu;
                                int j=0;
                                while ( j<SecondCount)
                                {
                                    if (CGPDFDictionaryGetDictionary(secondDic, "Next", &secondDic))
                                    {
//                                        -------第三
                                        CGPDFStringRef secondTempTitle;
                                        if (CGPDFDictionaryGetString(secondDic, "Title", &secondTempTitle))
                                        {
                                            NSString *tempSecondStr1=(NSString *)CGPDFStringCopyTextString(secondTempTitle);
                                      
                                            NSString *tempSecondStr = [NSString stringWithFormat:@"  %@",tempSecondStr1];
                                            NSLog(@"第二层目录的titile=======……%@",tempSecondStr);
                                            [tempSecondStr1 release];
                                            NSString *secondNum = [self returnCatalogListNumber:secondDic PDFDoc:pdf];
                                            NSLog(@"secondeNum===000000======%@",secondNum);
                                            NSDictionary *secondDictionary=[NSDictionary dictionaryWithObjectsAndKeys:tempSecondStr,@"title",secondNum,@"link",nil];
                                            [cataLogTitles addObject:secondDictionary];
//                                            NSLog(@"catalog、**************%@",cataLogTitles);
                                            //第三层目录
                                            long int ThirdCount;
                                            if (CGPDFDictionaryGetInteger(secondDic, "Count", &ThirdCount))
                                            {
                                                NSLog(@"---------第二层目市me:%ld", ThirdCount);
                                                if (ThirdCount<0)
                                                {
                                                    ThirdCount = ThirdCount *(-1);
                                                }
                                            }
                                            /////
                                            CGPDFDictionaryRef thirdMunu;
                                            if (CGPDFDictionaryGetDictionary(secondDic, "First", &thirdMunu))
                                            {
                                                //目录标题
                                                CGPDFStringRef tempThirdTitle;
                                                if( CGPDFDictionaryGetString(thirdMunu, "Title", &tempThirdTitle) )
                                                {
                                                    NSString *tempThirdStr1 = (NSString *)CGPDFStringCopyTextString(tempThirdTitle);
                                                    NSString *tempThirdStr=[NSString stringWithFormat:@"      %@",tempThirdStr1];
                                                    [tempThirdStr1 release];
                                                    NSString *thirdNum = [self returnCatalogListNumber:thirdMunu PDFDoc:pdf];
                                                    NSDictionary *thirdDiction=[NSDictionary dictionaryWithObjectsAndKeys:tempThirdStr,@"title",thirdNum,@"link",nil];
                                                    [cataLogTitles addObject:thirdDiction];      
                                                }
                                                CGPDFDictionaryRef thirdDic;
                                                thirdDic=thirdMunu;
                                                int third=0;
                                                while (third<ThirdCount)
                                                {
                                                    if (CGPDFDictionaryGetDictionary(thirdDic, "Next", &thirdDic))
                                                    {
                                                        CGPDFStringRef thirdTempTitle;
                                                        if (CGPDFDictionaryGetString(thirdDic, "Title", &thirdTempTitle))
                                                        {
                                                            NSString *tempThirdStr1=(NSString *)CGPDFStringCopyTextString(thirdTempTitle);
                                                            NSString *tempThirdStr=[NSString stringWithFormat:@"      %@",tempThirdStr1];
                                                            [tempThirdStr1 release];
                                                            NSString *thirdNum = [self returnCatalogListNumber:thirdDic PDFDoc:pdf];
                                                            NSDictionary *thirdDictionary=[NSDictionary dictionaryWithObjectsAndKeys:tempThirdStr,@"title",thirdNum,@"link",nil];
                                                            [cataLogTitles addObject:thirdDictionary];
                                                        }
                                                    }
                                                    third++;
                                                }  
                                            }
                                            //////
                                        }
//                                        -------第三
                                    }// while 下的第一个if
                                    j++;
                                }
                                // while寻韩    
                            }
                              //第二 层 走
                        }//tempTitle
                         
                    }
                    
                    i++;
                }//WHILE循环
                  
            }// 章节的名字
          
        }

    }//目录下的名字字典K
    
//    [pdfMuluArray addObjectsFromArray:cataLogTitles];
//    [pdfMuluArray addObject:cataLogTitles];

}
//*****//
-(NSString *)returnCatalogListNumber:(CGPDFDictionaryRef)tempCGPDFDic PDFDoc:(CGPDFDocumentRef)tempCGPDFDoc
{
    NSLog(@"进入目录对应页数的方法");

        CGPDFArrayRef destArray;
        if( CGPDFDictionaryGetArray(tempCGPDFDic,"Dest",&destArray) )//"D"
        {
//            NSLog(@"123");
            NSInteger targetPageNumber = 0; // The target page number
            
            CGPDFDictionaryRef pageDictionaryFromDestArray = NULL; // Target reference
          
            if (CGPDFArrayGetDictionary(destArray, 0, &pageDictionaryFromDestArray) == true)
            {
//                NSLog(@"jinlai la ");
            }
            else // Try page number from array possibility
            {
//                NSLog(@"你别玩我了啊啊 大哥");
                CGPDFInteger pageNumber = 0; // Page number in array
                
                if (CGPDFArrayGetInteger(destArray, 0, &pageNumber) == true)
                {
                    targetPageNumber = (pageNumber + 1); // 1-based
//                    NSLog(@"还玩我啊 ");
                }
            }
            
            if (targetPageNumber > 0) // We have a target page number
            {
//                NSLog(@"12121212121212121212121212==%d",targetPageNumber);
                return [NSString stringWithFormat:@"%d", targetPageNumber];
            }
            
        }   
   
    else
    {
        NSLog(@"为啥呢");
    }
    return @"0";
}

抱歉!评论已关闭.