首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有人可以帮我用IOS在PDF中读写注解

有没有人可以帮我用IOS在PDF中读写注解
EN

Stack Overflow用户
提问于 2011-04-19 19:57:19
回答 1查看 1.8K关注 0票数 0

有没有人能帮我用iOS读写注解?这是我尝试过的示例代码。有没有人能告诉我我哪里错了?我可以加载Annotation Array,但当我尝试获取A的字典(如下所示)时,我无法解析它。

代码语言:javascript
复制
CGPDFDictionaryRef aDict;
        if(!CGPDFDictionaryGetDictionary(annotDict, "A", &aDict)) 

我尝试过的代码如下:

代码语言:javascript
复制
CGPDFPageRef pageAd = CGPDFDocumentGetPage(pdf, index+1);

CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(pageAd);

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

int arrayCount = CGPDFArrayGetCount( outputArray );


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);
    UIButton *button = [[UIButton alloc] initWithFrame:rect];
    [button setTitle:@"LINK" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(openLink:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-20 00:19:18

A键在注释字典中是可选的。如果批注是指向另一个页面的链接,则可以使用Dest键。或者,它可以使用附加操作字典中的U键。如果无法检索A字典,请尝试加载AA密钥。如果它不存在,则注释没有操作。如果存在,则从AA字典中加载U密钥。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5715971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档