2010-07-27 2 views
12

다음 코드를 사용하고 있습니다. if는 항상 대상 문자열에 들어갑니다. 그것은 페이지 번호 루프를 얻으려고하지 않습니다. pdf가 대상 문자열을 찾은 경우 다른 부분으로 이동하지 않습니다. 그래서 어떻게 대상 문자열에서 페이지 번호를 얻을 수 있습니다. 아래 코드의 예를 볼 수 있습니다. 미리 감사드립니다.iphone에서 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; 
} 

답변

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

한 번 이렇게하고 docRef을 유지하는 경우는 "Annots"배열을 읽을 때 신지는 참조로 페이지 사전 참조가 동일합니다. pagePointers 배열에있는 객체의 색인은 페이지 번호 (0부터 시작하므로 당연히 1을 더합니다)가됩니다.

관련 문제