生成PDF文件

参考:

基本上,在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 line
    CGContextSetLineWidth(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

最终的效果:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值