我使用以下代码.它始终进入目标字符串if.它不会去获取页码循环.如果pdf找到目标字符串,那么它将不会转到其他部分.那么如何从目标字符串中获取页码.您可以在下面看到代码示例.提前致谢.
- (OutlineItem*)recursiveUpdateOutlines: (CGPDFDictionaryRef) outlineDic parent:(OutlineItem*) parentItem level:(NSUInteger) level;
{
// update outline count
outlineCount++;
OutlineItem* item = [[OutlineItem alloc] init];
// Level
item.level = level;
// Title
CGPDFStringRef title;
if(CGPDFDictionaryGetString(outlineDic, "Title", &title)) {
const char* pchTitle = CGPDFStringGetBytePtr(title);
item.title = [NSString stringWithUTF8String:pchTitle];
// DEBUG
//NSLog(item.title);
}
if (parentItem != nil) {
// Add to parent
[parentItem.children addObject:item];
// Next
CGPDFDictionaryRef nextDic;
if (CGPDFDictionaryGetDictionary(outlineDic, "Next", &nextDic)) {
[self recursiveUpdateOutlines:nextDic parent:parentItem level: level];
}
}
// First child
CGPDFDictionaryRef firstDic;
if (CGPDFDictionaryGetDictionary(outlineDic, "First", &firstDic)) {
[self recursiveUpdateOutlines:firstDic parent:item level: level + 1];
}
// Dest
CGPDFStringRef destString;
if(CGPDFDictionaryGetString(outlineDic, "Dest", &destString)) {
const char* pchDest = CGPDFStringGetBytePtr(destString);
CGPDFDictionaryRef destDic;
if(CGPDFDictionaryGetDictionary(dests, pchDest, &destDic)) {
NSLog(@"");
}
else {
item.destString = [NSString stringWithUTF8String:pchDest];
}
} else {
CGPDFDictionaryRef ADic;
if (CGPDFDictionaryGetDictionary(outlineDic, "A", &ADic)) {
const char* pchS;
if (CGPDFDictionaryGetName(ADic, "S", &pchS)) {
CGPDFArrayRef destArray;
if (CGPDFDictionaryGetArray(ADic, "D", &destArray)) {
int count = CGPDFArrayGetCount(destArray);
switch (count) {
case 5:
{
// dest page
CGPDFDictionaryRef destPageDic;
if (CGPDFArrayGetDictionary(destArray, 0, &destPageDic)) {
int pageNumber = [self.pages indexOfObjectIdenticalTo:destPageDic];
item.page = pageNumber;
}
// x
CGPDFInteger x;
if (CGPDFArrayGetInteger(destArray, 2, &x)) {
item.x = x;
}
// y
CGPDFInteger y;
if (CGPDFArrayGetInteger(destArray, 3, &y)) {
item.y = y;
}
// z
}
break;
default:
NSLog(@"");
break;
}
}
}
}
}
return item;
}
Run Code Online (Sandbox Code Playgroud)
当您第一次打开 PDF 并获取页面时,您可以将所有页面引用存储在一个数组中。稍后,当您获得 Dest 引用时,只需将其与数组中的项目进行匹配,您就会知道页码。
CGPDFDocumentRef docRef = CGPDFDocumentCreateWithURL(fileURL);
int numberOfPages = CGPDFDocumentGetNumberOfPages(docRef);
pagePointers = [[NSMutableArray alloc] initWithCapacity:numberOfPages];
if (0<numberOfPages) for (int i=1; i<numberOfPages; i++) {
[pagePointers addObject:[NSValue valueWithPointer:CGPDFPageGetDictionary(CGPDFDocumentGetPage(docRef, i))]];
}
Run Code Online (Sandbox Code Playgroud)
如果执行此操作一次并保留 docRef,则当您阅读“Annots”数组时,页面字典引用将与 Dest 引用相同。pagePointers 数组中对象的索引将是页码(从 0 开始,所以自然要加 1)。
| 归档时间: |
|
| 查看次数: |
1373 次 |
| 最近记录: |