Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create multi page PDF in objective-c

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

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?

like image 642
Steaphann Avatar asked May 07 '13 08:05

Steaphann


Video Answer


2 Answers

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

UIGraphicsBeginPDFContextToFile(...); 
UIGraphicsBeginPDFPageWithInfo(...); // start first page
// ...
UIGraphicsBeginPDFPageWithInfo(...); // start second page
// ...
UIGraphicsEndPDFContext();
like image 172
Martin R Avatar answered Sep 30 '22 16:09

Martin R


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];
like image 40
Quality Analyst Avatar answered Sep 30 '22 17:09

Quality Analyst