2011-08-10 8 views

答えて

0

次の関数を使用して、UIButtonsを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でウェブサイトのURLにリンクを設定したいが、このコードは自分のアプリケーションでは実行されていません。 – parag

+0

このコードは任意のpdfページで動作します。あなたはどんな問題に取り組んでいますか? –

+0

お返事ありがとうございます。jennishあなたは私にメールを送っています。私はこのpdfファイルのリンクを設定しなければなりません。また、pdfのカールページエフェクトも使用します。デモやサンプルコードを持っているなら、私にリンクを送ってください。 – parag

関連する問題