IOS 绘制PDF

 -(void)createPdf:(UIImage *)img andText:(NSString *)text{
 2     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 3     NSString *saveDirectory = [paths objectAtIndex:0];
 4     NSString *saveFileName = @"myPDF.pdf";
 5     NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName];
 6     const char *filename = [newFilePath UTF8String];
 7     CGRect pageRect = CGRectMake(0, 0, 612, 792);
 8       //  This code block sets up our PDF Context so that we can draw to it
 9       
10      
11      CGContextRef pdfContext;
12     CFStringRef path;
13     CFURLRef url;
14     CFMutableDictionaryRef myDictionary = NULL;
15       //  Create a CFString from the filename we provide to this method when we call it
16       path = CFStringCreateWithCString (NULL, filename, kCFStringEncodingUTF8);
17       //  Create a CFURL using the CFString we just defined
18       url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
19     CFRelease (path);
20       //  This dictionary contains extra options mostly for ‘signing’ the PDF
21       myDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
22     CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
23     CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
24       //  Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary
25       pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
26       //  Cleanup our mess
27       CFRelease(myDictionary);
28     CFRelease(url);
29  
30       //  Done creating our PDF Context, now it’s time to draw to it
31        //  Starts our first page
32       CGContextBeginPage (pdfContext, &pageRect);
33     
34     UIImage* myUIImage = img;
35     CGImageRef pageImage = [myUIImage CGImage];
36     CGContextDrawImage(pdfContext, CGRectMake(0,0,([myUIImage size].width) , ([myUIImage size].height)), pageImage); //绘制图片
37       //  Draws a black rectangle around the page inset by 50 on all sides
38   //     CGContextStrokeRect(pdfContext, CGRectMake(50, 50, pageRect.size.width - 100, pageRect.size.height - 100));
39       
40        //  Adding some text on top of the image we just added
41   //     CGContextSelectFont (pdfContext, "Helvetica", 30, kCGEncodingMacRoman);
42       CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
43     CGContextSetRGBFillColor (pdfContext, 0, 0, 0, 1);
44 
45     
46 
47     UIGraphicsPushContext(pdfContext);  //将需要绘制的层push
48     CGContextTranslateCTM(pdfContext, 0, 792);  //转换Y轴坐标,  底层坐标与cocoa 组件不同 Y轴相反
49     CGContextScaleCTM(pdfContext, 1, -1);
50      
51   //     CGContextShowTextAtPoint (pdfContext, 260, 390, [text UTF8String], strlen([text UTF8String]));  // 汉字不正常
52  
53      [text drawAtPoint:CGPointMake(80, 80) withFont:[UIFont systemFontOfSize:18]];  //绘制汉字
54  
55   //     UIFont *font = [UIFont systemFontOfSize:15 ]; //自定义字体
56   //     CGContextSetFillColorWithColor(pdfContext, [UIColor blackColor].CGColor); //颜色
57   //     [text drawAtPoint:CGPointMake(260,390) forWidth:50 withFont:font minFontSize:8 actualFontSize:NULL lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignCenters];
58       
59      UIGraphicsPopContext(); 
60     
61     
62     CGContextStrokePath(pdfContext);
63      
64       //  End text
65        //  We are done drawing to this page, let’s end it
66        //  We could add as many pages as we wanted using CGContextBeginPage/CGContextEndPage
67       CGContextEndPage (pdfContext);
68       //  We are done with our context now, so we release it
69       CGContextRelease (pdfContext);
70  }


1 - (void)viewDidLoad
2 {
3     [super viewDidLoad];
4       //  Do any additional setup after loading the view, typically from a nib.
5       
6        // 调用方法
7       [self createPdf:[UIImage imageNamed:@"aa"] andText:@"汉字"];
8   
9  }


没注释掉的代码 每一行都有其意义. 注意步骤, 仔细阅读注释
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值