在objective-c中创建多页PDF [英] Create multi page PDF in objective-c

查看:131
本文介绍了在objective-c中创建多页PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建多页PDF。我已经按照教程
这是用于静态文本的XIB文件,然后从代码中添加一个表。但我有ATM的问题是,当表格大于一页时。当表有超过9行时。它应该在下一页继续。

I am trying to create a multipage PDF. I have followed this tutorial. This is working with a XIB file for static text and then adds a table from code. But the problem I'm having ATM is that when the table is bigger then one page. When the table has more then 9 rows. It should continue on the next page.

这就是我在代码中所做的。

This is what I'm doing in code.

+(void)drawPDF:(NSString*)fileName
{
    NSMutableDictionary *mutDictValues = [[[NSUserDefaults standardUserDefaults] objectForKey:@"dicValues"] mutableCopy];
    NSMutableArray *arrSelectedCities = [[mutDictValues objectForKey:@"cities"]mutableCopy ];

    if(arrSelectedCities.count <= 8){
        // If there are only 8 rows --> we can fit everyting on one page !

        // Create the PDF context using the default page size of 612 x 792.
        UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil);
        // Mark the beginning of a new page.
        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

        [self drawLabels];
        [self drawLogo];

        int xOrigin = 50;
        int yOrigin = 350;

        int rowHeight = 50;
        int columnWidth = 240;

        int numberOfRows = 9;
        int numberOfColumns = 2;

        [self drawTableAt:CGPointMake(xOrigin, yOrigin) withRowHeight:rowHeight andColumnWidth:columnWidth andRowCount:numberOfRows andColumnCount:numberOfColumns];

        [self drawTableDataAt:CGPointMake(xOrigin, yOrigin) withRowHeight:rowHeight andColumnWidth:columnWidth andRowCount:numberOfRows andColumnCount:numberOfColumns withArray:arrSelectedCities];

        // Close the PDF context and write the contents out.
        UIGraphicsEndPDFContext();

    }else{
        // When we have more then 8 rows we should have 2 pages.
        NSLog(@"Create 2 pages");
        // Create the PDF context using the default page size of 612 x 792.
        UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil);
        // Mark the beginning of a new page.
        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

        [self drawLabels];
        [self drawLogo];

        int xOrigin = 50;
        int yOrigin = 350;

        int rowHeight = 50;
        int columnWidth = 240;

        int numberOfRows = 9;
        int numberOfColumns = 2;

        [self drawTableAt:CGPointMake(xOrigin, yOrigin) withRowHeight:rowHeight andColumnWidth:columnWidth andRowCount:numberOfRows andColumnCount:numberOfColumns];

        [self drawTableDataAt:CGPointMake(xOrigin, yOrigin) withRowHeight:rowHeight andColumnWidth:columnWidth andRowCount:numberOfRows andColumnCount:numberOfColumns withArray:arrSelectedCities];


        // Create the PDF context using the default page size of 612 x 792.
        UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil);
        // Mark the beginning of a new page.
        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);


        int xOrigin2 = 50;
        int yOrigin2 = 60;
        int numberOfRows2 = ((arrSelectedCities.count+1)-9);

        [self drawTableAt:CGPointMake(xOrigin2, yOrigin2) withRowHeight:rowHeight andColumnWidth:columnWidth andRowCount:numberOfRows2 andColumnCount:numberOfColumns];

        [self drawTableDataAt:CGPointMake(xOrigin2, yOrigin2) withRowHeight:rowHeight andColumnWidth:columnWidth andRowCount:numberOfRows2 andColumnCount:numberOfColumns withArray:arrSelectedCities];


    }
    // Close the PDF context and write the contents out.
    UIGraphicsEndPDFContext();
}

让我解释一下我在这里做的事情。我有一个数组应该填满我的tableview。如果数组大于8那么我应该使用2页。否则一切都适用于一页。

Let me explain what I'm doing here. I have an array that should fill up my tableview. If the array is bigger then 8 then I should use 2 pages. Else everything works with one page.

这是做什么的,它只创建第二页....

What this does is, it's creating only the second page....

任何人都可以帮助我吗?

Can anybody help me?

推荐答案

创建第二个时,不应再次调用 UIGraphicsBeginPDFContextToFile() page,
only UIGraphicsBeginPDFPageWithInfo()

You should not call UIGraphicsBeginPDFContextToFile() again when creating the second page, only UIGraphicsBeginPDFPageWithInfo():

UIGraphicsBeginPDFContextToFile(...); 
UIGraphicsBeginPDFPageWithInfo(...); // start first page
// ...
UIGraphicsBeginPDFPageWithInfo(...); // start second page
// ...
UIGraphicsEndPDFContext();

这篇关于在objective-c中创建多页PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆