如何在iphone/ipad中访问PDF文档中的超链接



如何在iPhone和iPad上访问PDF文档中的超链接?

我想在电子书的内容上设置链接。

使用下面的函数将ui按钮放在PDF中的链接上。这会在你的UIView上绘制按钮。您必须将页面作为参数传递。目前它将绘制类型为UIButtonTypeRoundedRect的按钮,之后你将其更改为custom,因此它将被隐藏。最初,按钮可能绘制在链接以外的地方,因此您需要根据需要调整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];
    }
}

最新更新