image_thumb78.png  

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

 

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




本文方法參考了:官方文檔。見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];

 

這樣就可以了,而且中文啥的都沒問題。
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()