[contact-form-7 404 "Not Found"]
Other Ways To Meet Us :info@zealousys.com
Our Blog
Jan 16

Create PDF in iOS sdk

By zealousys

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.

CGContextRef pdfContext;

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.

CGRect pageRect =CGRectMake(0, 0,DOC_WIDTH,DOC_HEIGHT);

Above method defines the page rect means page size. DOC_WIDTH is equal to the total width of the document page and DOC_HEIGHTis equal to the total page height.

Create file name using below string:
NSString *fileName=@”Test.pdf”;
const char *filename = [fileName UTF8String];

Create a CFString from the filename we provide to this method when we call it.
CFStringRef path = CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);

Create a CFURL using the CFString we just defined.
CFURLRef url = CFURLCreateWithFileSystemPath (NULL, path,kCFURLPOSIXPathStyle, 0);

Now create dictionary using CFMutableDictionaryRef. This dictionary contains extra options mostly for ‘signing’ the PDF.

CFMutableDictionaryRef myDictionary = NULL;
myDictionary=CFDictionaryCreateMutable(NULL,0,&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(myDictionary, kCGPDFContextTitle,(__bridge_retainedCFStringRef)fileName);
CFDictionarySetValue(myDictionary,kCGPDFContextCreator, (__bridge_retainedCFStringRef)fileName);

Create PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary pdfContext=CGPDFContextCreateWithURL (url, &pageRect, myDictionary);

Clean up your mess

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:

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.

CGContextTranslateCTM(pdfContext, 0,20);

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.

CGContextSetRGBFillColor (pdfContext, 0, 0, 0, 1);

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

UIImage *image = [UIImageimageNamed:@”pdf_front_page.png”];
CGRect imageRect = CGRectMake(0,-768, 1004, 768);
[image drawInRect:imageRect];

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.

What Can We Build for You?


NDA From The Start
We keep your idea safe. We're always willing to sign a Non-Disclosure Agreement with you and fully protect your intellectual property.



For sales inquiry