谁能帮我使用 IOS 在 PDF 中阅读和编写注释

发布于 2024-11-02 09:36:27 字数 3176 浏览 6 评论 0原文

谁能帮我使用 iOS 在 PDF 中阅读和编写注释?这是我尝试过的示例代码。谁能告诉我哪里出错了?我能够加载注释数组,但是当我尝试获取 A 的字典(下面一行)时,我无法解析它。

CGPDFDictionaryRef aDict;
        if(!CGPDFDictionaryGetDictionary(annotDict, "A", &aDict)) 

我尝试过的代码如下:

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];

Can anyone please help me with reading and writing annotations in a PDF using iOS? This is the sample code that I have tried. Can anyone please tell me where I am going wrong? I am able to load The Annotation Array, but when I try to get the dictionary for A (line below) I am not able to parse it.

CGPDFDictionaryRef aDict;
        if(!CGPDFDictionaryGetDictionary(annotDict, "A", &aDict)) 

The code that I've tried is as follows:

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初熏 2024-11-09 09:36:27

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

The A key is optional in an annotation dictionary. If the annotation is a link to another page, it might use the Dest key. Or it might use the U key in the additional actions dictionary. If the A dictionary cannot be retrieved, try loading the AA key. If it does not exist, the annotation does not have an action. If it exists, then load the U key from the AA dictionary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文