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]; }
相关文章推荐
- iOS 类别与继承的区别以及复用机制的理解
- IOS 常用枚举写法
- iOS 设置控件圆角及边框
- iOS蓝牙开发入门05--模仿demo写程序
- iOS开发日常技术
- iOS开发需要学习哪些内容?
- iOS编程:学习篇(十)
- iOS 地图(添加大头针)
- iOS多线程知识点总结
- iOS中制作可复用的框架Framework
- iOS事件处理
- ios中的代理与协议(delegate)
- iOS开发——多线程篇——GCD
- IOS 设置TableViewCell背景透明
- 6.ios-类存管理与分类
- iOS 地图
- iOS开发——多线程篇——NSThread
- 5.ios-内存管理
- iOS开发——多线程篇——多线程介绍
- iOS开发之遍历Model类的属性并完善使用Runtime给Model类赋值