Powered By Blogger

Thursday, July 5, 2012

how to draw images to PDF in iPhone sdk?

Hello friends,
I'm going to place PDF creation code for iOS SDK including images and textdraw. For this, you have to include "QuartzCore.framework" into your project.

Step 1: Include "QuartzCore.framework".

Step 2: Import "<QuartzCore/QuartzCore.h>" into your controller header file.

Step 3: Place below code into your class where you want to use it. You can change the parameters in your methods as per your requirement. You may change it any of the object.(Ex. UIView, UIImageView, etc...)


#pragma mark -
#pragma mark PDF Generation Code


-(IBAction)drawPdf:(id)sender
{
    [self writeImage:ImageView1 :1];
    [self writeImage:ImageView2 :2];
    [self writeImage:ImageView3 :3];
    [self drawImagesToPdf:ImageView1];
}


-(void)writeImage:(UIImageView *)btn:(NSInteger)tag
{
    CGRect screenRect = btn.frame;
    UIGraphicsBeginImageContext(screenRect.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor whiteColor] set];
    CGContextFillRect(ctx, screenRect);
    [btn.layer renderInContext:ctx];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
   
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png",tag]];
   
    [UIImagePNGRepresentation(newImage) writeToFile:pngPath atomically:YES];
}

-(void)drawImagesToPdf:(UIImageView *)button
{
CGSize pageSize = CGSizeMake(button.frame.size.width, button.frame.size.height*3+30);
  
    NSString *fileName = @"Demo.pdf";
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
    UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectMake(0, 0, button.frame.size.width, button.frame.size.height*3), nil);
  
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0.0, pageSize.width, button.frame.size.height), nil);
  
    NSString *name  = [NSString stringWithFormat:@"Unknown User];
    NSString *email = @"unknwon.hotmail.com";
    NSString *city=@"unknown";
    NSString *zip=@"unknown";
  
    [name drawInRect:CGRectMake(50.0, 50.0, button.frame.size.width-50.0, 20.0) withFont:[UIFont fontWithName:@"Verdana" size:15.0] lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
  
    [email drawInRect:CGRectMake(50.0, 75.0, button.frame.size.width-50.0, 20.0) withFont:[UIFont fontWithName:@"Verdana" size:13.0] lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
  
    [city drawInRect:CGRectMake(50.0, 100.0, button.frame.size.width-50.0, 20.0) withFont:[UIFont fontWithName:@"Verdana" size:13.0] lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];

    [zip drawInRect:CGRectMake(50.0, 125.0, button.frame.size.width-50.0, 20.0) withFont:[UIFont fontWithName:@"Verdana" size:13.0] lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
  
    for (int i =1; i<=3; i++)
    {
        double currentHeight = 0.0;
        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, currentHeight, pageSize.width, button.frame.size.height), nil);
        NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", i]];
        UIImage *pngImage = [UIImage imageWithContentsOfFile:pngPath];
        [pngImage drawInRect:CGRectMake(0, currentHeight, pageSize.width, pngImage.size.height)];
    }
    UIGraphicsEndPDFContext();}

Feel free to ask if you have any issue using this code. 

Thanks & Regards,
Nilesh Prajapati

No comments:

Post a Comment