参考:
基本上,在iOS中生成PDF有5个步骤:
- 在Documents目录中创建一个新的PDF文件。
创建一个图形上下文。
- (void)setupPDFDocumentNamed:(NSString*)name Width:(float)width Height:(float)height { _pageSize = CGSizeMake(width, height); NSString *newPDFName = [NSString stringWithFormat:@"%@.pdf", name]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:newPDFName]; UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil); }
Begin a New Page
- (void)beginPDFPage { UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, _pageSize.width, _pageSize.height), nil); }
绘制内容
绘制文字
- (CGRect)addText:(NSString*)text withFrame:(CGRect)frame fontSize:(float)fontSize { UIFont *font = [UIFont systemFontOfSize:fontSize]; CGSize stringSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(_pageSize.width - 2*20-2*20, _pageSize.height - 2*20 - 2*20) lineBreakMode:UILineBreakModeWordWrap]; float textWidth = frame.size.width; if (textWidth < stringSize.width) textWidth = stringSize.width; if (textWidth > _pageSize.width) textWidth = _pageSize.width - frame.origin.x; CGRect renderingRect = CGRectMake(frame.origin.x, frame.origin.y, textWidth, stringSize.height); [text drawInRect:renderingRect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft]; frame = CGRectMake(frame.origin.x, frame.origin.y, textWidth, stringSize.height); return frame; }
绘制线条
- (CGRect)addLineWithFrame:(CGRect)frame withColor:(UIColor*)color { CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(currentContext, color.CGColor); // this is the thickness of the line CGContextSetLineWidth(currentContext, frame.size.height); CGPoint startPoint = frame.origin; CGPoint endPoint = CGPointMake(frame.origin.x + frame.size.width, frame.origin.y); CGContextBeginPath(currentContext); CGContextMoveToPoint(currentContext, startPoint.x, startPoint.y); CGContextAddLineToPoint(currentContext, endPoint.x, endPoint.y); CGContextClosePath(currentContext); CGContextDrawPath(currentContext, kCGPathFillStroke); return frame; }
绘制图片
- (CGRect)addImage:(UIImage*)image atPoint:(CGPoint)point { CGRect imageFrame = CGRectMake(point.x, point.y, image.size.width, image.size.height); [image drawInRect:imageFrame]; return imageFrame; }
结束文件
- (void)finishPDF { UIGraphicsEndPDFContext(); }
完整的调用过程:
- (IBAction)didClickMakePDF {
[self setupPDFDocumentNamed:@"NewPDF" Width:850 Height:1100];
[self beginPDFPage];
CGRect textRect = [self addText:@"This is some nice text here, don't you agree?"
withFrame:CGRectMake(kPadding, kPadding, 400, 200) fontSize:48.0f];
CGRect blueLineRect = [self addLineWithFrame:CGRectMake(kPadding, textRect.origin.y + textRect.size.height + kPadding, _pageSize.width - kPadding*2, 4)
withColor:[UIColor blueColor]];
UIImage *anImage = [UIImage imageNamed:@"tree.jpg"];
CGRect imageRect = [self addImage:anImage
atPoint:CGPointMake((_pageSize.width/2)-(anImage.size.width/2), blueLineRect.origin.y + blueLineRect.size.height + kPadding)];
[self addLineWithFrame:CGRectMake(kPadding, imageRect.origin.y + imageRect.size.height + kPadding, _pageSize.width - kPadding*2, 4)
withColor:[UIColor redColor]];
[self finishPDF];
}
参考此教程来浏览PDF文件:
Reading & Displaying PDF Documents
最终的效果: