如何在Objective C for iPad中解析PDF

Tec*_*Bee 7 pdf objective-c ipad

我很难解析PDF文件.请指导我如何做到这一点.

头文件.

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

-(void)drawInContext:(CGContextRef)context;

@end
Run Code Online (Sandbox Code Playgroud)

实施文件

//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
Run Code Online (Sandbox Code Playgroud)

现在我将这个类(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];
Run Code Online (Sandbox Code Playgroud)

它没有任何表现.我收到以下错误/警告:

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
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

问候.

Joo*_*ost 4

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

\n\n

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

\n\n
//PDFViewer.m\n@implementation PDFViewer\n\n- (void)drawRect:(CGRect)rect {\n    [self drawInContext:UIGraphicsGetCurrentContext()];\n}\n\n@end\n
Run Code Online (Sandbox Code Playgroud)\n\n

编辑\n尽管我不喜欢给任何人复制粘贴就绪代码,但如果您不理解我的最新评论,我认为没有其他选择。我不知道你尝试过什么,但如果你试图理解我真正在说什么,这是你唯一能想到的:

\n\n
//PDFViewer.m\n@implementation PDFViewer\n\n- (id)initWithFrame:(CGRect)frame \n{\n\n    if (self = [super initWithFrame:frame]) \n    {\n        CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("WR1MayJun1S08.pdf"), NULL, NULL);\n        pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);\n        CFRelease(pdfURL);\n    }\n    return self;\n}\n\n-(void)drawInContext:(CGContextRef)context\n{\n    // PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate system\n    // before we start drawing.\n    CGContextTranslateCTM(context, 0.0, self.bounds.size.height);\n    CGContextScaleCTM(context, 1.0, -1.0);\n\n    // Grab the first PDF page\n    CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);\n    // 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\n    CGContextSaveGState(context);\n    // CGPDFPageGetDrawingTransform provides an easy way to get the transform for a PDF page. It will scale down to fit, including any\n    // base rotations necessary to display the PDF page correctly. \n    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true);\n    // And apply the transform.\n    CGContextConcatCTM(context, pdfTransform);\n    // Finally, we draw the page and restore the graphics state for further manipulations!\n    CGContextDrawPDFPage(context, page);\n    CGContextRestoreGState(context);\n}\n\n- (void)drawRect:(CGRect)rect {\n    [self drawInContext:UIGraphicsGetCurrentContext()];\n}\n\n- (void)dealloc \n{\n    CGPDFDocumentRelease(pdf);\n    [super dealloc];\n}\n\n@end\n
Run Code Online (Sandbox Code Playgroud)\n\n

\xe2\x80\x94

\n\n
//MainViewController.m\n\nCGRect frame = CGRectMake(0, 200, 300, 500);\n\nPDFViewer *pdfViewer = [[PDFViewer alloc] initWithFrame:frame];\n[self.view addSubview:pdfViewer];\n
Run Code Online (Sandbox Code Playgroud)\n