我正在尝试使用 Quartz 框架解析 PDF 文档,并将 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;
}