我正在尝试使用Quartz框架解析PDF文档并使用copy&将Apple文档中的代码片段粘贴到我的源代码中。 不幸的是,它不会检索任何数据。它只是迭代页面,将当前页面的数量记录到控制台并在最后崩溃。 您对代码有什么问题有任何想法吗?
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;
NSLog(@"DP /%s\n", name);
}
static void op_BMC (CGPDFScannerRef s, void *info)
{
const char *name;
if (!CGPDFScannerPopName(s, &name))
return;
NSLog(@"BMC /%s\n", name);
}
static void op_BDC (CGPDFScannerRef s, void *info)
{
const char *name;
if (!CGPDFScannerPopName(s, &name))
return;
NSLog(@"BDC /%s\n", name);
}
static void op_EMC (CGPDFScannerRef s, void *info)
{
const char *name;
if (!CGPDFScannerPopName(s, &name))
return;
NSLog(@"EMC /%s\n", name);
}
static void op_TJ (CGPDFScannerRef s, void *info)
{
const char *name;
if (!CGPDFScannerPopName(s, &name))
return;
NSLog(@"TJ /%s\n", name);
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGPDFDocumentRef myDocument;
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"];
NSURL *fileUrl = [NSURL fileURLWithPath:urlAddress];
CFURLRef url = (__bridge CFURLRef)fileUrl;
myDocument = CGPDFDocumentCreateWithURL(url);
CFRelease (url);
if (myDocument == NULL) {// 2
NSLog(@"can't open `%@'.", fileUrl);
}
if (!CGPDFDocumentIsUnlocked (myDocument)) {// 4
CGPDFDocumentRelease(myDocument);
}
else if (CGPDFDocumentGetNumberOfPages(myDocument) == 0) {// 5
CGPDFDocumentRelease(myDocument);
}
else {
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);
CGPDFOperatorTableSetCallback (myTable, "Tj", &op_TJ);
int k;
CGPDFPageRef myPage;
CGPDFScannerRef myScanner;
CGPDFContentStreamRef myContentStream;
int numOfPages = CGPDFDocumentGetNumberOfPages (myDocument);// 1
for (k = 0; k < numOfPages; k++) {
myPage = CGPDFDocumentGetPage (myDocument, k + 1 );// 2
myContentStream = CGPDFContentStreamCreateWithPage (myPage);// 3
myScanner = CGPDFScannerCreate (myContentStream, myTable, NULL);// 4
CGPDFScannerScan (myScanner);// 5
CGPDFPageRelease (myPage);// 6
CGPDFScannerRelease (myScanner);// 7
CGPDFContentStreamRelease (myContentStream);// 8
NSLog(@"processed page %i",k);
}
CGPDFOperatorTableRelease(myTable);
CGPDFDocumentRelease(myDocument);
}
return YES;
}
答案 0 :(得分:1)
我没有运行代码,但前5个运算符可能不存在于您的页面内容中。其中一些还有一个名称操作数,其中一些没有任何操作数(如EMC)。此外,Tj运算符具有字符串操作数,而不是名称 删除所有pop名称方法并仅保留日志记录,您可能会得到一些输出。然后查看PDF规范以查看每个运算符的确切操作数并相应地更新代码。
答案 1 :(得分:0)
虽然我无法为您的示例代码崩溃提供解决方案,但上次我们需要这样做时,我们将解析器基于PDFKitten。
https://github.com/KurtCode/PDFKitten
如果您对解析代码感兴趣,那么有趣的东西就在Scanner.m中:
https://github.com/KurtCode/PDFKitten/blob/master/PDFKitten/Scanner.m
鉴于PDF解析的复杂性,我建议将此库作为基础并从那里移动。如果您需要在截止日期前完成实施,那么PSPDFKit可能是最发达(但价格昂贵)的软件包。
答案 2 :(得分:0)
关于CFRelease(url)
。删除它就可以了。
“(__ bridge T)op将操作数强制转换为目标类型T.如果T是可保留的对象指针类型,则op必须具有不可保留的指针类型。”