title: Can anyone please help me in reading and writing annotation in the PDF using IOS
body: <p>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. </p>
  
  <pre><code>CGPDFDictionaryRef aDict;
          if(!CGPDFDictionaryGetDictionary(annotDict, "A", &amp;aDict)) 
  </code></pre>
  
  <p>The code that I've tried is as follows:</p>
  
  <pre><code>CGPDFPageRef pageAd = CGPDFDocumentGetPage(pdf, index+1);
  
  CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(pageAd);
  
  CGPDFArrayRef outputArray;
  if(!CGPDFDictionaryGetArray(pageDictionary, "Annots", &amp;outputArray)) {
      return;
  }
  
  int arrayCount = CGPDFArrayGetCount( outputArray );
  
  
  for( int j = 0; j &lt; arrayCount; ++j ) {
      CGPDFObjectRef aDictObj;
      if(!CGPDFArrayGetObject(outputArray, j, &amp;aDictObj)) {
          return;
      }
  
      CGPDFDictionaryRef annotDict;
      if(!CGPDFObjectGetValue(aDictObj, kCGPDFObjectTypeDictionary, &amp;annotDict)) {
          return;
      }
  
      CGPDFDictionaryRef aDict;
      if(!CGPDFDictionaryGetDictionary(annotDict, "A", &amp;aDict)) {
          return;
      }
  
      CGPDFStringRef uriStringRef;
      if(!CGPDFDictionaryGetString(aDict, "URI", &amp;uriStringRef)) {
          return;
      }
  
      CGPDFArrayRef rectArray;
      if(!CGPDFDictionaryGetArray(annotDict, "Rect", &amp;rectArray)) {
          return;
      }
  
      int arrayCount = CGPDFArrayGetCount( rectArray );
      CGPDFReal coords[4];
      for( int k = 0; k &lt; arrayCount; ++k ) {
          CGPDFObjectRef rectObj;
          if(!CGPDFArrayGetObject(rectArray, k, &amp;rectObj)) {
              return;
          }
  
          CGPDFReal coord;
          if(!CGPDFObjectGetValue(rectObj, kCGPDFObjectTypeReal, &amp;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", &amp;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];
  </code></pre>