This article describes how to create a pdf file in iOS sdk. To create a pdf file first of all user have to add QuartzCore framework in their project and import <QuartzCore/QuartzCore.h> header file in their view controller.
The CGContextRef opaque type represents a Quartz 2D drawing destination. A graphic context contains drawing parameters and all device-specific information needed to render the paint on a page to the destination, whether the destination is a window in an application, a bitmap image, a PDF document or a printer.
Create PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary pdfContext=CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
Clean up your mess CFRelease(myDictionary); CFRelease(url);
We have done creating our PDF Context, now it’s time to draw. For drawing first of all we have to start our first page using CGContextBeginPage method. This method starts new page in context. CGContextBeginPage (pdfContext, &pageRect);
Using UIGraphicsPushContext function user can save previous graphics content and make specified context as a current context. User has to balance this call using: UIGraphicsPopContext. UIGraphicsPushContext(pdfContext);
CGContextTranslateCTM function is used to translate origin of the user co-ordinate into the context. General method is CGContextTranslateCTM(current context,dx,dy);
current context= context in which you want to apply to Translate,
dx = The replacement value of x-axis of the coordinate space in specified context,
dy = The replacement value of y-axis of the coordinate space in specified context.
CGContextSaveGState creates the copy of current graphics state and save it in the stack of context.
CGContextScaleCTM function changes the scale of user coordinates in a given context. General method for this function is CGContextScaleCTM(pdfContext, sx, sy)
pdfContext = Current graphics context;
sx = scale factor of x co-ordinate in given context
sy = scale factor of y co-ordinate in given context
CGContextScaleCTM(pdfContext, 1.0, –1.0);
CGContextSetRGBFillColor function used to set colour fill value in current context using RGB colour space.
We are done with all the properties related to context set and now we have to start drawing text or image on pdf page. So, for drawing text or image on a page drawInRect method is used.
For text drawing we will use – (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment method.
rect = rect in which you want to draw text
font = Set UIFont of string
lineBreakMode = it set the break mode of line
alignment = it sets the text alignment
If you want to draw string like “This is pdf title” on pdf page then write
NSString *title= [NSStringstringWithFormat:@”This is pdf title“]; [titledrawInRect:CGRectMake(0,-575,1024,00)withFont: [UIFontfontWithName:@”Helvetica”size:45.0f] lineBreakMode:NSLineBreakByWordWrappingalignment:UITextAlignmentCenter];
If you want to draw an image on pdf page then you have to write
image = image you want to draw on pdf page,
imageRect = rect in which you want to draw image.
After drawing all the content on pdf we have to call CGContextRestoreGState function. The function CGContextRestoreGState replaces the current graphics state with the graphics state that is stored on top of the graphics stack.
After that call UIGraphicsPopContext().This method removes the current graphics context from the top of the stack, restoring the previous graphics context.
After removing graphics content, user can end the pdf page using CGContextEndPage(pdfContext); this method ends the current page in a page-based graphics context.