天天看点

iOS开发之Quartz2D生成PDF-Part1

iOS开发之Quartz2D生成PDF-Part1

使用iOS的Quartz2D 生成PDF文件

首先创建一个工程,导入CoreText.framework框架。在

PDFViewController.h

文件中引入CoreText文件

PDFViewController.m

文件中创建一个新方法:

-(void)drawText
{
    NSString* fileName = @"Invoice.PDF";

    NSArray *arrayPaths =
    NSSearchPathForDirectoriesInDomains(
                                        NSDocumentDirectory,
                                        NSUserDomainMask,
                                        YES);
    NSString *path = [arrayPaths objectAtIndex:];
    NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];

    NSString* textToDraw = @"Hello World";
    CFStringRef stringRef = (__bridge CFStringRef)textToDraw;

    // 使用 Core Text Framesetter.
    CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);

    CGRect frameRect = CGRectMake(, , , );
    CGMutablePathRef framePath = CGPathCreateMutable();
    CGPathAddRect(framePath, NULL, frameRect);

    // 获取将要被渲染的帧
    CFRange currentRange = CFRangeMake(, );
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
    CGPathRelease(framePath);

    // 创建一个PDF的上下文,页面大小默认为 612 x 792
    UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);

    // 标记新页面的开头
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(, , , ), nil);

    // 获取上下文.
    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    // 把文字变成矩阵已知状态。这将确保没有旧缩放因子被留在原处。
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);

    //文本坐标翻转
    CGContextTranslateCTM(currentContext, , );
    CGContextScaleCTM(currentContext, , -);

    // 绘制帧
    CTFrameDraw(frameRef, currentContext);

    CFRelease(frameRef);
    CFRelease(stringRef);
    CFRelease(framesetter);

    //关闭PDF上下文
    UIGraphicsEndPDFContext();

}
           

此方法有点长,在此分段解释下:

在沙盒目录下创建PDF文件,并命名为

Invoice.pdf

NSString* fileName = @"Invoice.PDF";

NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
                                    NSDocumentDirectory,
                                    NSUserDomainMask,
                                    YES);
NSString *path = [arrayPaths objectAtIndex:];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
           

下面的代码块是创建一个已“Hello,world”为内容的PDF,将string转换为CFStringRef格式

NSString* textToDraw = @"Hello World";
CFStringRef stringRef = (__bridge CFStringRef)textToDraw;

CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
           

然后是创建一个CGRect用于定义文本的大小

CGRect frameRect = CGRectMake(, , , );
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);

CFRange currentRange = CFRangeMake(, );
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);
           

接下来,我们创建一个PDF上下文和标记的PDF的起始页。 PDF的每个页面都有开始并调用UIGraphicsBeginPDFPageWithInfo 。

UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);

UIGraphicsBeginPDFPageWithInfo(CGRectMake(, , , ), nil);

CGContextRef currentContext = UIGraphicsGetCurrentContext();
           

由于Core Graphics 的坐标是从左下角开始,而UIKit的坐标是从左上角开始,所以需做一个变换:

CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);

CGContextTranslateCTM(currentContext, , );
CGContextScaleCTM(currentContext, , -);
           

我们绘制文本实际帧,释放Core Graphics对象,并关闭PDF上下文至此PDF写入到沙盒目录下。

CTFrameDraw(frameRef, currentContext);

CFRelease(frameRef);
CFRelease(stringRef);
CFRelease(framesetter);

UIGraphicsEndPDFContext();
           

使用UIWebView展示PDF文件

PDFViewController.m

中的

drawText

方法后添加下面方法:

-(void)showPDFFile
{
    NSString* fileName = @"Invoice.PDF";

    NSArray *arrayPaths =
    NSSearchPathForDirectoriesInDomains(
                                        NSDocumentDirectory,
                                        NSUserDomainMask,
                                        YES);
    NSString *path = [arrayPaths objectAtIndex:];
    NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];

    UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(, , , )];

    NSURL *url = [NSURL fileURLWithPath:pdfFileName];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView setScalesPageToFit:YES];
    [webView loadRequest:request];

    [self.view addSubview:webView];    
}
           

viewDidLoad

中添加

- (void)viewDidLoad
{
    [self drawText];
    [self showPDFFile];

    [super viewDidLoad];
}