NSData to pdf //CGContextRef相關的操作 // iOS讀取和顯示PDF文檔


使用下面函數創建一個pdf

  1. CGPDFDocumentRef CGPDFDocumentCreateWithProvider ( CGDataProviderRef provider );
復制代碼

創建provider使用下面函數

  1. CGDataProviderRef CGDataProviderCreateWithCFData ( CFDataRef data );

復制代碼

CGContextRef context = UIGraphicsGetCurrentContext();

 

畫一個正方形圖形 沒有邊框

CGContextSetRGBFillColor(context, 0, 0.25, 0, 0.5);
CGContextFillRect(context, CGRectMake(2, 2, 270, 270));
CGContextStrokePath(context);

 

寫文字

CGContextSetLineWidth(context, 1.0);
CGContextSetRGBFillColor (context,  1, 1, 1, 1.0);

UIFont  *font = [UIFont boldSystemFontOfSize:11.0];

[@"fangyp" drawInRect:CGRectMake(40, 40, 80, 20) withFont:font];

 

畫一條線

CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//線條顏色
CGContextMoveToPoint(context, 20, 20);
CGContextAddLineToPoint(context, 200,20);
CGContextStrokePath(context);

 

畫正方形邊框

CGContextSetRGBStrokeColor(context, 1, 1.0, 1.0, 1.0); 
CGContextSetLineWidth(context, 2.0);
CGContextAddRect(context, CGRectMake(2, 2, 270, 270));
CGContextStrokePath(context);

 

畫方形背景顏色

  CGContextTranslateCTM(ctx, 0.0f, self.view.bounds.size.height);
   CGContextScaleCTM(ctx, 1.0f, -1.0f);
   UIGraphicsPushContext(ctx);
  CGContextSetLineWidth(ctx,320);
  CGContextSetRGBStrokeColor(ctx, 250.0/255, 250.0/255, 210.0/255, 1.0); 
  CGContextStrokeRect(ctx, CGRectMake(0, 0, 320, 460));

  UIGraphicsPopContext(); 

iOS讀取和顯示PDF文檔

iOS創建PDF文件中簡單介紹了如何生成pdf文件。現在有需求要顯示PDF文檔。看了一下Apple的API,大概有兩種方法:

  • 使用WebView,可以直接讀取PDF,這個比較簡單,可參見:最簡單的WebView應用,缺點是自定義的能力較弱,優點是簡單,像讀取網頁一樣;
  • 使用自定義的UIView,需要繼承UIView,自定義效果很好,問題是需要了解和使用的API較多。

本文只說明自定義UIView的方法。實現的在iPad模擬器上的效果:

本文方法參考了:官方文檔。見A function that draw a PDF page的代碼部分:

void MyDisplayPDFPage (CGContextRef myContext, 
                    size_t pageNumber, 
                    const char *filename) 

    CGPDFDocumentRef document; 
    CGPDFPageRef page; 
    CGRect box; 
  
    document = MyGetPDFDocumentRef (filename);// 1 
    page = CGPDFDocumentGetPage (document, pageNumber);// 2 
    CGContextDrawPDFPage (myContext, page);// 3 
    CGPDFDocumentRelease (document);// 4 
}

可見,編寫讀取的代碼很簡單,只需給定三個參數即可。后兩個很容易,pageNumber是int型的數字,表示第幾頁,filename是肯定知道的。問題是如何獲取CGContextRef,這個類型對象是用於繪圖的上下文對象引用,沒有它就沒法繪制到屏幕界面上。

 

查了一下文檔,特別是這個帖子:

http://stackoverflow.com/questions/3287635/how-to-parse-pdf-in-objective-c-for-ipad

看來要繼承UIView,才能得到當前視圖的Context。基本思路是覆蓋UIView的drawRect方法,在該方法中:

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

調用UIGraphicsGetCurrentContext方法,將當前的圖形上下文設置給調用PDF的代碼。drawRect方法會在iOS系統繪制界面的時候調用。

下面來說說編寫代碼的步驟,首先創建一個view-based application,然后,通過IB,設置控制器到view的關聯。

以下不再用IB了,PDF的UIView是通過程序生成的。

創建PdfView類,是UIView的子類。頭文件:

#import <Foundation/Foundation.h>

@interface PdfView : UIView { 
    CGPDFDocumentRef pdf; 
}

-(void)drawInContext:(CGContextRef)context;

@end

 

里面帶一個成員,pdf,代表pdf文檔對象的引用。一個方法,用於根據圖形上下文在視圖中繪制制定的pdf頁面。

m文件:

#import "PdfView.h"

@implementation PdfView 
- (id)initWithFrame:(CGRect)frame{ 
    
    if ((self = [super initWithFrame:frame])) 
    { 
        // Initialization code 
        if(self != nil) 
        { 
            CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("test.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()]; 
}

在這里使用的pdf文檔,是放在項目的Resources目錄下的。

再往下,就是在Controller中通過程序創建PdfView實例,並將它關聯為Controller根視圖的子視圖:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    CGRect frame = CGRectMake(0, 0, 768, 1000); 
    
    PdfView *pdfView = [[PdfView alloc] initWithFrame:frame]; 
    pdfView.backgroundColor=[UIColor whiteColor]; 
    [self.view addSubview:pdfView]; 
    
}

 

這里因為是使用iPad,因此長寬是1000(上面留點空間)和768。另外,需要設置底色,默認情況下底色是黑色的,和黑體的文字在一起就顯示不出文字了,我設置的是白色:

pdfView.backgroundColor=[UIColor whiteColor];


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM