iOS的创建PDF的步骤

今天的项目中需要在PDF的中创建一些表格和雷达图。

先放最后的效果图:



1、先说雷达图,有两个比较好用的分别是BTSpiderPlotterViewJYRadarChart

这两个库的使用都非常简单,属于那种一看就会的,详情就不说了。

代码:

	radarChartView = [[JYRadarChart alloc] initWithFrame:CGRectMake(0, 220, 320, 200)];
	radarChartView.showLegend = YES;
//	[radarChartView setTitles:@[@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j"]];
	radarChartView.drawPoints = YES;
	radarChartView.attributes = @[@"展厅", @"客休区", @"精品展示区", @"服务部办公室", @"服务前台", @"公示墙",
	                  @"接车通道", @"调度室", @"维修车间", @"备件部", @"待交区", @"新车交车区", @"收银处", @"客户运营中心", @"会议室", @"酒店"];
	NSArray *b1 = @[@(100), @(50), @(100), @(78), @(80), @(100), @(78), @(100), @(100), @(100), @(100), @(100), @(100), @(50), @(70), @(50)];
	radarChartView.dataSeries = @[b1];
    [radarChartView setColors:@[[UIColor greenColor]]];
	radarChartView.steps = 5;
	radarChartView.backgroundColor = [UIColor clearColor];
	[self.view addSubview:radarChartView];
注意数组的数量统一就好。

效果:

2、截图保存

建立好雷达图之后

if ([radarChartView isKindOfClass:NSClassFromString(@"JYRadarChart")]) {//找到自己需要的subView
        NSLog(@"fund");
        //支持retina高分的关键
        if(UIGraphicsBeginImageContextWithOptions != NULL)
        {
            UIGraphicsBeginImageContextWithOptions(radarChartView.frame.size, NO, 0.0);
        } else {
            UIGraphicsBeginImageContext(radarChartView.frame.size);
        }
        
        //获取图像
        [radarChartView.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        //保存图像
        
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *path = [paths objectAtIndex:0];
        NSString *filepath = [path stringByAppendingString:@"/0.png"];
        if ([UIImagePNGRepresentation(image) writeToFile:filepath atomically:YES]) {
            //                index += 1;
            NSLog(@"Succeeded! %@",filepath);
            [self createPdf];
        }
        else {
            NSLog(@"Failed!");
        }
    }


3、创建PDF
然后就是需要把之前的图片加到PDF里面

-(void)createPdf{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *saveDirectory = [paths objectAtIndex:0];
    NSString *saveFileName = @"myPDF.pdf";
    NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName];
    const char *filename = [newFilePath UTF8String];
    CGRect pageRect=CGRectMake(0, 0, 1099, 714);
    
    // This code block sets up our PDF Context so that we can draw to it
//    CGContextRef pdfContext;
    CFStringRef path;
    CFURLRef url;
    CFMutableDictionaryRef myDictionary = NULL;
    // Create a CFString from the filename we provide to this method when we call it
    path = CFStringCreateWithCString (NULL, filename,
                                      kCFStringEncodingUTF8);
    // Create a CFURL using the CFString we just defined
    url = CFURLCreateWithFileSystemPath (NULL, path,
                                         kCFURLPOSIXPathStyle, 0);
    CFRelease (path);
    // This dictionary contains extra options mostly for ‘signing’ the PDF
    myDictionary = CFDictionaryCreateMutable(NULL, 0,
                                             &kCFTypeDictionaryKeyCallBacks,
                                             &kCFTypeDictionaryValueCallBacks);
    CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
    CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
    // Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary
    pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
    // Cleanup our mess
    CFRelease(myDictionary);
    CFRelease(url);
    // Done creating our PDF Context, now it’s time to draw to it
    // Starts our first page
    CGContextBeginPage (pdfContext, &pageRect);
    // Draws a black rectangle around the page inset by 50 on all sides
//    CGContextStrokeRect(pdfContext, CGRectMake(50, 50, pageRect.size.width, pageRect.size.height));
    // Adding some text on top of the image we just added
    
    
    //release
    CGContextEndPage (pdfContext);
    CGContextRelease (pdfContext);
}
这里就是创建PDF的代码。

4、在PDF上面写字或者绘图

绘图:

    UIImage *image = [UIImage imageWithContentsOfFile:[saveDirectory stringByAppendingString:@"/0.png"]];
    CGContextDrawImage(pdfContext, CGRectMake(623, 412, 465, 272), image.CGImage);//在坐标中画出图片
    CGContextSetRGBStrokeColor(pdfContext,0,0,0,0.2);//画笔线的颜色
    CGContextStrokeRect(pdfContext,CGRectMake(623, 412, 465, 272));//画方框
写字(这里我写了个方法,方便重复调用):

- (void)addText:(NSString *)text x:(CGFloat )x y:(CGFloat)y{
    // Prepare font
    CGFloat s = 15;
    CTFontRef ctfont = CTFontCreateWithName(CFSTR("STHeitiSC-Medium"), s, NULL);
    CGColorRef ctColor = [[UIColor blackColor] CGColor];
    
    // Create an attributed string
    CFStringRef keys[] = { kCTFontAttributeName,kCTForegroundColorAttributeName };
    CFTypeRef values[] = { ctfont,ctColor};
    CFDictionaryRef attr = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values,
                                              sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    
    
    CFStringRef ctStr = CFStringCreateWithCString(nil, [text UTF8String], kCFStringEncodingUTF8);
    CFAttributedStringRef attrString = CFAttributedStringCreate(NULL,ctStr, attr);
    CTLineRef line = CTLineCreateWithAttributedString(attrString);
    CGContextSetTextMatrix(pdfContext, CGAffineTransformIdentity);
    //CGContextSetTextMatrix(context, CGAffineTransformMakeRotation(3.14));
    //CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0)); //Use this one if the view's coordinates are flipped
    CGContextSetTextPosition(pdfContext, x, y);
    CTLineDraw(line, pdfContext);
    
    CFRelease(line);
    CFRelease(attrString);
    CFRelease(ctStr);
    

    // Clean up
    CFRelease(attr);
    CFRelease(ctfont);
    

}

大功告成,其实这个PDF使用上下文的方式创建,然后保存为PDF而已,所以平常CG的绘图方法基本都能够用

另外就是写文字的部分,CGContextShowTextAtPoint是不能绘中文的,所以必须用CoreText的方法。

最后说一句,你们看到表格模糊是因为我偷懒,直接PS了一张png作为背景,所以看起来有点模糊,各位勤奋的同学还是自己一点一点绘比较好,也可以练习一下怎么写CG


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值