如何生成多页ios7的pdf?

时间:2014-08-08 10:37:27

标签: ipad ios7 pdf-generation ios7.1 xcode5.0.1

根据我的应用程序,我创建了Pdf文件但是我需要生成多页的Pdf?这是我用来生成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);

//开始新页面。

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

//为标题绘制文字。

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


}

1 个答案:

答案 0 :(得分:1)

您可以在ios中看到此链接以生成pdf

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

同样在您的代码中使用[数组计数+ 1]作为行数