2014-01-05 46 views
2

我一直試圖解析一段文本的pdf頁面到NSString一段時間,而我唯一能找到的就是搜索特定字符串值的方法。在iPhone上將整個pdf頁面解析爲NSString

我希望做的是解析PDF的一個頁面,而無需使用任何外部庫,如PDFKitten,PDFKit等

我想有一個NSArray,的NSString或NSDictionary中,如果數據可能。

感謝:D!

到目前爲止我嘗試過的一塊。

CGPDFDocumentRef MyGetPDFDocumentRef (const char *filename) { 
    CFStringRef path; 
    CFURLRef url; 
    CGPDFDocumentRef document; 
    path = CFStringCreateWithCString (NULL, filename,kCFStringEncodingUTF8); 
    url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0); 
    CFRelease (path); 
    document = CGPDFDocumentCreateWithURL (url);// 2 
    CFRelease(url); 
    int count = CGPDFDocumentGetNumberOfPages (document);// 3 
    if (count == 0) { 
     printf("`%s' needs at least one page!", filename); 
     return NULL; 
    } 
    return document; 
} 

// table methods to parse pdf 
static void op_MP (CGPDFScannerRef s, void *info) { 
    const char *name; 
    if (!CGPDFScannerPopName(s, &name)) 
     return; 
    printf("MP /%s\n", name); 
} 

static void op_DP (CGPDFScannerRef s, void *info) { 
    const char *name; 
    if (!CGPDFScannerPopName(s, &name)) 
     return; 
    printf("DP /%s\n", name); 
} 

static void op_BMC (CGPDFScannerRef s, void *info) { 
    const char *name; 
    if (!CGPDFScannerPopName(s, &name)) 
     return; 
    printf("BMC /%s\n", name); 
} 

static void op_BDC (CGPDFScannerRef s, void *info) { 
    const char *name; 
    if (!CGPDFScannerPopName(s, &name)) 
     return; 
    printf("BDC /%s\n", name); 
} 

static void op_EMC (CGPDFScannerRef s, void *info) { 
    const char *name; 
    if (!CGPDFScannerPopName(s, &name)) 
     return; 
    printf("EMC /%s\n", name); 
} 

void MyDisplayPDFPage (CGContextRef myContext,size_t pageNumber,const char *filename) { 
    CGPDFDocumentRef document; 
    CGPDFPageRef page; 
    document = MyGetPDFDocumentRef (filename);// 1 
    totalPages=CGPDFDocumentGetNumberOfPages(document); 
    page = CGPDFDocumentGetPage (document, 1);// 2 

    CGPDFDictionaryRef d; 

    d = CGPDFPageGetDictionary(page); 

    CGPDFScannerRef myScanner; 
    CGPDFOperatorTableRef myTable; 
    myTable = CGPDFOperatorTableCreate(); 
    CGPDFOperatorTableSetCallback (myTable, "MP", &op_MP); 
    CGPDFOperatorTableSetCallback (myTable, "DP", &op_DP); 
    CGPDFOperatorTableSetCallback (myTable, "BMC", &op_BMC); 
    CGPDFOperatorTableSetCallback (myTable, "BDC", &op_BDC); 
    CGPDFOperatorTableSetCallback (myTable, "EMC", &op_EMC); 

    CGPDFContentStreamRef myContentStream = CGPDFContentStreamCreateWithPage (page);// 3 
    myScanner = CGPDFScannerCreate (myContentStream, myTable, NULL);// 4 

    CGPDFScannerScan (myScanner);// 5 

    CGPDFStringRef str; 

    d = CGPDFPageGetDictionary(page); 

    if (CGPDFDictionaryGetString(d, "Lorem", &str)){ 
     CFStringRef s; 
     s = CGPDFStringCopyTextString(str); 
     if (s != NULL) { 
      NSLog(@"%@ testing it", s); 
     } 
     CFRelease(s); 
    } 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    MyDisplayPDFPage(UIGraphicsGetCurrentContext(), 1, [[[NSBundle mainBundle] pathForResource:@"TestPage" ofType:@"pdf"] UTF8String]); 

} 

回答

4

石英提供了讓你檢查PDF文檔結構和內容流的功能。檢查文檔結構可讓您閱讀文檔目錄中的條目以及與每個條目相關的內容。通過遞歸遍歷目錄,您可以檢查整個文檔。

PDF內容流正如其名稱所暗示的那樣,是一個連續的數據流,如「BT 12/F71 Tf(繪製此文本)Tj」。 。 。 'PDF操作符及其描述符與實際的PDF內容混合在一起。檢查內容流需要您順序訪問它。

This developer.apple documentation顯示如何檢查PDF文檔的結構並解析PDF文檔的內容。