2012-04-10 81 views
0

我尝试阅读pdf文件。我想用这个功能找到pdf文件中的字节并读取线

databuffer = [file readDataOfLength : 7] but i want read all the byte in the line . 

它的意思是我用seekToFileOffset找到字节,但我想读线。

NSMutableArray  *nameArray = [[NSMutableArray alloc] initWithObjects:nil]; 

NSMutableArray  *nameArrayDict = [[NSMutableArray alloc] initWithObjects:nil]; 
NSString   *path   = [[NSBundle mainBundle] pathForResource:@"testpdf" ofType:@"pdf"]; 
NSString   *contents  = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding error:nil]; 


    int var=[[nameArray objectAtIndex:[nameArray count]-2] intValue]; 

    NSFileHandle *file; 
    NSData *databuffer; 
    file = [NSFileHandle fileHandleForReadingAtPath: appFile]; 

    int i=0; 
    while (file!=nil) { 
     [file seekToFileOffset: var+i]; 

     databuffer = [file readDataOfLength : 7]; 
     NSString* aStr; 
     aStr = [[NSString alloc] initWithData: databuffer encoding:NSASCIIStringEncoding]; 

     NSLog(@"%@",aStr); 

     i=i+[databuffer length]; 
    } 

现在我尝试你的解决方案,但我什么都不能显示!

CGPDFPageRef page = CGPDFDocumentGetPage (myDocument, 1);// 2 

CGPDFDictionaryRef d; 

d = CGPDFPageGetDictionary(page); 


CGPDFScannerRef myScanner; 

CGPDFOperatorTableRef myTable; 
myTable = CGPDFOperatorTableCreate(); 



CGPDFContentStreamRef myContentStream = CGPDFContentStreamCreateWithPage (page);// 3 




myScanner = CGPDFScannerCreate (myContentStream, myTable, NULL);// 4 





CGPDFScannerScan (myScanner);// 5 


CGPDFOperatorTableSetCallback(myTable, "BT", &op_BT);//Begin text object 
CGPDFOperatorTableSetCallback(myTable, "ET", &op_ET);//End text object 
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); 
+0

PDF文件不是ASCII文本。它们中的大部分通常是二进制的,甚至可能被压缩。 – JeremyP 2012-04-10 10:47:23

回答

0

我建议不要以您喜欢的方式解析您的pdf。

尝试使用CGPDFScannerdocs here

由于JeremyP说:批号的zlib的压缩的东西是在一个PDF文件。搜索行尾。 使用CGPDFScanner来提取字体图,图像等。

但并不那么容易。我可以告诉你。 :)

+0

你能看到我的链接吗? – franck 2012-04-10 11:11:11

+0

stackoverflow.com/questions/10042723/haw-i-can-parse-cgpdfarrayref-with-cgpdf – franck 2012-04-10 11:11:32