如何在iOS中的PDF文件中绘制表格。

问题描述 投票:1回答:2

目前我正在开发一个费用管理应用程序。在这个应用程序中,我想从本地数据库中获取反向数据,并以表格格式为它制作一个pdf文件。指导我需要如何继续这样做。

谢谢。

ios iphone
2个回答
2
投票

您可以按照以下步骤创建PDF格式的表格:

步骤1.创建UIGraphics PDF上下文:

UIGraphicsBeginPDFContextToFile(filePath, CGRectZero, nil);
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kPageWidth, kPageHeight), nil);
[self drawTableAt:CGPointMake(6,38) withRowHeight:30 andColumnWidth:120 andRowCount:kRowsPerPage andColumnCount:5];
UIGraphicsEndPDFContext();

步骤2.将这两个辅助函数添加到同一个文件(或同一个类实现):

-(void)drawTableAt: (CGPoint)origin  withRowHeight: (int)rowHeight  andColumnWidth: (int)columnWidth  andRowCount: (int)numberOfRows  andColumnCount:(int)numberOfColumns{

    for (int i = 0; i <= numberOfRows; i++) {
        int newOrigin = origin.y + (rowHeight*i);
        CGPoint from = CGPointMake(origin.x, newOrigin);
        CGPoint to = CGPointMake(origin.x + (numberOfColumns*columnWidth), newOrigin);
        [self drawLineFromPoint:from toPoint:to];
    }

    for (int i = 0; i <= numberOfColumns; i++) {
        int newOrigin;
        if(i==0){
            newOrigin = origin.x ;
        }
        if(i==1){
            newOrigin = origin.x + 30;
        }
        if(i==2){
            newOrigin = origin.x + 150;
        }
        if(i==3){
            newOrigin = origin.x + 270;
        }
        if(i==4){
            newOrigin = origin.x + 480;
        }
//        newOrigin = origin.x + (columnWidth*i);
        CGPoint from = CGPointMake(newOrigin, origin.y);
        CGPoint to = CGPointMake(newOrigin, origin.y +(numberOfRows*rowHeight));
        [self drawLineFromPoint:from toPoint:to];
    }
}

-(void)drawLineFromPoint:(CGPoint)from toPoint:(CGPoint)to
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.0);
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGFloat components[] = {0.2, 0.2, 0.2, 0.3};
    CGColorRef color = CGColorCreate(colorspace, components);

    CGContextSetStrokeColorWithColor(context, color);
    CGContextMoveToPoint(context, from.x, from.y);
    CGContextAddLineToPoint(context, to.x, to.y);

    CGContextStrokePath(context);
    CGColorSpaceRelease(colorspace);
    CGColorRelease(color);
}

© www.soinside.com 2019 - 2024. All rights reserved.