Problem while creating PDF Pages

Discussion in 'iOS Programming' started by AbhishekApple, Dec 15, 2010.

  1. AbhishekApple, Dec 15, 2010
    Last edited: Dec 15, 2010

    AbhishekApple macrumors member

    Joined:
    Aug 5, 2010
    #1
    I want to create pdf pages after clicking the button and on click of other button
    it should save the pdf(release pdf context).

    But this code creates the pdf file without any content....

    How to refer the CGContextRef variable and CGRect variable in this
    function -(IBAction)createPage: (id)sender

    Code:
    
    [COLOR="DarkOrange"]
    CGContextRef pdfContext;
    CGRect pageRect;
    [/COLOR]
    
    -(void)viewDidLoad{
    ....
    ....
    [self createPdfContext];
    }
    
    -(void)createPdfContext{
    	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    	NSString *saveDirectory = [paths objectAtIndex:0];
    	NSString *saveFileName = @"myPDF.pdf";
    	NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName];
    	const char *filename = [newFilePath UTF8String];
    	
    	float detW=[detailView frame].size.width;
    	float detH=[detailView frame].size.height;
    	
    	pageRect = CGRectMake(0, 0, detW, detH);
    	
    	// This code block sets up our PDF Context so that we can draw to it
    	//CGContextRef pdfContext;
    	CFStringRef path;
    	CFURLRef url;
    	CFMutableDictionaryRef myDictionary = NULL;
    	// Create a CFString from the filename we provide to this method when we call it
    	path = CFStringCreateWithCString (NULL, filename,
    									  kCFStringEncodingUTF8);
    	// Create a CFURL using the CFString we just defined
    	url = CFURLCreateWithFileSystemPath (NULL, path,
    										 kCFURLPOSIXPathStyle, 0);
    	NSLog(@"path= %@",path);
    	CFRelease (path);
    	// This dictionary contains extra options mostly for 'signing' the PDF
    	myDictionary = CFDictionaryCreateMutable(NULL, 0,
    											 &kCFTypeDictionaryKeyCallBacks,
    											 &kCFTypeDictionaryValueCallBacks);
    	CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
    	CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
    	// Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary
    	pdfContext = CGPDFContextCreateWithURL(url, &pageRect, myDictionary);
    	NSLog(@"%@",url);
    	// Cleanup our mess
    	CFRelease(myDictionary);
    	CFRelease(url);
    	// Done creating our PDF Context, now it's time to draw to it	
    	
    }
    
    -(IBAction)createPage:(id)sender{
    	// Starts our first page
    	[COLOR="rgb(255, 140, 0)"]CGContextBeginPage (pdfContext, &pageRect);[/COLOR]
    	// Draws a black rectangle around the page inset by 50 on all sides
    	CGContextStrokeRect(pdfContext, CGRectMake(50, 50, pageRect.size.width - 100, pageRect.size.height - 100));
    	
    	// This code block will create an image that we then draw to the page
    	//const char *picture = "Picture";
    	CGRect childRect;
    	for (UIView *childview in detailView.subviews) {
    			//to get the mainView coordinates for comparision with detailView boundary
    			childRect = [childview frame];
    		
    		DragImageView *tmpView = (DragImageView*)childview;
    		UIImage *imgView = [UIImage imageNamed:@"button_grey_light.png"];
    		CGImageRef image=[imgView CGImage];
    	NSLog(@"Image Nmae=%@",[tmpView imageData]);
    		CGContextDrawImage (pdfContext,childRect,image);
    		CGImageRelease (image);
    	}
    	CGContextEndPage (pdfContext);
    	
    }
    
    -(IBAction)savePdf:(id)sender{
    	// We are done with our context now, so we release it
    	CGContextRelease (pdfContext);
    }
    
    
    
    Please suggest...
     
  2. AbhishekApple thread starter macrumors member

    Joined:
    Aug 5, 2010
    #2
    The following code works but the problem is it will create the pdf file....what i want is after the createPage button is clicked one page of the pdf should be generated and subsequently other shuld get attached to it after clicking createPage......

    In following code i can do as===> create multiple pdf files and then merge those files to one.....

    Again i have least idea of merging the pdf files...

    Code:
    	CGContextRef pdfContext;
    	CGRect pageRect;
    
    -(IBAction)createPage:(id)sender{
    
    	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    	NSString *saveDirectory = [paths objectAtIndex:0];
    	NSString *saveFileName = @"myPDF.pdf";
    	NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName];
    	const char *filename = [newFilePath UTF8String];
    	
    	float detW=[detailView frame].size.width;
    	float detH=[detailView frame].size.height;
    	
    	pageRect = CGRectMake(0, 0, detW, detH);
    	
    	// This code block sets up our PDF Context so that we can draw to it
    	//CGContextRef pdfContext;
    	CFStringRef path;
    	CFURLRef url;
    	CFMutableDictionaryRef myDictionary = NULL;
    	// Create a CFString from the filename we provide to this method when we call it
    	path = CFStringCreateWithCString (NULL, filename,
    									  kCFStringEncodingUTF8);
    	// Create a CFURL using the CFString we just defined
    	url = CFURLCreateWithFileSystemPath (NULL, path,
    										 kCFURLPOSIXPathStyle, 0);
    	NSLog(@"path= %@",path);
    	CFRelease (path);
    	// This dictionary contains extra options mostly for 'signing' the PDF
    	myDictionary = CFDictionaryCreateMutable(NULL, 0,
    											 &kCFTypeDictionaryKeyCallBacks,
    											 &kCFTypeDictionaryValueCallBacks);
    	CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
    	CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
    	// Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary
    	pdfContext = CGPDFContextCreateWithURL(url, &pageRect, myDictionary);
    	NSLog(@"%@",url);
    	// Cleanup our mess
    	CFRelease(myDictionary);
    	CFRelease(url);
    	
    	// Starts our first page
    	CGContextBeginPage (pdfContext, &pageRect);
    	
    	// Draws a black rectangle around the page inset by 50 on all sides
    	CGContextStrokeRect(pdfContext, CGRectMake(50, 50, pageRect.size.width - 100, pageRect.size.height - 100));
    	
    	// This code block will create an image that we then draw to the page
    	//const char *picture = "Picture";
    	CGRect childRect;
    	for (UIView *childview in detailView.subviews) {
    			//to get the mainView coordinates for comparision with detailView boundary
    			childRect = [childview frame];
    		
    		DragImageView *tmpView = (DragImageView*)childview;
    		//NSLog(@"Image Nmae=%@",[tmpView imageData]);
    	
    		UIImage *imgView = [UIImage imageNamed:@"button_grey_light.png"];
    		CGImageRef image=[imgView CGImage];
    	NSLog(@"Image Nmae=%@",[tmpView imageData]);
    		CGContextDrawImage (pdfContext,childRect,image);
    		CGImageRelease (image);
    	}
    	CGContextEndPage (pdfContext);
    	
    }
    
    -(IBAction)savePdf:(id)sender{
    	// We are done with our context now, so we release it
    	CGContextRelease (pdfContext);
    }
    
    
    
     

Share This Page