0

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];
Diego
  • 16,830
  • 8
  • 34
  • 46
philip abraham
  • 587
  • 1
  • 5
  • 20

1 Answers1

0

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.

iPDFdev
  • 5,229
  • 2
  • 17
  • 18
  • Thanks once again. Im new to Objective C and PDF parsing. I am not very familiar with PDF structure. Can you please tell me how i can extract the Annotation Text from the PDF. Also can you please tell me where i can get the PDF structure so that i can extract the text and Images from the PDF. Thanks once again. – philip abraham Apr 20 '11 at 05:13
  • PDF format is described here: http://www.adobe.com/devnet/pdf/pdf_reference.html. Link annotations do not have text, they are simply rectangles placed on top of some page content so that text appears as a link. – iPDFdev Apr 20 '11 at 07:15
  • Thanks.I was able to solve it. This is what i had done to read the content of the Text annotation if(CGPDFDictionaryGetName(annotDict, "Subtype",&abc)){ NSString *subType = [NSString stringWithCString:abc length:strlen(abc)]; if([subType isEqualToString:@"Text"]) { //GETTING THE CONTENT CGPDFStringRef contentText; if(CGPDFDictionaryGetString(annotDict, "Contents", &contentText)) { NSString* data=(NSString *)CGPDFStringCopyTextString(contentText); NSLog(@"%@",data); } – philip abraham Apr 21 '11 at 11:09
  • hi do you have any idea on how i can write a text annotate to the existing PDF – philip abraham Apr 21 '11 at 11:54
  • @user532240 - it is not possible to create annotations with Quartz PDF API. You have to use a 3rd party PDF library. – iPDFdev Apr 21 '11 at 16:48
  • @iPDFdev Can u answer my Question - [link] (http://stackoverflow.com/questions/8122166/copy-table-of-contents-from-one-pdf-to-another) – DivineDesert Nov 25 '11 at 08:21