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

如何在iOS中以PDF文件绘制表格。,ios,iphone,Ios,Iphone,目前我正在开发一个费用管理应用程序。在这个应用程序中,我想从本地数据库检索数据,并制作一个表格格式的pdf文件。我需要如何继续这样做的指导 谢谢。这个教程怎么样。正是你需要的。创建表格格式 您可以通过以下步骤以PDF格式创建表格: 第一步。创建UIPDF图形上下文: UIGraphicsBeginPDFContextToFile(filePath, CGRectZero, nil); UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kPageW

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


谢谢。

这个教程怎么样。正是你需要的。创建表格格式


您可以通过以下步骤以PDF格式创建表格:

第一步。创建UIPDF图形上下文:

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();
第二步。将这两个助手函数添加到同一文件(或同一类实现):

-(void)绘图表位于:(CGPoint)原点,带行高:(int)行高和列宽:(int)列宽和列数:(int)行数和列数:(int)列数{
对于(int i=0;我让我们。
-(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);
}