【问题标题】:How to parse PDF in Objective C for iPad如何在 iPad 的 Objective C 中解析 PDF
【发布时间】:2011-03-18 06:55:46
【问题描述】:

我无法解析 PDF 文件。请指导我如何做到这一点。

头文件。

//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。

//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];

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

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

我错过了什么?

问候。

【问题讨论】:

    标签: objective-c ipad pdf


    【解决方案1】:

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

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

    //PDFViewer.m
    @implementation PDFViewer
    
    - (void)drawRect:(CGRect)rect {
        [self drawInContext:UIGraphicsGetCurrentContext()];
    }
    
    @end
    

    编辑 尽管我不喜欢给任何人复制和粘贴准备好的代码,但如果您不理解我的最新评论,我认为没有其他选择。我不知道您尝试了什么,但如果您尝试理解我的真正意思,这是您唯一能想到的:

    //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];
    

    【讨论】:

    • 我做了以下: - (void)drawRect:(CGRect)rect { CGRect frame = CGRectMake(0, 200, 400, 400); pdfViewer = [[PDFViewer alloc] initWithFrame:frame]; [pdfViewer drawInContext:UIGraphicsGetCurrentContext()]; [self.view addSubview:pdfViewer]; } 它什么也没显示。
    • 不,那当然什么也没做。只需将初始化部分保留在MainViewController 中,CGContextRef context = ...; [pdfViewer drawInContext:context]; 行除外。那就是你在初始化时调用的绘制代码,应该在需要绘制视图时调用,在PDFViewer-drawRect:方法中。
    • k 你能给我一个示例代码吗?我很困惑:(问候
    【解决方案2】:

    我有另一种简单的方法来解析 iPhone/iPad 的 PDF:
    1.取一张UIwebView(姓名:pdfView)。

    2.Give IBoutlet 连接并将其委托给 FilesOwner

    3.在Viewdidload

    [self.pdfView loadRequest:[NSURLRequest requestWithURL:[NSURL 
                                           fileURLWithPath:[[NSBundle mainBundle] 
                                           pathForResource:@"ObjC" ofType:@"pdf"]]]];
    

    4.ObjC.pdf 应该在资源文件夹中..

    【讨论】:

      猜你喜欢
      • 2011-08-31
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 2011-10-10
      • 1970-01-01
      相关资源
      最近更新 更多