我正在尝试从 URL 获取 pdf 数据,然后将其解析为 iOS 应用程序中的 NSString。
像 FastPdfKit 这样的库会获取信息,然后将其显示在一个新的模态视图控制器中,这不是我想要的。它应该在同一个视图控制器中加载和解析。
当尝试使用 Quartz 解析它时,如下所示:
即使页数应该是 1,我也没有收到任何回调。
那么我怎样才能在单个视图控制器中做到这一点呢?感谢:D
代码尝试:
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("MP /%s\n", name);
}
static void
op_BMC (CGPDFScannerRef s, void *info)
{
const char *name;
if (!CGPDFScannerPopName(s, &name))
return;
printf("MP /%s\n", name);
}
static void
op_BDC (CGPDFScannerRef s, void *info)
{
const char *name;
if (!CGPDFScannerPopName(s, &name))
return;
printf("MP /%s\n", name);
}
static void
op_EMC (CGPDFScannerRef s, void *info)
{
const char *name;
if (!CGPDFScannerPopName(s, &name))
return;
printf("MP /%s\n", name);
}
- (void)viewDidLoad
{
[super viewDidLoad];
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);
CGPDFDocumentRef myDocument;
CFURLRef url = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("lel.pdf"), NULL, NULL);
myDocument = CGPDFDocumentCreateWithURL(url);
int k;
CGPDFPageRef myPage;
CGPDFScannerRef myScanner;
CGPDFContentStreamRef myContentStream;
int numOfPages = CGPDFDocumentGetNumberOfPages (myDocument);// 1
NSLog(@"%i", numOfPages);
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
}
CGPDFOperatorTableRelease(myTable);
}
尝试使用 ZachRon 的 pdf2iPhone 时,出现此错误:
http://gyazo.com/52c87cc88a397a64cdbb015113c201c6
视图控制器.m:
#import "JONViewController.h"
#import "pdf.h"
@interface JONViewController ()
@end
@implementation JONViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *path = [[NSBundle mainBundle] URLForResource:@"lel" withExtension:@"pdf"];
NSString *string = [[NSString alloc] initWithContentsOfURL:path encoding:NSUTF8StringEncoding error:nil];
NSString *pdfString = convertPDF(string);
NSLog(@"%@", pdfString);
}