如何在目标C中解析从URL到文本的pdf?

时间:2013-10-06 16:54:11

标签: objective-c parsing pdf

我正在尝试从URL获取pdf数据,然后将其解析为iOS应用程序中的NSString。

像FastPdfKit这样的库获取信息,然后将它显示在一个新的模态视图控制器中,这不是我想要的。它应该在同一个viewcontroller中加载和解析。

尝试使用Quartz解析它时,如下所示:

https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_pdf_scan/dq_pdf_scan.html

即使页面数量为1,我也没有得到任何回调。

那么我怎么能在一个viewcontroller中做到这一点? 谢谢: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

ViewController.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);
}

1 个答案:

答案 0 :(得分:1)

PdfIphone代码对我有用:https://github.com/zachron/pdfiphone

它将为您解析Pdf并使用converPdf方法返回NSString。