Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get page number from destination string while pdf parsing in iphone

Tags:

iphone

I am using the following code. it always gets into the destination string if. it does not go to get page number loop. if pdf found destination string then it will not go to else part. so how can i get the page number from destination string. You can see an example of the code below. Thanks in advance.

- (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;
}
like image 417
user373017 Avatar asked Jul 27 '10 07:07

user373017


1 Answers

When you first open the PDF and get the pages you can store all the page references in an array. Later on when you get a Dest reference, just match it against the items in the array, and you will know the page number.

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))]];
}

If you do this once, and KEEP the docRef, the page dictionary references will be the same as the Dest references when you are reading the "Annots" array. The index of the object in your pagePointers array will be the page number (starting with 0, so naturally you add 1).

like image 128
rage Avatar answered Nov 11 '22 16:11

rage