UIGraphicsBeginPDFPageWithInfo用来绘制PDF文档,可以绘制文字和图片。
//PDF文件保存路径
NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pdfPath = [[documentPath firstObject] stringByAppendingString:@"/newPDF.pdf"];
NSLog(@"%@", pdfPath);
//开启PDF图形上下文
/*
path 文件路径
bounds PDF绘制尺寸,设置为CGRectZero则使用默认值612*912
documentInfo PDF文档信息
*/
UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, [NSDictionary dictionaryWithObjectsAndKeys:@"MacPen",kCGPDFContextAuthor, nil]);
//PDF文档是分页的,开启一页文档开始绘制
UIGraphicsBeginPDFPage();
//绘制文字
NSString *content1 = @"WONG! !";
NSMutableParagraphStyle *style1 = [[NSMutableParagraphStyle alloc]init];
NSTextAlignment align1 = NSTextAlignmentLeft;
style1.alignment = align1;
[content1 drawInRect:CGRectMake(56, 120, 300, 50) withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Marker Felt" size:35],NSParagraphStyleAttributeName:style1}];
NSString *content = @"WONG WONG WONG!!";
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
NSTextAlignment align = NSTextAlignmentLeft;
style.alignment = align;
[content drawInRect:CGRectMake(56, 180, 300, 50) withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Marker Felt" size:30],NSParagraphStyleAttributeName:style}];
//绘制图片
UIImage *image = [UIImage imageNamed:@"dog_PNG2454.png"];
[image drawInRect:CGRectMake(200, 390, 233, 400)];
//关闭PDF图形上下文
UIGraphicsEndPDFContext();