2010-07-27 26 views
12

Tôi đang sử dụng mã sau. nó luôn đi vào chuỗi đích nếu. nó không đi để có được vòng lặp số trang. nếu pdf tìm thấy chuỗi đích thì nó sẽ không đi đến phần khác. vì vậy làm thế nào tôi có thể nhận được số trang từ chuỗi đích. Bạn có thể xem ví dụ về mã bên dưới. Cảm ơn trước.Cách lấy số trang từ chuỗi đích trong khi phân tích cú pháp pdf trong iphone

- (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; 
} 

Trả lời

1

Khi lần đầu tiên bạn mở tệp PDF và nhận các trang, bạn có thể lưu trữ tất cả các tham chiếu trang trong một mảng. Sau đó, khi bạn nhận được một tham chiếu Dest, chỉ cần khớp nó với các mục trong mảng, và bạn sẽ biết số trang.

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

Nếu bạn thực hiện việc này một lần và KEEP docRef, tham chiếu từ điển trang sẽ giống với tham chiếu đích khi bạn đọc mảng "Annots". Chỉ mục của đối tượng trong mảng pagePointers của bạn sẽ là số trang (bắt đầu bằng 0, do đó bạn tự nhiên thêm 1).

Các vấn đề liên quan