生成PDF文件
来源:互联网 发布:淘宝怎样推广店铺爆款 编辑:程序博客网 时间:2024/05/17 22:43
参考:
- Generating PDF Documents
- Apple documentation
- Generating PDF Content
基本上,在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 lineCGContextSetLineWidth(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
最终的效果:
0 0
- c#生成pdf文件
- PDF文件的生成
- Jasper生成PDF文件
- PDF文件的生成
- JAVA生成PDF文件
- python生成pdf文件
- java生成PDF文件
- java生成PDF文件
- android 生成pdf文件
- 生成PDF文件
- 生成pdf文件
- PHP生成PDF文件
- java 生成pdf文件
- Itext生成PDF文件
- java生成PDF文件
- java生成pdf文件
- java生成PDF文件
- 生成pdf文件
- 线性素数筛法
- 查看linux 系统 服务器型号
- Java I/O流
- 视频播放入门1
- 玩玩三维重建
- 生成PDF文件
- IOS8开启GPS
- MediaPlayer的状态机
- 《高效能人士的七个习惯》:有图有真相
- 圆盘抽奖的界面简单实现
- UVa1599 - Ideal Path
- 一般的英语作用
- 【每日一题】字符串是否包含问题
- 微信扫码支付asp.net(C#)实现步骤