2011-06-16 69 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

相關問題