2011-08-10 31 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