从核心数据信息创建PDF中的其他页面

时间:2013-12-06 12:10:59

标签: ios objective-c pdf text

目前,我有一个工作系统可以快速生成一个PDF页面,其中包含填充整个PDF的边框,标题和核心数据信息。它的效果非常好,除非我有更多的记录可以放在页面上。它不是自动创建新页面,而是离开页面。

我非常感谢能够从第一页自动创建第二页(具有相同的边框,标题等)来处理其他数据。

我创建文本,边框和标题的代码如下:

- (void) drawLine
{
    CGContextRef    currentContext = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(currentContext, kLineWidth);

    CGContextSetStrokeColorWithColor(currentContext, [UIColor blueColor].CGColor);

    CGPoint startPoint = CGPointMake(kMarginInset + kBorderInset, kMarginInset + kBorderInset + 40.0);
    CGPoint endPoint = CGPointMake(pageSize.width - 4*kMarginInset -4*kBorderInset, kMarginInset + kBorderInset + 40.0);

    CGContextBeginPath(currentContext);
    CGContextMoveToPoint(currentContext, startPoint.x, startPoint.y);
    CGContextAddLineToPoint(currentContext, endPoint.x, endPoint.y);

    CGContextClosePath(currentContext);
    CGContextDrawPath(currentContext, kCGPathFillStroke);
}

- (void) drawBorder
{
    CGContextRef    currentContext = UIGraphicsGetCurrentContext();
    UIColor *borderColor = [UIColor brownColor];

    CGRect rectFrame = CGRectMake(kBorderInset, kBorderInset, pageSize.width-kBorderInset*2, pageSize.height-kBorderInset*2);

    CGContextSetStrokeColorWithColor(currentContext, borderColor.CGColor);
    CGContextSetLineWidth(currentContext, kBorderWidth);
    CGContextStrokeRect(currentContext, rectFrame);
}

- (void) drawText
{

     NSManagedObjectContext *managedObjectContext = [self managedObjectContext];

     NSFetchRequest *pdfFetchRequest = [[NSFetchRequest alloc] init];
     NSEntityDescription *entity = [NSEntityDescription entityForName:@"Transaction" inManagedObjectContext:managedObjectContext];
     pdfFetchRequest.entity = entity;

     NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"dates.dateOfEvent" ascending:NO];

     pdfFetchRequest.sortDescriptors = [NSArray arrayWithObject:sort];
     pdfFetchRequest.fetchBatchSize = 20;

     pdfFetchRequest.predicate = [NSPredicate predicateWithFormat:@"whoBy = %@", self.person];

     NSError *error = nil;
     NSArray *types = [managedObjectContext executeFetchRequest:pdfFetchRequest error:&error];


    CGContextRef    currentContext = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);
    UIFont *font = [UIFont systemFontOfSize:14.0];

    CGSize textSize = CGSizeMake(pageSize.width - 5*kBorderInset-5*kMarginInset, pageSize.height - 5*kBorderInset - 5*kMarginInset);

    CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset + 50.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, textSize.height);



    for (Transaction *trans in types)
    {


        if ([types count] == 1)
        {
            NSString *amount = trans.item.amount; 
            NSString *occasions = trans.occasion.title;
            NSString *textToDraw = [NSString stringWithFormat:@"Occasion = %@, Amount = %@ ", occasions, amount];

            [textToDraw drawWithRect:renderingRect options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];

        }

        if ([types count] > 1)
        {
            NSString *amount = trans.item.amount; 
            NSString *occasions = trans.occasion.title;
            NSString *textToDraw = [NSString stringWithFormat:@"Occasion = %@, Amount = %@ ", occasions, amount];

            renderingRect.origin.y += 50.0;

            [textToDraw drawWithRect:renderingRect options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
        }
    }
}

因此有两个问题:

1)当文本在PDF页面中达到一定高度时,如何让PDF创建第二页?我想我必须创建一些递增的数字,但我不知道我会做什么以及我该怎么做!

2)如果Core数据有一个条目,则从顶部开始。如果它有两个或更多条目,则从50.0以下的位置开始。我理解为什么会发生这种情况,因为我以这种方式实现了它,但是如何将文本显示在顶部,即使它超过1个条目。

对此的任何想法都将受到大力赞赏。

1 个答案:

答案 0 :(得分:0)

Ad.1 if (ypos >= maxY) { // create a new page and reset the current page's Y value UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil); ypos=30; }