Подтвердить что ты не робот

Как получить номер страницы из строки назначения во время разбора PDF в iphone

Я использую следующий код. он всегда попадает в строку назначения 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;
}
4b9b3361

Ответ 1

Когда вы впервые открываете 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))]];
}

Если вы сделаете это один раз, а KEEP docRef, ссылки на словарь страниц будут такими же, как ссылки Dest, когда вы читаете массив "Annots". Индекс объекта в вашем массиве pagePointers будет номером страницы (начиная с 0, поэтому, естественно, вы добавляете 1).