【问题标题】:Creating additional pages in a PDF from Core Data Information根据核心数据信息在 PDF 中创建附加页面
【发布时间】:2013-12-06 12:10:58
【问题描述】:

目前,我有一个工作系统,可以愉快地生成一个 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) 如果核心数据有一个条目,它从顶部开始。如果它有两个或更多条目,则它从下面的位置 50.0 开始。我理解为什么会发生这种情况,因为我是这样实现的,但是如何让文本出现在顶部,即使它超过 1 个条目。

对此的任何想法将不胜感激。

【问题讨论】:

标签: ios objective-c pdf text


【解决方案1】:

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; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 2013-02-20
    相关资源
    最近更新 更多