Generating PDF Content
UIkit框架通
过提供一系列的函数用于创建一个PDF。通常情况下创一个PDF文档需要一下几个步骤:
1、通过UIGraphicsBeginPDFContex
tToFile创建一个PDF context,并且将这个context和一个系统目录下文件名关联
2、通过UIGrapicsBeginPDFPage函数打开或者创建PDF的新一页,关闭之前的一页(如果之前一页被打开)
3、绘制内容
4、通过UIGraphicsEndPDFContext函数结束绘制
Creating and Configuring the PDF Context
创建一个PDF context的函数有两个:
UIGraphicsBeginPDFContex
tToData:context的目的地为一个PDF data,它是一个NSMutableData类的数据
UIGraphicsBeginPDFContex
tToFile:context的目的地为一个主目录下的PDF文档
PDF文档通过一种page-based结构来组织。这种结构的文件有下面两种限制:
1、在进行任何的绘制之前,必须一个打开的page,即是UIGraphicsBeginPDFPage已经被调用了
2、绘制之前必须指名每一页的大小
还有一个函数UIGraphicsBeginPDFPageWi
thInfo,这个函数也是可以创建一个PDF page。
不同的是UIGraphicsBeginPDFPage函数创建一个默认大小的page;
而UIGraphicsBeginPDFPageWi
thInfo允许对page的大小进行编辑,并且可以设置其它的一些属性
通过UIGraphicsEndContext函数,关闭context的绘制,这时候context中的数据被存储到对应的file或data中。
Drawing PDF Pages
在PDF context中绘画的时候,所有的绘画操作都被context捕捉,并被转为PDF命令。PDF命令被context捕捉之后转化为PDF 数据。
针对这部分,官方文档给出了两个函数:
Creating Links Within Your PDF Content
PDF link的作用不用多说,都是分为两种情况:
1、调到某一页
通过两个函数UIGraphicsAddPDFContextD
estinationAtPoint和UIGraphicsSetPDFContextD
estinationForRect,参数都是一个 能够唯一标识某一目的地的string和点坐标或者一个rect。
2、跳到某个URL
通过函数UIGraphicsSetPDFContextU
RLForRect,直接为某一个矩形范围内的部分设置对应的链接URL
针对Image的操作与PDF类似,有兴趣可以参考官方文档。 Print部分,暂时本人用不到,不再进行翻译,需要的评优可以参考官方文档:https://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/Printing/Printing.html#//apple_ref/doc/uid/TP40010156-CH12-SW1
1、通过UIGraphicsBeginPDFContex
2、通过UIGrapicsBeginPDFPage函数打开或者创建PDF的新一页,关闭之前的一页(如果之前一页被打开)
3、绘制内容
4、通过UIGraphicsEndPDFContext函数结束绘制
Creating and Configuring the PDF Context
创建一个PDF context的函数有两个:
UIGraphicsBeginPDFContex
UIGraphicsBeginPDFContex
不同的是UIGraphicsBeginPDFPage函数创建一个默认大小的page;
而UIGraphicsBeginPDFPageWi
Drawing PDF Pages
// Use Core Text to draw the text in a frame on the page. |
- (CFRange)renderPage:(NSInteger)pageNum withTextRange:(CFRange)currentRange |
andFramesetter:(CTFramesetterRef)framesetter |
{ |
// Get the graphics context. |
CGContextRef currentContext = UIGraphicsGetCurrentCont |
// Put the text matrix into a known state. This ensures |
// that no old scaling factors are left in place. |
CGContextSetTextMatrix(context, CGAffineTransformIdentit |
// Create a path object to enclose the text. Use 72 point |
// margins all around the text. |
CGRect frameRect = CGRectMake(72, 72, 468, 648); |
CGMutablePathRef framePath = CGPathCreateMutable(); |
CGPathAddRect(framePath, NULL, frameRect); |
// Get the frame that will do the rendering. |
// The currentRange variable specifies only the starting point. The framesetter |
// lays out as much text as will fit into the frame. |
CTFrameRef frameRef = CTFramesetterCreateFrame |
CGPathRelease(framePath); |
// Core Text draws from the bottom-left corner up, so flip |
// the current transform prior to drawing. |
CGContextTranslateCTM(currentContext, 0, 792); |
CGContextScaleCTM(currentContext, 1.0, -1.0); |
// Draw the frame. |
CTFrameDraw(frameRef, currentContext); |
// Update the current range based on what was drawn. |
currentRange = CTFrameGetVisibleStringR |
currentRange.location += currentRange.length; |
currentRange.length = 0; |
CFRelease(frameRef); |
return currentRange; |
} |
- (void)drawPageNumber:(NSInteger)pageNum |
{ |
NSString* pageString = [NSString stringWithFormat:@"Page %d", pageNum]; |
UIFont* theFont = [UIFont systemFontOfSize:12]; |
CGSize maxSize = CGSizeMake(612, 72); |
CGSize pageStringSize = [pageString sizeWithFont:theFont |
constrainedToSize:maxSize |
lineBreakMode:UILineBreakModeClip]; |
CGRect stringRect = CGRectMake(((612.0 - pageStringSize.width) / 2.0), |
720.0 + ((72.0 - pageStringSize.height) / 2.0) , |
pageStringSize.width, |
pageStringSize.height); |
[pageString drawInRect:stringRect withFont:theFont]; |
} |

针对Image的操作与PDF类似,有兴趣可以参考官方文档。 Print部分,暂时本人用不到,不再进行翻译,需要的评优可以参考官方文档:https://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/Printing/Printing.html#//apple_ref/doc/uid/TP40010156-CH12-SW1