2011-06-16 103 views
2

我目前使用Get PDF hyperlinks on iOS with Quartz中的一些修改代码来搜索pdf中的注释,然后将按钮放在它们上方进行导航。目前它正在传递整个pdf,因为我只想传递当前页面并将注释放置在该页面上。我已经阅读了关于这个主题的文档,并且一直在玩这个游戏一天左右,但没有运气。在当前页面的iOS上使用Quartz获取PDF超链接

我敢肯定我错过了一些愚蠢的东西,所以任何帮助将不胜感激。

- (id)initWithFrame:(CGRect)frame { 

    self = [super initWithFrame:(CGRect)frame]; 
    if (self) { 
     self.backgroundColor = [UIColor clearColor]; 
     NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:@"testpdf" ofType:@"pdf"]; 
     NSURL *pdfUrl = [NSURL fileURLWithPath:pathToPdfDoc]; 
     document = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl); 
     currentPage = 1; 
    } 
    return self; 
} 

-(void)drawRect:(CGRect)inRect{ 
    if(document) { 
     CGPDFPageRef page = CGPDFDocumentGetPage(document, currentPage); 
     CGContextRef ctx = UIGraphicsGetCurrentContext(); 
     CGContextSaveGState(ctx); 
     CGContextTranslateCTM(ctx, 0.0, [self bounds].size.height); 
     CGContextScaleCTM(ctx, 1.0, -1.0); 
     CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, [self bounds], 0, true)); 
     CGContextDrawPDFPage(ctx, page);  
     CGContextRestoreGState(ctx); 
     int pageCount = CGPDFDocumentGetNumberOfPages (document); 

     for(i=0; i<=currentPage; i++) { 
      CGPDFPageRef page = CGPDFDocumentGetPage(document, i+1); 

      CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(page); 

      CGPDFArrayRef outputArray; 
      if(!CGPDFDictionaryGetArray(pageDictionary, "Annots", &outputArray)) { 
       break; 
      } 

      int arrayCount = CGPDFArrayGetCount(outputArray); 
      if(!arrayCount) { 
       continue; 
      } 
      for(int j = 0; j < arrayCount; ++j) { 
       CGPDFObjectRef aDictObj; 
       if(!CGPDFArrayGetObject(outputArray, j, &aDictObj)) { 
        break; 
       } 
       NSLog(@"poop2"); 
       CGPDFDictionaryRef annotDict; 
       if(!CGPDFObjectGetValue(aDictObj, kCGPDFObjectTypeDictionary, &annotDict)) { 
        break; 
       } 
       CGPDFDictionaryRef aDict; 
       if(!CGPDFDictionaryGetDictionary(annotDict, "A", &aDict)) { 
        break; 
       }     
       CGPDFStringRef uriStringRef; 
       if(!CGPDFDictionaryGetString(aDict, "URI", &uriStringRef)) { 
        break; 
       }     
       CGPDFArrayRef rectArray; 
       if(!CGPDFDictionaryGetArray(annotDict, "Rect", &rectArray)) { 
        break; 
       } 
       int arrayCount = CGPDFArrayGetCount(rectArray); 
       CGPDFReal coords[4]; 
       for(int k = 0; k < arrayCount; ++k) { 
        CGPDFObjectRef rectObj; 
        if(!CGPDFArrayGetObject(rectArray, k, &rectObj)) { 
         break; 
        } 
        CGPDFReal coord; 
        if(!CGPDFObjectGetValue(rectObj, kCGPDFObjectTypeReal, &coord)) { 
         break; 
        } 
        coords[k] = coord; 
       }   
       char *uriString = (char *)CGPDFStringGetBytePtr(uriStringRef); 
       NSString *uri = [NSString stringWithCString:uriString encoding:NSUTF8StringEncoding]; 
       if (950 <= coords[3] && coords[3] < 1000){      
        coords[3]= (coords[3]*1.01); 
        coords[1]= (coords[1]*1.01)-10; 
       } 
       else if (900 <= coords[3] && coords[3] < 949){      
        coords[3]= (coords[3]*1.02); 
        coords[1]= (coords[1]*1.02)-10; 
       } 
       else if (850 <= coords[3] && coords[3] < 899){      
        coords[3]= (coords[3]*1.03); 
        coords[1]= (coords[1]*1.03)-10; 
       }     
       else if (800 <= coords[3] && coords[3] < 849){      
        coords[3]= (coords[3]*1.035); 
        coords[1]= (coords[1]*1.035)-10; 
       }     
       else if (750 <= coords[3] && coords[3] < 799){      
        coords[3]= (coords[3]*1.04); 
        coords[1]= (coords[1]*1.04)-10; 
       } 
       else if (700 <= coords[3] && coords[3] < 749){      
        coords[3]= (coords[3]*1.05); 
        coords[1]= (coords[1]*1.05)-10; 
       } 
       else if (650 <= coords[3] && coords[3] < 699){      
        coords[3]= (coords[3]*1.06); 
        coords[1]= (coords[1]*1.06)-10; 
       } 
       else if (600 <= coords[3] && coords[3] < 649){      
        coords[3]= (coords[3]*1.075); 
        coords[1]= (coords[1]*1.075)-10; 
       }     
       else if (550 <= coords[3] && coords[3] < 599){      
        coords[3]= (coords[3]*1.09); 
        coords[1]= (coords[1]*1.09)-10; 
       } 
       else if (500 <= coords[3] && coords[3] < 549){      
        coords[3]= (coords[3]*1.1); 
        coords[1]= (coords[1]*1.1)-10; 
       } 
       else if (450 <= coords[3] && coords[3] < 499){      
        coords[3]= (coords[3]*1.13); 
        coords[1]= (coords[1]*1.13)-10; 
       } 
       else if (400 <= coords[3] && coords[3] < 449){      
        coords[3]= (coords[3]*1.15); 
        coords[1]= (coords[1]*1.15)-10; 
       } 
       else if (300 <= coords[3] && coords[3] < 399){      
        coords[3]= (coords[3]*1.18); 
        coords[1]= (coords[1]*1.18)-10; 
       } 
       else if (250 <= coords[3] && coords[3] < 299){ 
        coords[3]= (coords[3]*1.30); 
        coords[1]= (coords[1]*1.30)-10; 
       } 
       else if (200 <= coords[3] && coords[3] < 249){ 
        coords[3]= (coords[3]*1.4); 
        coords[1]= (coords[1]*1.4)-10; 
       } 
       else if (150 <= coords[3] && coords[3] < 199){ 
        coords[3]= (coords[3]*1.6); 
        coords[1]= (coords[1]*1.6)-10; 
       } 
       else if (100 <= coords[3] && coords[3] < 149){ 
        coords[3]= (coords[3]*1.8); 
        coords[1]= (coords[1]*1.8)-10; 
       } 
       else if (50 <= coords[3] && coords[3] < 99){ 
        coords[3]= (coords[3]*2); 
        coords[1]= (coords[1]*2)-10; 
       } 
       else if (0 <= coords[3] && coords[3] < 49){ 
        coords[3]= (coords[3]*2.5); 
        coords[1]= (coords[1]*2.5-10); 
       } 
       CGRect rect = CGRectMake(coords[0] /1.11, coords[1],coords[2],coords[3]); 
       CGPDFInteger pageRotate = 0; 
       CGPDFDictionaryGetInteger(pageDictionary, "Rotate", &pageRotate); 
       CGRect pageRect = CGRectIntegral(CGPDFPageGetBoxRect(page, kCGPDFMediaBox)); 
       if(pageRotate == 90 || pageRotate == 270) { 
        CGFloat temp = pageRect.size.width; 
        pageRect.size.width = pageRect.size.height; 
        pageRect.size.height = temp; 
       } 
       rect.size.width -= rect.origin.x; 
       rect.size.height -= rect.origin.y; 
       CGAffineTransform trans = CGAffineTransformIdentity; 
       trans = CGAffineTransformTranslate(trans, 0, pageRect.size.height); 
       trans = CGAffineTransformScale(trans, 1.0, -1.0); 
       rect = CGRectApplyAffineTransform(rect, trans); 
       // do whatever you need with the coordinates. 
       // e.g. you could create a button and put it on top of your page 
       // and use it to open the URL with UIApplication's openURL 
       url = [NSURL URLWithString:uri]; 
       CGPDFContextSetURLForRect(ctx, (CFURLRef)url, rect); 
       buttonArray = [[NSMutableArray alloc] init]; 
       for(NSInteger b = 0; b < CGPDFArrayGetCount(outputArray); b++){ 
        button = [[UIButton alloc] initWithFrame:rect]; 
        [button setTitle:@"" forState:UIControlStateNormal]; 
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 
        [button setBackgroundColor:[UIColor blueColor]]; 
        [button.titleLabel setBackgroundColor:[UIColor clearColor]]; 
        button.tag = currentPage; 
        [button addTarget:self action:@selector(openLink:) forControlEvents:UIControlEventTouchUpInside]; 
        [self addSubview:button]; 
        [buttonArray addObject:button]; 
       } 
       NSLog(@"%i", CGPDFArrayGetCount(buttonArray));     
      } 
     } 
    } 
} 

回答

2

所以这行:

for(i=0; i<=currentPage; i++) { 

是循环(迭代)对所有从第一页到当前页的页面。您应该删除循环(与关联}),然后该行更改:

CGPDFPageRef page = CGPDFDocumentGetPage(document, i+1); 

这个

CGPDFPageRef page = CGPDFDocumentGetPage(document, currentPage); 

您可以设置currentPage作为initWithFrame1。这将使您获得当前页面。

+0

这已经完成了。感谢您的帮助! – JapanDev 2011-06-19 03:07:48

相关问题