Get PDF hyperlinks on iOS with Quartz

IphoneIpadPdfQuartz Graphics

Iphone Problem Overview


I've spent all day trying to get hyperlinks metadata from PDFs in my iPad application. The CGPDF* APIs are a true nightmare, and the only piece of information I've found on the net about all this is that I have to look for an "Annots" dictionary, but I just can't find it in my PDFs.

I even used the old https://github.com/below/PDF-Voyeur/network">Voyeur Xcode sample to inspect my test PDF file, but no trace of this "Annots" dictionary...

You know, this is a feature I see on every PDF reader - this same question has https://stackoverflow.com/questions/93297/newbie-wants-to-create-a-pdf-reader-for-ipod-touch-whats-the-best-approach">been</a> https://stackoverflow.com/questions/3257057/iphone-cgpdfdocument-pdf-links">asked</a> https://stackoverflow.com/questions/2609602/pdf-hyperlinks-on-iphone-ipad">multiple</a> https://stackoverflow.com/questions/1247792/how-to-access-hyperlinks-in-pdf-documents-iphone">times</a> here with no real practical answers. I usually never ask for sample code directly but apparently this time I really need it... anyone got this working, possibly with sample code?

Update: I just realized the guy who has done my testing PDF had just inserted an URL as text, and not a real annotation. He tried putting an annotation and my code works now... But that's not what I need, so it seems I'll have to analyze text and search for URLs. But that's another story...

Update 2: So I finally came up with some working code. I'm posting it here so hopefully it'll help someone. It assumes the PDF document actually contains annotations.

for(int i=0; i<pageCount; i++) {
    CGPDFPageRef page = CGPDFDocumentGetPage(doc, i+1);
	
    CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(page);
	
    CGPDFArrayRef outputArray;
    if(!CGPDFDictionaryGetArray(pageDictionary, "Annots", &outputArray)) {
        return;
    }

    int arrayCount = CGPDFArrayGetCount( outputArray );
    if(!arrayCount) {
        continue;
    }

    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
    }
}

Iphone Solutions


Solution 1 - Iphone

heres the basic idea to get to the annots CGPDFDictionary for each page atleast. after that you should be able to figure it out with help from the PDF spec from Adobe.

1.) get the CGPDFDocumentRef.

2.) get each page.

3.) on each page, use CGPDFDictionaryGetArray(pageDictionary, "Annots", &outputArray) where pageDictionary is the CGPDFDictionary representing the CGPDFPage, and outputArray is the variable (CGPDFArrayRef) to store the Annots array of that page in.

Solution 2 - Iphone

Great code but I am having a little trouble working it into my project. It gets all the URL's correctly but when I click on it nothing happens. Here is my code I had to modify yours slightly to work with my project). Is there something missing:

- (void) renderPageAtIndex:(NSUInteger)index inContext:(CGContextRef)ctx {
//CGPDFPageRef page = CGPDFDocumentGetPage(pdf, index+1);

CGPDFPageRef page = CGPDFDocumentGetPage(pdf, index+1);
CGAffineTransform transform1 = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFMediaBox),
										 CGContextGetClipBoundingBox(ctx));
CGContextConcatCTM(ctx, transform1);
CGContextDrawPDFPage(ctx, page);

int pageCount = CGPDFDocumentGetNumberOfPages(pdf);
int i = 0;
while (i<pageCount) {
	i++;
	CGPDFPageRef page = CGPDFDocumentGetPage(pdf, i+1);
	
	CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(page);
	
	CGPDFArrayRef outputArray;
	if(!CGPDFDictionaryGetArray(pageDictionary, "Annots", &outputArray)) {
		return;
	}
	
	int arrayCount = CGPDFArrayGetCount( outputArray );
	if(!arrayCount) {
		continue;
	}
	
	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);
       // CFRelease(url);
		}
	}	
	
	
}

Thanks & great work BrainFeeder!

UPDATE:

For anybody using the leaves project in your app this is how I got the PDF links to work (it's not perfect as the rect seems to fill the entire screen but it's a start):

- (void) renderPageAtIndex:(NSUInteger)index inContext:(CGContextRef)ctx {

CGPDFPageRef page = CGPDFDocumentGetPage(pdf, index+1);
CGAffineTransform transform1 = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFMediaBox),
										 CGContextGetClipBoundingBox(ctx));
CGContextConcatCTM(ctx, transform1);
CGContextDrawPDFPage(ctx, page);


	CGPDFPageRef pageAd = CGPDFDocumentGetPage(pdf, index);

	CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(pageAd);
	
	CGPDFArrayRef outputArray;
	if(!CGPDFDictionaryGetArray(pageDictionary, "Annots", &outputArray)) {
		return;
	}
	
	int arrayCount = CGPDFArrayGetCount( outputArray );
	if(!arrayCount) {
		//continue;
	}
	
	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];
           // CFRelease(url);
		}
	//}	
	

Final Update Below is the final code I used in my apps.

- (void) renderPageAtIndex:(NSUInteger)index inContext:(CGContextRef)ctx {
//If the view already contains a button control remove it
if ([[self.view subviews] containsObject:button]) {
	[button removeFromSuperview];
}

CGPDFPageRef page = CGPDFDocumentGetPage(pdf, index+1);
CGAffineTransform transform1 = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFMediaBox),
										 CGContextGetClipBoundingBox(ctx));
CGContextConcatCTM(ctx, transform1);
CGContextDrawPDFPage(ctx, page);


CGPDFPageRef pageAd = CGPDFDocumentGetPage(pdf, index);

CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(pageAd);

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

int arrayCount = CGPDFArrayGetCount( outputArray );
if(!arrayCount) {
	//continue;
}

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, 35, pageRect.size.height+150);
	trans = CGAffineTransformScale(trans, 1.15, -1.15);
	
	rect = CGRectApplyAffineTransform(rect, trans);
	
	urlLink = [NSURL URLWithString:uri];
	[urlLink retain];
	
	//Create a button to get link actions
	button = [[UIButton alloc] initWithFrame:rect];
	[button setBackgroundImage:[UIImage imageNamed:@"link_bg.png"] forState:UIControlStateHighlighted];
	[button addTarget:self action:@selector(openLink:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:button];
}	
[leavesView reloadData];
}
	
}

Solution 3 - Iphone

I must be confused, because this all works if I use:

CGRect rect = CGRectMake(coords[0],coords[1],coords[2]-coords[0]+1,coords[3]-coords[1]+1);

Am I misusing something later, perhaps? PDF supplies the corners, and CGRect wants a corner and a size.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionySgPjxView Question on Stackoverflow
Solution 1 - IphoneJesse NaugherView Answer on Stackoverflow
Solution 2 - Iphoneuser470763View Answer on Stackoverflow
Solution 3 - IphoneBill CheswickView Answer on Stackoverflow