您的位置:首页 > 移动开发 > IOS开发

iOS开发之Quartz2D生成PDF-Part1

2015-08-17 22:35 573 查看

iOS开发之Quartz2D生成PDF-Part1

使用iOS的Quartz2D 生成PDF文件

首先创建一个工程,导入CoreText.framework框架。在
PDFViewController.h
文件中引入CoreText文件

#import <CoreText/CoreText.h>


PDFViewController.m
文件中创建一个新方法:

-(void)drawText
{
NSString* fileName = @"Invoice.PDF";

NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];

NSString* textToDraw = @"Hello World";
CFStringRef stringRef = (__bridge CFStringRef)textToDraw;

// 使用 Core Text Framesetter.
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);

CGRect frameRect = CGRectMake(0, 0, 300, 50);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);

// 获取将要被渲染的帧
CFRange currentRange = CFRangeMake(0, 0);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);

// 创建一个PDF的上下文,页面大小默认为 612 x 792
UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);

// 标记新页面的开头
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

// 获取上下文.
CGContextRef currentContext = UIGraphicsGetCurrentContext();

// 把文字变成矩阵已知状态。这将确保没有旧缩放因子被留在原处。
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);

//文本坐标翻转
CGContextTranslateCTM(currentContext, 0, 100);
CGContextScaleCTM(currentContext, 1.0, -1.0);

// 绘制帧
CTFrameDraw(frameRef, currentContext);

CFRelease(frameRef);
CFRelease(stringRef);
CFRelease(framesetter);

//关闭PDF上下文
UIGraphicsEndPDFContext();

}


此方法有点长,在此分段解释下:

在沙盒目录下创建PDF文件,并命名为
Invoice.pdf


NSString* fileName = @"Invoice.PDF";

NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];


下面的代码块是创建一个已“Hello,world”为内容的PDF,将string转换为CFStringRef格式

NSString* textToDraw = @"Hello World";
CFStringRef stringRef = (__bridge CFStringRef)textToDraw;

CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);


然后是创建一个CGRect用于定义文本的大小

CGRect frameRect = CGRectMake(0, 0, 300, 50);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);

CFRange currentRange = CFRangeMake(0, 0);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);


接下来,我们创建一个PDF上下文和标记的PDF的起始页。 PDF的每个页面都有开始并调用UIGraphicsBeginPDFPageWithInfo 。

UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);

UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

CGContextRef currentContext = UIGraphicsGetCurrentContext();


由于Core Graphics 的坐标是从左下角开始,而UIKit的坐标是从左上角开始,所以需做一个变换:

CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);

CGContextTranslateCTM(currentContext, 0, 100);
CGContextScaleCTM(currentContext, 1.0, -1.0);


我们绘制文本实际帧,释放Core Graphics对象,并关闭PDF上下文至此PDF写入到沙盒目录下。

CTFrameDraw(frameRef, currentContext);

CFRelease(frameRef);
CFRelease(stringRef);
CFRelease(framesetter);

UIGraphicsEndPDFContext();


使用UIWebView展示PDF文件

PDFViewController.m
中的
drawText
方法后添加下面方法:

-(void)showPDFFile
{
NSString* fileName = @"Invoice.PDF"; NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [arrayPaths objectAtIndex:0]; NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];

UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

NSURL *url = [NSURL fileURLWithPath:pdfFileName];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[webView loadRequest:request];

[self.view addSubview:webView];
}


viewDidLoad
中添加

- (void)viewDidLoad
{
[self drawText];
[self showPDFFile];

[super viewDidLoad];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: