如何在Objective C for iPad中解析PDF [英] How to parse PDF in Objective C for iPad

查看:115
本文介绍了如何在Objective C for iPad中解析PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难解析PDF文件。请指导我如何操作。

I am stuck with parsing a PDF file. Please guide me how to do this.

头文件。

//PDFViewer.h
@interface PDFViewer : UIView 
{
 CGPDFDocumentRef pdf;
}

-(void)drawInContext:(CGContextRef)context;

@end

实施档案

//PDFViewer.m
@implementation PDFViewer


- (id)initWithFrame:(CGRect)frame 
{

 if ((self = [super initWithFrame:frame])) 
 {
        // Initialization code
  if(self != nil)
  {
   CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("WR1MayJun1S08.pdf"), NULL, NULL);
   pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
   CFRelease(pdfURL);
  }
    }
    return self;
}


-(void)drawInContext:(CGContextRef)context
{
 // PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate system
 // before we start drawing.
 CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
 CGContextScaleCTM(context, 1.0, -1.0);

 // Grab the first PDF page
 CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);
 // We're about to modify the context CTM to draw the PDF page where we want it, so save the graphics state in case we want to do more drawing
 CGContextSaveGState(context);
 // CGPDFPageGetDrawingTransform provides an easy way to get the transform for a PDF page. It will scale down to fit, including any
 // base rotations necessary to display the PDF page correctly. 
 CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true);
 // And apply the transform.
 CGContextConcatCTM(context, pdfTransform);
 // Finally, we draw the page and restore the graphics state for further manipulations!
 CGContextDrawPDFPage(context, page);
 CGContextRestoreGState(context);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc 
{
    CGPDFDocumentRelease(pdf);
 [super dealloc];
}


@end

现在我我将这个类(PDFViewer.h)添加到我的MainViewController。

Now I am adding this class (PDFViewer.h) to my MainViewController.

//MainViewController.m

CGRect frame = CGRectMake(0, 200, 300, 500);

PDFViewer *pdfViewer = [[PDFViewer alloc] initWithFrame:frame];
CGContextRef context = UIGraphicsGetCurrentContext();
[pdfViewer drawInContext:context];
[self.view addSubview:pdfViewer];

它什么都没有显示。我收到以下错误/警告:

It shows nothing. I get the following errors/warnings:

local MultiView[2850] <Error>: CGContextTranslateCTM: invalid context
local MultiView[2850] <Error>: CGContextScaleCTM: invalid context
local MultiView[2850] <Error>: CGContextSaveGState: invalid context
local MultiView[2850] <Error>: CGContextConcatCTM: invalid context
local MultiView[2850] <Error>: CGContextRestoreGState: invalid context

我是什么遗失?

问候。

推荐答案

UIGraphicsGetCurrentContext 如果没有上下文,则不会返回上下文。显然。

UIGraphicsGetCurrentContext does not return a context if there isn't one, obviously.

您尝试在视图初始化时获取上下文没有上下文可用。在调用 - [UIView drawRect:] 之前,有效的上下文被压入堆栈。这应该有效:

You try to get the context at view initialization, at that time there is no context available. A valid context gets pushed onto the stack just before -[UIView drawRect:] is being called. This should work:

//PDFViewer.m
@implementation PDFViewer

- (void)drawRect:(CGRect)rect {
    [self drawInContext:UIGraphicsGetCurrentContext()];
}

@end

EDIT
尽管我不想给任何人复制并粘贴准备代码,但如果你不理解我的最新评论,我认为还有其他选择。我不知道你尝试了什么,但是如果你试着理解我真正说的是什么,这是你唯一能想到的:

EDIT Eventhough I don't like to give anyone copy-and-paste-ready-code, I don't think there is another option left if you didn't understand my latest comment. I don't know what you've tried, but if you try to understand what I'm really saying, this is the only thing you can come up with:

//PDFViewer.m
@implementation PDFViewer

- (id)initWithFrame:(CGRect)frame 
{

    if (self = [super initWithFrame:frame]) 
    {
        CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("WR1MayJun1S08.pdf"), NULL, NULL);
        pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
        CFRelease(pdfURL);
    }
    return self;
}

-(void)drawInContext:(CGContextRef)context
{
    // PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate system
    // before we start drawing.
    CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // Grab the first PDF page
    CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);
    // We're about to modify the context CTM to draw the PDF page where we want it, so save the graphics state in case we want to do more drawing
    CGContextSaveGState(context);
    // CGPDFPageGetDrawingTransform provides an easy way to get the transform for a PDF page. It will scale down to fit, including any
    // base rotations necessary to display the PDF page correctly. 
    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true);
    // And apply the transform.
    CGContextConcatCTM(context, pdfTransform);
    // Finally, we draw the page and restore the graphics state for further manipulations!
    CGContextDrawPDFPage(context, page);
    CGContextRestoreGState(context);
}

- (void)drawRect:(CGRect)rect {
    [self drawInContext:UIGraphicsGetCurrentContext()];
}

- (void)dealloc 
{
    CGPDFDocumentRelease(pdf);
    [super dealloc];
}

@end

-

//MainViewController.m

CGRect frame = CGRectMake(0, 200, 300, 500);

PDFViewer *pdfViewer = [[PDFViewer alloc] initWithFrame:frame];
[self.view addSubview:pdfViewer];

这篇关于如何在Objective C for iPad中解析PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆