2011-08-10 34 views

回答

0

使用以下功能將UIButton放在PDF鏈接上。這將在您的UIView上繪製按鈕。你必須傳遞你的頁面作爲參數。目前它將繪製帶有類型爲UIButtonTypeRoundedRect的按鈕,然後將其更改爲自定義,以便將其隱藏。最初按鈕可能會被繪製在除鏈接之外的其他位置,因此您需要根據您的要求調整x/y位置。

-(void)drawLinks:(CGPDFPageRef)thisPage 
{ 
    CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(thisPage); 

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

    int arrayCount = CGPDFArrayGetCount(outputArray); 
    if(!arrayCount) 
    { 
     //continue; 
    } 

    for(int j = 0; j < arrayCount; ++j) 
    { 
     CGPDFObjectRef aDictObj; 
     if(!CGPDFArrayGetObject(outputArray, j, &aDictObj)) 
     { 
      return; 
     } 

     CGPDFDictionaryRef annotDict; 
     if(!CGPDFObjectGetValue(aDictObj, kCGPDFObjectTypeDictionary, &annotDict)) { 
      return; 
     } 

     CGPDFDictionaryRef aDict; 
     if(!CGPDFDictionaryGetDictionary(annotDict, "A", &aDict)) { 
      return; 
     } 

     CGPDFStringRef uriStringRef; 
     if(!CGPDFDictionaryGetString(aDict, "URI", &uriStringRef)) { 
      return; 
     } 

     CGPDFArrayRef rectArray; 
     if(!CGPDFDictionaryGetArray(annotDict, "Rect", &rectArray)) { 
      return; 
     } 

     int arrayCount = CGPDFArrayGetCount(rectArray); 
     CGPDFReal coords[4]; 
     for(int k = 0; k < arrayCount; ++k) { 
      CGPDFObjectRef rectObj; 
      if(!CGPDFArrayGetObject(rectArray, k, &rectObj)) { 
       return; 
      } 

      CGPDFReal coord; 
      if(!CGPDFObjectGetValue(rectObj, kCGPDFObjectTypeReal, &coord)) { 
       return; 
      } 

      coords[k] = coord; 
     }    

     char *uriString = (char *)CGPDFStringGetBytePtr(uriStringRef); 

     NSString *uri = [NSString stringWithCString:uriString encoding:NSUTF8StringEncoding]; 
     CGRect rect = CGRectMake(coords[0],coords[1],coords[2],coords[3]); 

     CGPDFInteger pageRotate = 0; 
     CGPDFDictionaryGetInteger(pageDictionary, "Rotate", &pageRotate); 
     CGRect pageRect = CGRectIntegral(CGPDFPageGetBoxRect(thisPage, 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); 

     UIButton *btnTmp = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // After testing put here UIButtonTypeCustom 
     [btnTmp addTarget:self action:@selector(urlOpen:) forControlEvents:UIControlEventTouchUpInside]; 
     rect.origin.x = rect.origin.x + 78; //I have adjusted this as per my requirement 
     rect.origin.y = rect.origin.y + 108; // I have adjusted this as per my requirement 
     btnTmp.frame = rect; 
     btnTmp.titleLabel.text = uri; 
     [self.superview addSubview:btnTmp]; 
    } 
} 
+0

謝謝jennis,但我想在一個捲曲頁面pdf設置網站鏈接的鏈接,但這段代碼沒有在我的應用程序中運行。 – parag

+0

此代碼適用於任何pdf頁面。你遇到什麼問題? –

+0

感謝jennish的回覆.jennish你有例子然後發送給我...我必須在這個pdf文件上設置一個鏈接,並且我還可以在pdf中使用curl頁面效果。如果你有演示或示例代碼請給我鏈接。 – parag