【发布时间】:2014-08-11 00:07:56
【问题描述】:
目前我正在开发一个费用管理应用程序。在这个应用程序中,我想从本地数据库中检索数据并以表格格式为它制作一个 pdf 文件。需要有关如何继续执行此操作的指导。
谢谢。
【问题讨论】:
目前我正在开发一个费用管理应用程序。在这个应用程序中,我想从本地数据库中检索数据并以表格格式为它制作一个 pdf 文件。需要有关如何继续执行此操作的指导。
谢谢。
【问题讨论】:
您可以按照以下步骤在 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);
}