iOS学习笔记14—PDF的文件的创建、显示和解析
2013-08-16 21:29
671 查看
PDF的文件的创建、显示和解析在上一篇文章的这里有介绍:
具体PDF文件的创建、显示和解析在官方文档里面有,大家可以点击看下。这里介绍的是简单的PDF的显示和创建。
点击打开链接
首先是要创建一个项目,导入一个.pdf文件
并导入一下这个框架
我总是喜欢在.pch文件中引入
#import<QuartzCore/QuartzCore.h>准备工作完成
先建一个基于UIView的PDFView
在PDFView.h文件中加载
在mainViewController.m文件中
Quartz 2D,是iOS原生的,简单易用,缺点是只有2D,仅限于iOS
OpenGL ES,是开放标准的,2D和3D均有
使用Quartz 2D绘图到任意图形上下文(graphics context)创建pdf文件是很容易的事情。这需要:
指定pdf文件的位置
设置pdf的图形上下文(graphics context)
写了个特别简单的示例,效果如下:
完整的代码如下:
-(void)createPdf{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *saveDirectory = [paths objectAtIndex:0];
NSString *saveFileName = @"myPDF.pdf";
NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName];
const char *filename = [newFilePath UTF8String];
CGRect pageRect=CGRectMake(0, 0, 612, 792);
// This code block sets up our PDF Context so that we can draw to it
CGContextRef pdfContext;
CFStringRef path;
CFURLRef url;
CFMutableDictionaryRef myDictionary = NULL;
// Create a CFString from the filename we provide to this method when we call it
path = CFStringCreateWithCString (NULL, filename,
kCFStringEncodingUTF8);
// Create a CFURL using the CFString we just defined
url = CFURLCreateWithFileSystemPath (NULL, path,
kCFURLPOSIXPathStyle, 0);
CFRelease (path);
// This dictionary contains extra options mostly for ‘signing’ the PDF
myDictionary = CFDictionaryCreateMutable(NULL, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
// Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary
pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
// Cleanup our mess
CFRelease(myDictionary);
CFRelease(url);
// Done creating our PDF Context, now it’s time to draw to it
// Starts our first page
CGContextBeginPage (pdfContext, &pageRect);
// Draws a black rectangle around the page inset by 50 on all sides
CGContextStrokeRect(pdfContext, CGRectMake(50, 50, pageRect.size.width – 100, pageRect.size.height – 100));
// Adding some text on top of the image we just added
CGContextSelectFont (pdfContext, "Helvetica", 30, kCGEncodingMacRoman);
CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
CGContextSetRGBFillColor (pdfContext, 0, 0, 0, 1);
const char *text = (char *)[@"Hello world" UTF8String];
CGContextShowTextAtPoint (pdfContext, 260, 390, text, strlen(text));
// End text
// We are done drawing to this page, let’s end it
// We could add as many pages as we wanted using CGContextBeginPage/CGContextEndPage
CGContextEndPage (pdfContext);
// We are done with our context now, so we release it
CGContextRelease (pdfContext);
}
会在当前应用的Documents目录下创建一个myPDF.pdf文件,文件中只有一行字,Hello world。
图形框架: |
CoreGraphics.framework |
包含 Quartz 2D 绘图 API 接口 。Quartz 是 Mac OS X 系统使用的向量绘图引擎,它支持基于路径绘图、抗锯齿渲染、渐变、图片、颜色、坐标空间转换、PDF 文件的创建、显示和解析。虽然 API 基于 C 语言,但是它使用基于对象的抽象以表示基本绘图对象,这样可以让开发者可以更方便地保存并复用图像内容。 |
一.PDF的显示
老外写的文章点击打开链接
首先是要创建一个项目,导入一个.pdf文件
并导入一下这个框架
我总是喜欢在.pch文件中引入
#import<QuartzCore/QuartzCore.h>准备工作完成
先建一个基于UIView的PDFView
在PDFView.h文件中加载
#import <UIKit/UIKit.h> @interface PDFView : UIView { CGPDFDocumentRef pdf; } -(void)drawInContext:(CGContextRef)context; @end在PDFView.m文件中加载
#import "PDFView.h" @implementation PDFView - (void)dealloc { CGPDFDocumentRelease(pdf); [super dealloc]; } - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { if(self != nil) { CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("苹果编码规范.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()]; } @end
在mainViewController.m文件中
- (void)viewDidLoad { [super viewDidLoad]; CGRect frame = CGRectMake(0, 0, 320, 480); PDFView *pdfView =[[PDFView alloc]initWithFrame:frame]; [self.view addSubview:pdfView]; [pdfView release];
}
二.pdf的创建
pdf文档在iOS中是通过Quartz 2D库提供的api来操作的。iOS有两个图形库:Quartz 2D,是iOS原生的,简单易用,缺点是只有2D,仅限于iOS
OpenGL ES,是开放标准的,2D和3D均有
使用Quartz 2D绘图到任意图形上下文(graphics context)创建pdf文件是很容易的事情。这需要:
指定pdf文件的位置
设置pdf的图形上下文(graphics context)
写了个特别简单的示例,效果如下:
完整的代码如下:
-(void)createPdf{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *saveDirectory = [paths objectAtIndex:0];
NSString *saveFileName = @"myPDF.pdf";
NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName];
const char *filename = [newFilePath UTF8String];
CGRect pageRect=CGRectMake(0, 0, 612, 792);
// This code block sets up our PDF Context so that we can draw to it
CGContextRef pdfContext;
CFStringRef path;
CFURLRef url;
CFMutableDictionaryRef myDictionary = NULL;
// Create a CFString from the filename we provide to this method when we call it
path = CFStringCreateWithCString (NULL, filename,
kCFStringEncodingUTF8);
// Create a CFURL using the CFString we just defined
url = CFURLCreateWithFileSystemPath (NULL, path,
kCFURLPOSIXPathStyle, 0);
CFRelease (path);
// This dictionary contains extra options mostly for ‘signing’ the PDF
myDictionary = CFDictionaryCreateMutable(NULL, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
// Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary
pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
// Cleanup our mess
CFRelease(myDictionary);
CFRelease(url);
// Done creating our PDF Context, now it’s time to draw to it
// Starts our first page
CGContextBeginPage (pdfContext, &pageRect);
// Draws a black rectangle around the page inset by 50 on all sides
CGContextStrokeRect(pdfContext, CGRectMake(50, 50, pageRect.size.width – 100, pageRect.size.height – 100));
// Adding some text on top of the image we just added
CGContextSelectFont (pdfContext, "Helvetica", 30, kCGEncodingMacRoman);
CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
CGContextSetRGBFillColor (pdfContext, 0, 0, 0, 1);
const char *text = (char *)[@"Hello world" UTF8String];
CGContextShowTextAtPoint (pdfContext, 260, 390, text, strlen(text));
// End text
// We are done drawing to this page, let’s end it
// We could add as many pages as we wanted using CGContextBeginPage/CGContextEndPage
CGContextEndPage (pdfContext);
// We are done with our context now, so we release it
CGContextRelease (pdfContext);
}
会在当前应用的Documents目录下创建一个myPDF.pdf文件,文件中只有一行字,Hello world。
相关文章推荐
- iOS学习笔记(2)--Xcode6.1创建仅xib文件无storyboard的hello world应用
- Web 在线文件管理器学习笔记与总结(2)显示文件列表(名称,类型,大小,可读,可写,可执行,创建时间,修改时间,访问时间)
- iOS学习笔记(2)--Xcode6.1创建仅xib文件无storyboard的hello world应用
- iOS学习笔记58-iOS- 全方位解析.crash文件崩溃报告
- Python 创建、读取和写入文件以及yield关键字- 千月的python linux 系统管理指南学习笔记(14)
- [转]iOS学习笔记(2)--Xcode6.1创建仅xib文件无storyboard的hello world应用
- Struts2学习笔记14:Struts2的文件上传和下载【续】
- iOS学习笔记-100.彩票15——设置4_设置和推送提醒控制器创建
- 学习Hibernate源码——配置文件解析,SessionFactory创建
- iOS学习笔记-046.控制管理器的创建
- No.1 iOS---UI学习第一天(笔记)(以及为什么模拟器上不显示我们添加的控件的解决办法)
- IOS学习笔记27—使用GDataXML解析XML文档
- php学习笔记3--文件系统的操作(创建、打开及批量删除)
- iOS学习笔记20-通过AirServer使用AirPlay镜像并实现多屏幕显示控制
- Jfreechart学习笔记7-JFreeChart生成PDF文件
- ((ios开发学习笔记五))手动创建Tab Bar Controller 程序
- IntelliJ IDEA 学习笔记 - Hello World 项目创建与配置文件介绍
- iText制作PDF文件 学习笔记 (六)
- Hibernate学习之 -- 使用Middlegen-Hibernate-r5创建oracle10g的table的hibernate映射文件,Hibernate学习笔记三 ---持久化类和关系数据
- iOS开发CoreGraphics核心图形框架之九——PDF文件的渲染与创建