在objective-c中创建多页PDF

时间:2013-05-07 08:02:29

标签: iphone ios objective-c pdf uigraphicscontext

我正在尝试创建多页PDF。我遵循了这个tutorial。 这适用于静态文本的XIB文件,然后从代码中添加一个表。但我有ATM的问题是当桌子比一个页面更大时。当表有超过9行时。它应该在下一页继续。

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

+(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页。其他一切都适用于一页。

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

有人能帮助我吗?

3 个答案:

答案 0 :(得分:16)

创建第二页时不应再次调用UIGraphicsBeginPDFContextToFile(), 仅UIGraphicsBeginPDFPageWithInfo()

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

答案 1 :(得分:1)

NSArray *imageArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"3.png"], [UIImage imageNamed:@"4.png"], [UIImage imageNamed:@"5.png"], [UIImage imageNamed:@"3.png"], nil];



NSMutableData *pdfFile = [[NSMutableData alloc] init];
double pageWidth = 0.0;
double pageHeight = 0.0;
UIImage *image;
for (int i = 0; i < [imageArray count]; i++)
{
    image =[UIImage imageWithCGImage:[imageArray[i] CGImage]];
    pageWidth = pageWidth + image.size.width ;
    pageHeight = pageHeight + image.size.height;
}
image =[UIImage imageWithCGImage:[imageArray[0] CGImage]];
CGRect rect;
rect = CGRectMake(0, 0,image.size.width ,image.size.height);
UIGraphicsBeginPDFContextToData(pdfFile, CGRectZero, nil);
for (int i = 0; i < [imageArray count] ; i++)
{
    UIGraphicsBeginPDFPageWithInfo(rect, nil);
    UIImage *contextImage = imageArray[i];
    [contextImage drawInRect:rect];
}
UIGraphicsEndPDFContext();

// save PDF file
NSString *saveFileName = [NSString stringWithFormat:@"%@%fx%f.pdf", @"test", pageWidth, pageHeight];

NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* savePath = [documentDirectory stringByAppendingPathComponent:saveFileName];

if([[NSFileManager defaultManager] fileExistsAtPath:savePath])
{
    [[NSFileManager defaultManager] removeItemAtPath:savePath error:nil];
}
[pdfFile writeToFile: savePath atomically: YES];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"PDF File created and saved successfully." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];

答案 2 :(得分:0)

您不需要为第二页(UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil))创建上下文,您只需创建此页面。 您还应该记住使用CGPDFContextEndPage()关闭打开的页面。