问题描述
||
谁能帮助我使用iOS在PDF中阅读和编写注释吗?这是我尝试过的示例代码。谁能告诉我我要去哪里错了?我可以加载注释数组,但是当我尝试获取A的字典(下面的行)时,我无法解析它。
CGPDFDictionaryRef aDict;
if(!CGPDFDictionaryGetDictionary(annotDict,\"A\",&aDict))
我尝试过的代码如下:
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,&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,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];
解决方法
在注释字典中,A键是可选的。如果注释是指向另一个页面的链接,则它可能使用Dest键。或者它可能在其他操作字典中使用U键。如果无法检索A词典,请尝试加载AA密钥。如果它不存在,则注释不具有操作。如果存在,则从AA字典中加载U键。