Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6080179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:01:55+00:00 2026-05-23T11:01:55+00:00

I have developed an application which displays PDF on ipad using CATiled Layer. So

  • 0

I have developed an application which displays PDF on ipad using CATiled Layer. So far, so good& But there is a problem which really makes me cut my last hairs. I have a PDF with embedded annotations. Each annotation has an URL. I can find the coordinates of touch area but the question is how I can find if there is an annotation under my finger and how to extract URL to open it in browser?

If anyone can share any thoughts about how this can be done, I will really appreciate your help!

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:01:56+00:00Added an answer on May 23, 2026 at 11:01 am

    Getting the annotations isn’t very difficult with CGPDF although it required a little fiddling around for me at first.

    //Get the current page ref 
    CGPDFPageRef currentPdfPage = CGPDFDocumentGetPage(pdfDocumentRef, page);
    
    //Get the page dictionary
    CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(currentPdfPage);
    CGPDFArrayRef annotsArray;
    
    //Get the Annots array
    if(!CGPDFDictionaryGetArray(pageDictionary, "Annots", &annotsArray)) {
        //DLog(@"No Annots found for page %d", page);
        [self updateProgress];
        return;
    }
    
    int annotsArrayCount = CGPDFArrayGetCount(annotsArray);
    //DLog(@"%d annots found for page %d in file %@", annotsArrayCount, page, _fileName);
    NSMutableArray* touchRectsArray = [[NSMutableArray alloc] initWithCapacity:annotsArrayCount];
    for (int j=annotsArrayCount; j >= 0; j--) {
        int destPageNumber = 0;
        NSString* uri = nil;
    
        //DLog(@"%d/%d", j+1, annotsArrayCount);
    
        CGPDFObjectRef aDictObj;
        if(!CGPDFArrayGetObject(annotsArray, j, &aDictObj)) {
            //DLog(@"%@", @"can't get dictionary object");
            continue;
        }
    
        CGPDFDictionaryRef annotDict;
        if(!CGPDFObjectGetValue(aDictObj, kCGPDFObjectTypeDictionary, &annotDict)) {
            //DLog(@"%@", @"can't get annotDict");
            continue;
        }
    
        //------------
        CGPDFDictionaryRef aDict;
        CGPDFArrayRef destArray;
        if(CGPDFDictionaryGetDictionary(annotDict, "A", &aDict)) {
            CGPDFStringRef uriStringRef;
            if(CGPDFDictionaryGetString(aDict, "URI", &uriStringRef)) {
                char* uriString = (char *)CGPDFStringGetBytePtr(uriStringRef);
                uri = [NSString stringWithCString:uriString encoding:NSUTF8StringEncoding];
            }
        } else {
            continue;
        }
    

    This will get you the URLs.
    Getting the rects:

    CGPDFArrayRef rectArray;
        if(!CGPDFDictionaryGetArray(annotDict, "Rect", &rectArray)) {
            DLog(@"%@", @"can't get Rect");
        }
    
        int arrayCount = CGPDFArrayGetCount(rectArray);
        CGPDFReal coords[4];
        for (int k = 0; k < arrayCount; k++) {
            CGPDFObjectRef rectObj;
            if(!CGPDFArrayGetObject(rectArray, k, &rectObj)) {
                DLog(@"%@", @"can't get rect data");
            }
    
            CGPDFReal coord;
            if(!CGPDFObjectGetValue(rectObj, kCGPDFObjectTypeReal, &coord)) {
                DLog(@"%@", @"can't get coords");
            }
    
            coords[k] = coord;
        }
    
        CGRect drawRect = [[SharedConfig valueForKey:@"screenSize"] CGRectValue];
        BOOL drawBoxIsLandscape = NO;
        if (1 < drawRect.size.width/drawRect.size.height) {
            drawBoxIsLandscape = YES;
        }
    
        CGRect pageRect = CGRectIntegral(CGPDFPageGetBoxRect(currentPdfPage, kCGPDFMediaBox));
        landscape = NO;
        if (1 < pageRect.size.width/pageRect.size.height) {
            landscape = YES;
        }
    
        float ratio = 0.0;
    
        //Get the rect of the clickable area
        //CGRect coordsRect = CGRectMake(coords[0], coords[1], coords[2], coords[3]);
        //Transform to new coordinate system
        CGRect originalRect = CGRectMake(coords[0], (pageRect.size.height-(coords[3]-coords[1]))-coords[1], coords[2]-coords[0], coords[3]-coords[1]);
    
        CGPDFInteger pageRotate = 0;
        CGPDFDictionaryGetInteger(pageDictionary, "Rotate", &pageRotate);
    
        if (pageRotate == 90 || pageRotate == 270) {
            CGFloat temp = pageRect.size.width;
            pageRect.size.width = pageRect.size.height;
            pageRect.size.height = temp;
            ratio = drawRect.size.height / pageRect.size.height;
        }       
    
        if (drawBoxIsLandscape) {
            ratio = landscape ? (drawRect.size.height/pageRect.size.height) : (drawRect.size.height/pageRect.size.width);
    
            if (landscape && drawRect.size.width < pageRect.size.width*ratio) {
                ratio = drawRect.size.width/pageRect.size.width;
            } else if (!landscape && drawRect.size.height < pageRect.size.width*ratio) {
                ratio = drawRect.size.height/pageRect.size.width;
            }
        } else {
            ratio = landscape ? (drawRect.size.height/pageRect.size.width) : (drawRect.size.height/pageRect.size.height);
    
            if (landscape && drawRect.size.width < pageRect.size.height*ratio) {
                ratio = drawRect.size.width/pageRect.size.height;
            } else if (!landscape && drawRect.size.height < pageRect.size.height*ratio) {
                ratio = drawRect.size.height/pageRect.size.height;
            }
        }
    
        CGRect calculatedRect = CGRectMake(originalRect.origin.x*ratio, originalRect.origin.y*ratio, originalRect.size.width*ratio, originalRect.size.height*ratio);
    
        if ((landscape && !drawBoxIsLandscape) || (!landscape && drawBoxIsLandscape)) {
            CGFloat width = calculatedRect.size.width;
            calculatedRect.size.width = calculatedRect.size.height;
            calculatedRect.size.height = width;
    
            CGFloat yModifier = drawRect.size.height-(pageRect.size.width*ratio);
    
            CGFloat x = calculatedRect.origin.x;
            calculatedRect.origin.x = calculatedRect.origin.y;
            calculatedRect.origin.y = drawRect.size.height-(x+calculatedRect.size.height)-yModifier;
    
        }
    
        if (nil != uri) {
            [touchRectsArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithCGRect:calculatedRect], @"rect", uri, @"targetUrl", nil]];
        }
    

    As you can see this bit of code first gets the rectangle of the annotation, transforms it to the device coordinate system, then does some recalculation, sizing and repositioning, based on page to screen ratio, rotation factor, etc. At the end you will have an array of the touch-active areas for that page. For handling the touches the following simple solution can be used:

    - (void) tapGesture:(UIGestureRecognizer*)sender
    {
    if (UIGestureRecognizerStateEnded == sender.state) {
        CGPoint touchPoint = [sender locationInView:self.view];
    
        if (nil != self.touchRects) for (int i=0; i<[self.touchRects count]; i++) {
            if (CGRectContainsPoint([[[self.touchRects objectAtIndex:i] objectForKey:@"rect"] CGRectValue], touchPoint)) {
                if ([[self.touchRects objectAtIndex:i] objectForKey:@"targetUrl"]) {
                    NSString* targetUrl = [[self.touchRects objectAtIndex:i] objectForKey:@"targetUrl"];
                    DLog(@"Hit found for target url: %@", targetUrl);
                    NSURL* url = [NSURL URLWithString:targetUrl];
                    [[UIApplication sharedApplication] openURL:url];
                }               return;
            }       
        }
        DLog(@"No hit found for touch at %@", NSStringFromCGPoint(touchPoint));
    }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a Win32 application using C/C++, which runs on Vista and XP.
I have an application which was developed under Windows, but for gcc. The code
I have developed a standalone application using java, in which I am executing an
I have developed an application which displays Hebrew strings that are retrived from a
I have developed one application which used to send/receive mails using Exchange Server 2003
I have developed the application in which i want to set the background color
I have developed a simple location aware iPhone application which is functionally working very
Hai I have developed a standalone application in which when an user logs in,
I am developing a web application which has Chart Controls. I have developed a
I am new to iPhone world. I have developed an application, which I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.