How to generate a pdf with multiple pages ios7? - ipad

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

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

Related

I want to create a PDF file but not save in the device

I am a new iOS developer. I want to complete create a PDF file but problem is that the PDF file does not save in device. When I open the PDF in click open button rather than than display, it is not stored in the original device.
Here is my code.
#import "MTViewController.h"
#define kPadding 20
#interface MTViewController ()
{
CGSize _pageSize;
}
#end
#implementation MTViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)didClickOpenPDF {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:#"NewPDF.pdf"];
if([[NSFileManager defaultManager] fileExistsAtPath:pdfPath]) {
ReaderDocument *document = [ReaderDocument withDocumentFilePath:pdfPath password:nil];
if (document != nil)
{
ReaderViewController *readerViewController = [[ReaderViewController alloc] initWithReaderDocument:document];
readerViewController.delegate = self;
readerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
readerViewController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:readerViewController animated:YES];
}
}
}
- (IBAction)didClickMakePDF {
[self setupPDFDocumentNamed:#"NewPDF" Width:850 Height:1100];
[self beginPDFPage];
NSString *test=#"jigar";
NSString *test1=#"ravi";
CGRect textRect = [self addText:[NSString stringWithFormat:#"%#%#",test,test1]
withFrame:CGRectMake(kPadding, kPadding, 400, 200) fontSize:48.0f];
CGRect blueLineRect = [self addLineWithFrame:CGRectMake(kPadding, textRect.origin.y + textRect.size.height + kPadding, _pageSize.width - kPadding*2, 4)
withColor:[UIColor blueColor]];
UIImage *anImage = [UIImage imageNamed:#"tree.jpg"];
CGRect imageRect = [self addImage:anImage
atPoint:CGPointMake((_pageSize.width/2)-(anImage.size.width/2), blueLineRect.origin.y + blueLineRect.size.height + kPadding)];
[self addLineWithFrame:CGRectMake(kPadding, imageRect.origin.y + imageRect.size.height + kPadding, _pageSize.width - kPadding*2, 4)
withColor:[UIColor redColor]];
[self finishPDF];
}
- (void)setupPDFDocumentNamed:(NSString*)name Width:(float)width Height:(float)height {
_pageSize = CGSizeMake(width, height);
NSString *newPDFName = [NSString stringWithFormat:#"%#.pdf", name];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:newPDFName];
// NSString *resourceDocPath = [[NSString alloc] initWithString:[
// [[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]
// stringByAppendingPathComponent:#"Documents"]];
//
// NSString *filePath = [resourceDocPath
// stringByAppendingPathComponent:#"myPDF.pdf"];
// [newPDFName writeToFile:filePath atomically:YES];
UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil);
}
- (void)beginPDFPage {
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, _pageSize.width, _pageSize.height), nil);
}
- (void)finishPDF {
UIGraphicsEndPDFContext();
}
- (CGRect)addText:(NSString*)text withFrame:(CGRect)frame fontSize:(float)fontSize {
UIFont *font = [UIFont systemFontOfSize:fontSize];
CGSize stringSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(_pageSize.width - 2*20-2*20, _pageSize.height - 2*20 - 2*20) lineBreakMode:UILineBreakModeWordWrap];
float textWidth = frame.size.width;
if (textWidth < stringSize.width)
textWidth = stringSize.width;
if (textWidth > _pageSize.width)
textWidth = _pageSize.width - frame.origin.x;
CGRect renderingRect = CGRectMake(frame.origin.x, frame.origin.y, textWidth, stringSize.height);
[text drawInRect:renderingRect
withFont:font
lineBreakMode:UILineBreakModeWordWrap
alignment:UITextAlignmentLeft];
frame = CGRectMake(frame.origin.x, frame.origin.y, textWidth, stringSize.height);
return frame;
}
- (CGRect)addLineWithFrame:(CGRect)frame withColor:(UIColor*)color {
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(currentContext, color.CGColor);
// this is the thickness of the line
CGContextSetLineWidth(currentContext, frame.size.height);
CGPoint startPoint = frame.origin;
CGPoint endPoint = CGPointMake(frame.origin.x + frame.size.width, frame.origin.y);
CGContextBeginPath(currentContext);
CGContextMoveToPoint(currentContext, startPoint.x, startPoint.y);
CGContextAddLineToPoint(currentContext, endPoint.x, endPoint.y);
CGContextClosePath(currentContext);
CGContextDrawPath(currentContext, kCGPathFillStroke);
return frame;
}
- (CGRect)addImage:(UIImage*)image atPoint:(CGPoint)point {
CGRect imageFrame = CGRectMake(point.x, point.y, image.size.width, image.size.height);
[image drawInRect:imageFrame];
return imageFrame;
}
- (void)dismissReaderViewController:(ReaderViewController *)viewController {
[self dismissModalViewControllerAnimated:YES];
}

Image getting bigger after saving it

I would be grateful if anyone could help me to solve an issue with image saving.
I've got an app which contains recipes with corresponding images. I need to resize them in order to preserve memory and make my app more robust. Everything looks ok the UIImage has proper dimensions. But when I save this image to Documents it appears to become something like twice bigger than it should be.
The code I use for image resizing:
+(UIImage *) createImageFromImage: (UIImage *)image WithName:(NSString *)name Thumbnail: (BOOL)isThumbnail{
CGSize oldSize = image.size;
CGSize newSize;
CGFloat scaleFactor;
if (isThumbnail) {
name = [NSString stringWithFormat:#"t_%#", name];
newSize = CGSizeMake(125, 92);
}
else{
newSize = CGSizeMake(320, 240);
}
if (!CGSizeEqualToSize(newSize, oldSize)) {
CGFloat widthFactor = newSize.width/oldSize.width;
CGFloat heightFactor = newSize.height/oldSize.height;
if (((widthFactor > heightFactor)||(heightFactor > 1))&&(widthFactor < 1)) {
scaleFactor = widthFactor;
}
else{
scaleFactor = heightFactor;
}
newSize.width = scaleFactor*oldSize.width;
newSize.height = scaleFactor*oldSize.height;
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:name];
UIImage *newImage = nil;
newImage = [self drawImage:image InContextWithSize:newSize];
[UIImagePNGRepresentation(newImage) writeToFile:path atomically:YES];
return newImage;
}
+(UIImage *)drawImage: (UIImage *)image InContextWithSize: (CGSize) newSize {
UIImage *newImage;
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
I have just changed few lines of code to get scaleFactor
-(UIImage *) createImageFromImage: (UIImage *)image WithName:(NSString *)name Thumbnail: (BOOL)isThumbnail{
CGSize oldSize = image.size;
CGSize newSize;
CGFloat scaleFactorForWidth = 0.0,scaleFactorForHeight= 0.0;
if (isThumbnail) {
name = [NSString stringWithFormat:#"t_%#", name];
newSize = CGSizeMake(125, 92);
}
else{
newSize = CGSizeMake(320, 240);
}
if (!CGSizeEqualToSize(newSize, oldSize)) {
CGFloat widthFactor = newSize.width/oldSize.width;
CGFloat heightFactor = newSize.height/oldSize.height;
if (widthFactor != 1) {
scaleFactorForWidth = widthFactor;
newSize.width = scaleFactorForWidth*oldSize.width;
}
if (heightFactor != 1) {
scaleFactorForHeight = heightFactor;
newSize.height = scaleFactorForHeight*oldSize.height;
}
}
NSLog(#"New Size : (%f,%f)",newSize.width,newSize.height);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:name];
UIImage *newImage = nil;
newImage = [self drawImage:image InContextWithSize:newSize];
[UIImagePNGRepresentation(newImage) writeToFile:path atomically:YES];
return newImage;
}
The issue appeared to deal with the standard Xcode PNG compression option. One just needs to set it to NO if he doesn't wan't this to happen. I guess that this was made for the sake of retina displays.

Save UIWebView content as A4 sized PDF

I have gone over previous questions touching upon the subject, but A4 Sized PDF is the troublesome part.
The code I have written below saves a proper (proportional) PDF file, but its size (area wise) is 4 times of A4-Size. If I halve the height and width, I get an A4 sized PDF but the content overflows into two pages.
How do I solve the problem? (fundamentally pertaining to scaling)
-(void)savePDFFromWebView:(UIWebView*)webView fileName:(NSString*)_fileName
{
int height, width, header, sidespace;
height = 1754;
width = 1240;
header = 15;
sidespace = 30;
// set header and footer spaces
UIEdgeInsets pageMargins = UIEdgeInsetsMake(header, sidespace, header, sidespace);
webView.viewPrintFormatter.contentInsets = pageMargins;
UIPrintPageRenderer *renderer = [[UIPrintPageRenderer alloc] init];
[renderer addPrintFormatter:webView.viewPrintFormatter startingAtPageAtIndex:0];
CGSize pageSize = CGSizeMake(width, height);
CGRect printableRect = CGRectMake(pageMargins.left,
pageMargins.top,
pageSize.width - pageMargins.left - pageMargins.right,
pageSize.height - pageMargins.top - pageMargins.bottom);
CGRect paperRect = CGRectMake(0, 0, pageSize.width, pageSize.height);
[renderer setValue:[NSValue valueWithCGRect:paperRect] forKey:#"paperRect"];
[renderer setValue:[NSValue valueWithCGRect:printableRect] forKey:#"printableRect"];
NSData *pdfData = [self printToPDFWithRender:renderer paperRect:paperRect];
// save PDF file
NSString *saveFileName = [NSString stringWithFormat:#"%#%dx%d.pdf", _fileName, (int)pageSize.width, (int)pageSize.height];
NSString *savePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:saveFileName];
[pdfData writeToFile: savePath atomically: YES];
}
- (NSData*) printToPDFWithRenderer:(UIPrintPageRenderer*)renderer paperRect:(CGRect)paperRect
{
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData( pdfData, paperRect, nil );
[renderer prepareForDrawingPages: NSMakeRange(0, renderer.numberOfPages)];
CGRect bounds = UIGraphicsGetPDFContextBounds();
for ( int i = 0 ; i < renderer.numberOfPages ; i++ )
{
UIGraphicsBeginPDFPage();
[renderer drawPageAtIndex: i inRect: bounds];
}
UIGraphicsEndPDFContext();
return pdfData;
}
Here paperRect you providing is not of A4 size
CGRect paperRect = CGRectMake(0, 0,595.2,841.8);
According printableRect would change
CGRect printableRect = CGRectMake(sidespace,
header,
pageSize.width - (sidespace*2),
pageSize.height - (header*2));
Also I think no need to specify contentInsets as printableRect will do the same. So remove below lines
UIEdgeInsets pageMargins = UIEdgeInsetsMake(header, sidespace, header, sidespace);
webView.viewPrintFormatter.contentInsets = = pageMargins;

Convert multiple images as pdf with multiple page

In my application there is a WebView with HTML contents. Contents is of 3 page in WebView .
Now i want to convert WebView content in PDF. I have created 3 image from the WebView content. Now i want to create pdf using these 3 image. Each image should one page of PDF. But it resulting as single page image. So when i am taking print then content is cut off.
I am using this,
CGSize pageSize = CGSizeMake(612, 2324);
NSString *fileName = #"Demo.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
double currentHeight = 0.0;
for (int index = 1; index <= imageName ; index++)
{
NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%d.png", index]];
UIImage *pngImage = [UIImage imageWithContentsOfFile:pngPath];
[pngImage drawInRect:CGRectMake(0, currentHeight, pageSize.width, pngImage.size.height)];
currentHeight += pngImage.size.height;
}
UIGraphicsEndPDFContext();
What i am doing wrong with this code.
THanks
/*For call method*/
[self createPdfWithName:#"sam" array:[NSArray arrayWithObjects:[UIImage imageNamed:#"01.png"],[UIImage imageNamed:#"02.png"], nil]];
/*Create New Pdf*/
- (NSString *)createPdfWithName: (NSString *)name array:(NSArray*)images
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docspath = [paths objectAtIndex:0];
NSString *pdfFileName = [docspath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.pdf",name]];
UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
for (int index = 0; index <[images count] ; index++)
{
UIImage *pngImage=[images objectAtIndex:index];;
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, (pngImage.size.width), (pngImage.size.height)), nil);
[pngImage drawInRect:CGRectMake(0, 0, (pngImage.size.width), (pngImage.size.height))];
}
UIGraphicsEndPDFContext();
return pdfFileName;
}
func createPDFS(arrImages: [UIImage]) -> NSData? {
var pageHeight = 0.0
var pageWidth = 0.0
for img in arrImages
{
pageHeight = pageHeight+Double(img.size.height)
if Double(img.size.width) > pageWidth
{
pageWidth = Double(img.size.width)
}
}
let pdfData = NSMutableData()
let pdfConsumer = CGDataConsumer(data: pdfData as CFMutableData)!
var mediaBox = CGRect.init(x: 0, y: 0, width: pageWidth, height: pageHeight)
let pdfContext = CGContext(consumer: pdfConsumer, mediaBox: &mediaBox, nil)!
for img in arrImages
{
var mediaBox2 = CGRect.init(x: 0, y: 0, width: img.size.width, height: img.size.height)
pdfContext.beginPage(mediaBox: &mediaBox2)
pdfContext.draw(img.cgImage!, in: CGRect.init(x: 0.0, y: 0, width: pageWidth, height: Double(img.size.height)))
pdfContext.endPage()
}
return pdfData
}
CGSize pageSize = CGSizeMake(612, 2324);
NSString *fileName = #"Demo.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
double currentHeight = 0.0;
for (int index = 1; index <= imageName ; index++)
{
NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%d.png", index]];
UIImage *pngImage = [UIImage imageWithContentsOfFile:pngPath];
// TODO: See Here
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pngImage.size.height), nil);
[pngImage drawInRect:CGRectMake(0, currentHeight, pageSize.width, pngImage.size.height)];
//currentHeight += pngImage.size.height;
}
UIGraphicsEndPDFContext();

How do I make a multiple page PDF from a textView?

I generated a PDF file from my iPhone app and while most of the documents are only one page, I want to be able detect if the text is going to go outside of the "margins" and if so, add it to the next page. I am new to this so not really sure how to do this.
Below is the code. Any suggestions?
- (void) drawBorder
{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
UIColor *borderColor = [UIColor grayColor];
CGRect rectFrame = CGRectMake(kBorderInset, kBorderInset, pageSize.width-kBorderInset*2, pageSize.height-kBorderInset*2);
CGContextSetStrokeColorWithColor(currentContext, borderColor.CGColor);
CGContextSetLineWidth(currentContext, kBorderWidth);
CGContextStrokeRect(currentContext, rectFrame);
}
- (void)drawPageNumber:(NSInteger)pageNumber
{
NSString* pageNumberString = [NSString stringWithFormat:#"Page %d", pageNumber];
UIFont* theFont = [UIFont systemFontOfSize:12];
CGSize pageNumberStringSize = [pageNumberString sizeWithFont:theFont
constrainedToSize:pageSize
lineBreakMode:UILineBreakModeWordWrap];
CGRect stringRenderingRect = CGRectMake(kBorderInset,
pageSize.height - 40.0,
pageSize.width - 2*kBorderInset,
pageNumberStringSize.height);
[pageNumberString drawInRect:stringRenderingRect withFont:theFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
}
- (void) drawHeader:(NSString *)header
{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);
NSString *textToDraw = header;
UIFont *font = [UIFont systemFontOfSize:24.0];
CGSize stringSize = [textToDraw sizeWithFont:font constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset-2*kMarginInset, pageSize.height - 2*kBorderInset - 2*kMarginInset) lineBreakMode:UILineBreakModeWordWrap];
CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height);
[textToDraw drawInRect:renderingRect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
}
- (void) drawText:(NSString *)bodyText
{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);
NSString *textToDraw = bodyText;
UIFont *font = [UIFont systemFontOfSize:14.0];
CGSize stringSize = [textToDraw sizeWithFont:font
constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset-2*kMarginInset, pageSize.height - 2*kBorderInset - 2*kMarginInset)
lineBreakMode:UILineBreakModeWordWrap];
CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset + 50.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height);
[textToDraw drawInRect:renderingRect
withFont:font
lineBreakMode:UILineBreakModeWordWrap
alignment:UITextAlignmentLeft];
}
- (void) drawLine
{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(currentContext, kLineWidth);
CGContextSetStrokeColorWithColor(currentContext, [UIColor blackColor].CGColor);
CGPoint startPoint = CGPointMake(kMarginInset + kBorderInset, kMarginInset + kBorderInset + 40.0);
CGPoint endPoint = CGPointMake(pageSize.width - 2*kMarginInset -2*kBorderInset, kMarginInset + kBorderInset + 40.0);
CGContextBeginPath(currentContext);
CGContextMoveToPoint(currentContext, startPoint.x, startPoint.y);
CGContextAddLineToPoint(currentContext, endPoint.x, endPoint.y);
CGContextClosePath(currentContext);
CGContextDrawPath(currentContext, kCGPathFillStroke);
}
- (void) drawImage:(UIImage *)image
{
//UIImage * demoImage = [UIImage imageNamed:#"demo.png"];
[image drawInRect:CGRectMake( (pageSize.width - image.size.width/2)/2, 350, image.size.width/2, image.size.height/2)];
}
- (void) generatePdf: (NSString *)thefilePath :(NSString *) theHeader :(NSString *) theText :(UIImage *) theImage
{
pageSize = CGSizeMake(612, 792);
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
NSInteger currentPage = 0;
BOOL done = NO;
do
{
//Start a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
//Draw a page number at the bottom of each page.
currentPage++;
[self drawPageNumber:currentPage];
//Draw a border for each page.
[self drawBorder];
//Draw text fo our header.
[self drawHeader:theHeader];
//Draw a line below the header.
[self drawLine];
//Draw some text for the page.
[self drawText:theText];
//Draw an image
[self drawImage:theImage];
done = YES;
}
while (!done);
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)generatePdfWithFileName:(id)sender
{
pageSize = CGSizeMake(612, 792);
NSString *fileName = #"test.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
NSString *header = #"This is the bad ass header section!";
NSString *text = #"There is so much to say here I don't know where to begin....";
[self generatePdf : pdfFileName : header:text:nil];
}
#end
The concept can be seen here
[from http://spitzkoff.com/craig/?p=160]
When placing items, get the Height:
CGSize size = [name sizeWithFont:studentNameFont forWidth:maxWidth lineBreakMode:UILineBreakModeWordWrap];
[name drawAtPoint:CGPointMake(kMargin, currentPageY) forWidth:maxWidth withFont:studentNameFont lineBreakMode:UILineBreakModeWordWrap];
currentPageY += size.height;
At appropriate points, check the currentY and decide if you need to move to a new page:
if (size.height + currentPageY > maxHeight) {
// create a new page and reset the current page's Y value
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth, kDefaultPageHeight), nil);
currentPageY = kMargin;
}
I hope that helps.
I'm new to stackoverflow but I would like to suggest a brief answer to the "break into parts a long text view" problem.
The method I use is surely not the most efficient way to break a text into parts but it's the only way i found and it works.
Taking a part of a text, i just check in a loop, before drawing, word by word if the sub-text fit in my page, meaning height permitted. If the whole text doesn't fit then my code will only draw the part found. For instance, I could break a long text into four pages..., at least.
Sorry for my lack of experience and if my answer, that is just a glimpse, an idea of "how to", is not clear enough.
To clarify my answer. I added the method I use to check the height needed (found) for a "sub-text" (checking every time with a word added to the "sub-text"). Because "sizeWithFont:" is deprecated since IOS 7, I use "boundingRectWithSize:" method.
// Process, that can take long though, to find the next start of the part of a text that hasn't been drawned yet.
float widthRect;
widthRect = pageWidth - 2 * leftMargin;
- (unsigned int) findNextStartingPos:(NSString *)text withHeight:(float) height
{
NSString *character;
unsigned int pos = 0;
unsigned int posFound = 0;
unsigned int lastPosFound = 0;
BOOL found = NO;
NSString *textTest;
float heightFound;
while (pos <= [text length]-1)
{
character = [text substringWithRange:NSMakeRange(pos, 1)];
if ([character isEqualToString:#" "] || [character isEqualToString:#"\n"])
{
posFound = pos;
found = YES;
}
if (found)
{
textTest = [text substringWithRange:NSMakeRange(0, posFound)];
heightFound = lroundf([self heightFound:textTest withWidth:widthRect fontSize:fontSizeBody center:NO]);
if (heightFound > height)
{
if (lastPosFound == 0)
{
posFound = lastPosFound;
} else
{
posFound = lastPosFound + 1;
}
if (posFound > [text length]-1) posFound = lastPosFound;
break;
}
lastPosFound = posFound;
}
pos++;
}
return posFound;
} return posFound;
}
// Returns the height needed to draw a text (and to check for space in a page)
-(float) heightFound:(NSString *)text withWidth:(float)width fontSize:(CGFloat)fontSize center:(BOOL)center
{
if ([text length] != 0)
{
UIFont *font = [UIFont fontWithName:#"Helvetica" size:fontSize];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
if (center)
{
paragraphStyle.alignment = NSTextAlignmentCenter;
} else {
paragraphStyle.alignment = NSTextAlignmentLeft;
}
NSDictionary *attributes = #{NSFontAttributeName:font, NSParagraphStyleAttributeName: paragraphStyle};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text attributes:attributes];
CGRect newRect = [attributedString boundingRectWithSize:(CGSize){width, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin context:nil];
return newRect.size.height;
} else {
return 0;
}
}

Resources