代码之家  ›  专栏  ›  技术社区  ›  TechBee

如何解析iPad的Objective C中的PDF

  •  7
  • TechBee  · 技术社区  · 16 年前

    头文件。

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

    我错过了什么?

    当做。

    2 回复  |  直到 11 年前
        1
  •  4
  •   Joost    16 年前

    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];
    
        2
  •  3
  •   Alex Cio Olie    11 年前


    1.拿一个 UIwebView (姓名: pdfView

    2.给予 IBoutlet

    3.在 Viewdidload

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

    4.ObjC.pdf应位于资源文件夹中。。