ios 标示pdf中URL字串。

有一需求要把pdf中的url字串标示出来。原理是解析pdf结构,类似 xml结构。查找“Annots”的key。代码如下。 

- (void) renderPageAtIndex:(NSUInteger)index inContext:(CGContextRef)ctx inPage:(CGPDFPageRef)page{
        
        //CGPDFPageRef page = CGPDFDocumentGetPage(pdf, i+1);
        CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(page);
        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( 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
            NSURL *url = [NSURL URLWithString:uri];
            NSLog(@"URL: %@", url);
            CGPDFContextSetURLForRect(ctx, (CFURLRef)url, rect);
            //CFRelease(url);
            CGContextSetFillColorWithColor(ctx, [[UIColor yellowColor] CGColor]);
            CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
            
            CGContextSaveGState(ctx);
            CGContextConcatCTM(ctx, trans);
            CGContextFillRect(ctx, rect);
            CGContextRestoreGState(ctx);
            
        }
    
}

效果如下

另外,代码只是处理了当前一页。可自行修改处理整个pdf的查找标示。参考:https://ask.helplib.com/iphone/post_562254

 

转载于:https://my.oschina.net/u/989459/blog/1928265

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值