我正在尝试查找PDF文档中页面上的所有超链接,并在其上放置UIButton。
我找到并玩过下面的脚本应该可以做到。它找到UIButtons的超链接和位置,但它们不在链接之上。谁能告诉我为什么会这样?
我附上了一张图片,以显示按钮是如何关闭的。最上面的按钮应位于蓝色“METROPOLITAN”文本的顶部,最下面的按钮应位于文本“SÆSONEN2008-2009”的顶部。
这是我用来查找链接和放置按钮的代码。
NSMutableArray *rectArrays;
NSMutableArray *storeURLs;
CGPDFPageRef pdfPage = CGPDFDocumentGetPage(_pdf, pageNumber);
CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(pdfPage);
CGPDFArrayRef outputArray;
if(!CGPDFDictionaryGetArray(pageDictionary, "Annots", &outputArray)) {
//break;
}
int arrayCount = 0;
arrayCount = CGPDFArrayGetCount(outputArray);
if(arrayCount > 0) {
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]);
NSLog(@"Found: %f ; %f => %f x %f", coords[0], coords[1], coords[2], coords[3]);
CGPDFInteger pageRotate = 0;
CGPDFDictionaryGetInteger(pageDictionary, "Rotate", &pageRotate);
CGRect pageRect = CGRectIntegral( CGPDFPageGetBoxRect(pdfPage, 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.size.width - rect.origin.x;
rect.size.height = 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);
NSURL *url = [NSURL URLWithString:uri];
[rectArrays addObject:[NSValue valueWithCGRect:rect]];
[storeURLs addObject:url];
for(int i = 0; i <= [rectArrays count]; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:rect];
[button setTitle:@"Link" forState:UIControlStateNormal];
[[self view] addSubview:button];
NSLog(@"Added: %f ; %f => %f x %f", button.frame.origin.x, button.frame.origin.y, button.frame.size.width, button.frame.size.height);
}
}
}
编辑:尝试调整比例因子但不起作用。 UIButton仍然存在错误。
CGAffineTransform trans = CGAffineTransformIdentity;
trans = CGAffineTransformTranslate(trans, 0, pageRect.size.height * 1.5);
trans = CGAffineTransformScale(trans, 1.5, -1.5);
编辑我看了一下iPDFDev链接的源代码,似乎我现在已经有了定位工作。我的按钮现在太长了。有谁知道为什么会这样?如果我从viewRext.origin.x
中减去viewRect.size.width
,则按钮太短。
这是我现在的代码。
CGPDFPageRef pdfPage = CGPDFDocumentGetPage(_pdf, pageNumber);
CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(pdfPage);
CGPDFArrayRef outputArray;
if(!CGPDFDictionaryGetArray(pageDictionary, "Annots", &outputArray)) {
//break;
}
int arrayCount = 0;
arrayCount = CGPDFArrayGetCount(outputArray);
if(arrayCount > 0) {
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]);
CGPoint pdfPoint = CGPointMake(rect.origin.x, rect.origin.y);
CGPoint viewPoint = CGPointMake(0, 0);
CGRect cropBox = CGPDFPageGetBoxRect(pdfPage, kCGPDFCropBox);
int rotation = CGPDFPageGetRotationAngle(pdfPage);
CGRect pageRenderRect;
switch (rotation) {
case 90:
case -270:
pageRenderRect = CGRectMake(0, 0, 1024, 768);
viewPoint.x = pageRenderRect.size.width * (pdfPoint.y - cropBox.origin.y) / cropBox.size.height;
viewPoint.y = pageRenderRect.size.height * (pdfPoint.x - cropBox.origin.x) / cropBox.size.width;
break;
case 180:
case -180:
pageRenderRect = CGRectMake(0, 0, 768, 1024);
viewPoint.x = pageRenderRect.size.width * (cropBox.size.width - (pdfPoint.x - cropBox.origin.x)) / cropBox.size.width;
viewPoint.y = pageRenderRect.size.height * (pdfPoint.y - cropBox.origin.y) / cropBox.size.height;
break;
case -90:
case 270:
pageRenderRect = CGRectMake(0, 0, 1024, 768);
viewPoint.x = pageRenderRect.size.width * (cropBox.size.height - (pdfPoint.y - cropBox.origin.y)) / cropBox.size.height;
viewPoint.y = pageRenderRect.size.height * (cropBox.size.width - (pdfPoint.x - cropBox.origin.x)) / cropBox.size.width;
break;
case 0:
default:
pageRenderRect = CGRectMake(0, 0, 768, 1024);
viewPoint.x = pageRenderRect.size.width * (pdfPoint.x - cropBox.origin.x) / cropBox.size.width;
viewPoint.y = pageRenderRect.size.height * (cropBox.size.height - pdfPoint.y) / cropBox.size.height;
break;
}
viewPoint.x = viewPoint.x + pageRenderRect.origin.x;
viewPoint.y = viewPoint.y + pageRenderRect.origin.y;
CGRect viewRect = CGRectMake(viewPoint.x, viewPoint.y, rect.size.width, rect.size.height - rect.origin.y);
viewRect = CGRectMake(viewRect.origin.x, viewRect.origin.y - viewRect.size.height, viewRect.size.width, viewRect.size.height);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:viewRect];
[button setBackgroundColor:[UIColor greenColor]];
[button setAlpha:0.65];
[button setTag:kPDFLinkButton];
[[self view] addSubview:button];
}
}
编辑:这是我的最终代码。有关详细信息,请参阅iPDFDev的答案。
- (void)getLinksFromPDF:(CGPDFDocumentRef)_pdf withPageNumber:(NSUInteger)pageNumber
{
CGPDFPageRef pdfPage = CGPDFDocumentGetPage(_pdf, pageNumber);
CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(pdfPage);
CGPDFArrayRef outputArray;
if(!CGPDFDictionaryGetArray(pageDictionary, "Annots", &outputArray)) {
//break;
}
int arrayCount = 0;
arrayCount = CGPDFArrayGetCount(outputArray);
if(arrayCount > 0) {
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];
CGPoint lowerLeft = [self convertPDFPointToViewPoint:CGPointMake(coords[0], coords[1])];
CGPoint upperRight = [self convertPDFPointToViewPoint:CGPointMake(coords[2], coords[3])];
// This is the rectangle positioned under the link
CGRect viewRect = CGRectMake(lowerLeft.x, lowerLeft.y, upperRight.x - lowerLeft.x, lowerLeft.y - upperRight.y);
// Now adjusting the rectangle to be on top of the link
viewRect = CGRectMake(viewRect.origin.x, viewRect.origin.y - viewRect.size.height, viewRect.size.width, viewRect.size.height);
NSLog(@"%@", uri);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:viewRect];
[button setBackgroundColor:[UIColor greenColor]];
[button setAlpha:0.65];
[button setTag:kPDFLinkButton];
[[self view] addSubview:button];
}
}
}
- (CGPoint)convertPDFPointToViewPoint:(CGPoint)pdfPoint
{
CGPoint viewPoint = CGPointMake(0, 0);
CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdf, currentPage);
CGRect cropBox = CGPDFPageGetBoxRect(pdfPage, kCGPDFCropBox);
int rotation = CGPDFPageGetRotationAngle(pdfPage);
CGRect pageRenderRect;
switch (rotation) {
case 90:
case -270:
pageRenderRect = CGRectMake(0, 0, 1024, 768);
viewPoint.x = pageRenderRect.size.width * (pdfPoint.y - cropBox.origin.y) / cropBox.size.height;
viewPoint.y = pageRenderRect.size.height * (pdfPoint.x - cropBox.origin.x) / cropBox.size.width;
break;
case 180:
case -180:
pageRenderRect = CGRectMake(0, 0, 768, 1024);
viewPoint.x = pageRenderRect.size.width * (cropBox.size.width - (pdfPoint.x - cropBox.origin.x)) / cropBox.size.width;
viewPoint.y = pageRenderRect.size.height * (pdfPoint.y - cropBox.origin.y) / cropBox.size.height;
break;
case -90:
case 270:
pageRenderRect = CGRectMake(0, 0, 1024, 768);
viewPoint.x = pageRenderRect.size.width * (cropBox.size.height - (pdfPoint.y - cropBox.origin.y)) / cropBox.size.height;
viewPoint.y = pageRenderRect.size.height * (cropBox.size.width - (pdfPoint.x - cropBox.origin.x)) / cropBox.size.width;
break;
case 0:
default:
pageRenderRect = CGRectMake(0, 0, 768, 1024);
viewPoint.x = pageRenderRect.size.width * (pdfPoint.x - cropBox.origin.x) / cropBox.size.width;
viewPoint.y = pageRenderRect.size.height * (cropBox.size.height - pdfPoint.y) / cropBox.size.height;
break;
}
viewPoint.x = viewPoint.x + pageRenderRect.origin.x;
viewPoint.y = viewPoint.y + pageRenderRect.origin.y;
return viewPoint;
}
答案 0 :(得分:4)
链接存储在页面字典中的Annots数组中。循环遍历Annots数组时,通过验证注释字典中的子类型键来定位链接注释,它必须是“链接”(名称对象)。如果您有链接注释,则验证它是否在A条目中存储了操作。如果它有一个操作,则验证它是否是Web链接,操作字典中的S条目必须具有值“URI”(名称对象)。如果您有URI操作,则从操作字典中的URI条目中检索链接。
PDF页面上的注释位置存储在Rect数组中。这些是2个相对角的4个值,通常是左下x,左下y,右上x,右上y。这些值相对于页面媒体框的左下角。必须根据以下变量在视图坐标中转换这些值:1。在显示页面的视图中的位置; 2.显示页面视图中的大小(当前缩放系数* PDF页面大小); 3.页面裁剪框; 4.页面旋转。
转换方法如下所示:
- (CGPoint)convertPDFPointToViewPoint:(CGPoint)pdfPoint {
CGPoint viewPoint = CGPointMake(0, 0);
CGRect cropBox = CGPDFPageGetBoxRect(pdfPage, kCGPDFCropBox);
int rotation = CGPDFPageGetRotationAngle(pdfPage);
switch (rotation) {
case 90:
case -270:
viewPoint.x = pageRenderRect.size.width * (pdfPoint.y - cropBox.origin.y) / cropBox.size.height;
viewPoint.y = pageRenderRect.size.height * (pdfPoint.x - cropBox.origin.x) / cropBox.size.width;
break;
case 180:
case -180:
viewPoint.x = pageRenderRect.size.width * (cropBox.size.width - (pdfPoint.x - cropBox.origin.x)) / cropBox.size.width;
viewPoint.y = pageRenderRect.size.height * (pdfPoint.y - cropBox.origin.y) / cropBox.size.height;
break;
case -90:
case 270:
viewPoint.x = pageRenderRect.size.width * (cropBox.size.height - (pdfPoint.y - cropBox.origin.y)) / cropBox.size.height;
viewPoint.y = pageRenderRect.size.height * (cropBox.size.width - (pdfPoint.x - cropBox.origin.x)) / cropBox.size.width;
break;
case 0:
default:
viewPoint.x = pageRenderRect.size.width * (pdfPoint.x - cropBox.origin.x) / cropBox.size.width;
viewPoint.y = pageRenderRect.size.height * (cropBox.size.height - pdfPoint.y) / cropBox.size.height;
break;
}
viewPoint.x = viewPoint.x + pageRenderRect.origin.x;
viewPoint.y = viewPoint.y + pageRenderRect.origin.y;
return viewPoint;
}
其中pageRenderRect是显示页面的视图中的矩形。 您可以在此处找到其他参考源代码:http://ipdfdev.com/2011/06/21/links-navigation-in-a-pdf-document-on-iphone-and-ipad/
答案 1 :(得分:1)
猜测一下,您是否应用了一些计算变换来将PDF内容扩展到您拥有的任何帧,但未将其应用于按钮帧?您似乎已经对显示的代码进行了硬编码转换,假设PDF将使用一个源点等于一个目标点进行渲染,如果您完全缩放PDF,那么显然情况并非如此。
这个诊断可以解释为什么看起来这些按钮是为了涵盖'Metropolitan'和'Saesonen 2008-2009',但是他们的坐标系统已经缩小到左上角。