在iPhone上将整个pdf页面parsing为NSString

我一直试图parsing一个文本的PDF页面到NSString一段时间,我唯一能find的方法是search特定的string值。

我想要做的是parsing一个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]); } 

Quartz提供了让您检查PDF文档结构和内容stream的function。 检查文档结构可让您阅读文档目录中的条目以及与每个条目相关的内容。 通过recursion遍历目录,您可以检查整个文档。

一个PDF内容stream正如其名字所暗示的那样,是一个连续的数据stream,如“BT 12 / F71 Tf(画这个文本)Tj”。 。 。 “PDF操作符及其描述符与实际的PDF内容混合在一起。 检查内容stream需要您顺序访问它。

这个developer.apple文档显示了如何检查PDF文档的结构并parsingPDF文档的内容。