Search code examples
ipadios7pdf-generationios7.1xcode5.0.1

How to generate a pdf with multiple pages ios7?


According to my application i have created Pdf file but i need to generate Pdf with multiple pages?This is the code which i used for generating pdf.

-(void)drawPDF

{
pagesize = CGSizeMake(612, 792);

NSString *fileName = @"Report.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[self generatePdfWithFilePath:pdfFileName];
}

-(void) generatePdfWithFilePath: (NSString *)thefilePath
{

UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);

//Start a new page.

UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pagesize.width, pagesize.height), nil);

//Draw text fo our header.

CGContextRef    currentContext = UIGraphicsGetCurrentContext();

CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);

UIFont *font = [UIFont systemFontOfSize:14.0];

int xOrigin = 20;
int yOrigin = 300;

int rowHeight = 50;
int columnWidth = 140;

int numberOfRows = 7;
int numberOfColumns = 4;

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

[self drawImageAt:CGPointMake(xOrigin, yOrigin) withRowHeight:rowHeight andColumnWidth:columnWidth];

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

// [self drawTextAt:CGPointMake(xOrigin, yOrigin) withRowHeight:rowHeight andColumnWidth:columnWidth];

CGSize stringSize = [contents_for_pdf sizeWithFont:font constrainedToSize:CGSizeMake(pagesize.width - 2*kBorderInset-2*kMarginInset, pagesize.height - 2*kBorderInset - 2*kMarginInset) lineBreakMode:NSLineBreakByWordWrapping];

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

[contents_for_pdf drawInRect:renderingRect withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];

UIGraphicsEndPDFContext();


}

Solution

  • You can see this link for generating pdf in ios

    http://www.raywenderlich.com/6818/how-to-create-a-pdf-with-quartz-2d-in-ios-5-tutorial

    Also in your code use your [array count+1] for number of rows