2014-04-30 52 views
-3

我有一个图像或任何文字。我想创建一个PDF文件,如test.pdf。 创建pdf文件后,我想用MailComposer邮寄该文件。 请有人可以建议如何在ios编码中创建.pdf文件?如何在ios中创建.pdf文件?

+1

你做任何的搜索?你至少看过相关问题的清单吗? – rmaddy

+0

请你可以编辑我的问题,我是新的ios和stackoverflow也。请不要对我的问题给予负面投票,如果可能的话,您可以编辑我的问题 – user104

+0

如果您希望避免被拒绝投票,请点击上面的帮助链接,阅读如何提出正确的问题。你的问题太广泛了,你没有做任何研究。这不是“我懒得做任何工作,请给我代码”网站。 – rmaddy

回答

1

在PDF创建按钮,点击动作写代码..

#import "PDF_Screen.h" 

// Create Pdf... 

PDF_Screen *loPdf = [[PDF_Screen alloc] init]; // this is class for create pdf file 
[loPdf createPDF:@"Report.Pdf"]; 

// Attach with mail ...... 

Mail_Screen *lomailscreen = [[Mail_Screen alloc] initWithNibName:nil bundle:nil]; 
lomailscreen.mToRecepient = @""; 
lomailscreen.mSubject = @"Budget Report"; 
lomailscreen.mBody = @""; 
[self.navigationController pushViewController:lomailscreen animated:YES]; 

此处我写一个PDF类代码..

PDF_Screen.h

#import <Foundation/Foundation.h> 

@interface PDF_Screen : NSObject 

{ 

} 

void CreatePDFFile (CGRect pageRect, const char *filename); 
- (void)createPDF:(NSString *)PdfFilename; 
@end 

PDF_Screen.m

#import "PDF_Screen.h" 

@implementation PDF_Screen 

-(id)init 
{ 
self = [super init]; 
if (self) 
{ 

} 
return self ; 
} 

-(void)createPDF:(NSString *)PdfFilename 
{ 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *saveDirectory = [paths objectAtIndex:0]; 
NSString *saveFileName = PdfFilename ; 
NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName]; 
const char *filename = [newFilePath UTF8String]; 

    CreatePDFFile(CGRectMake(0, 0, 320, 345), filename); 

} 

void CreatePDFFile (CGRect pageRect , const char *filename) 
{ 
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); 

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

// Cleanup our mess 
CFRelease(myDictionary); 
CFRelease(url); 

// Done creating our PDF Context, now it's time to draw to it 

// Starts our first page 
CGContextBeginPage (pdfContext, &pageRect); 

// Draws a black rectangle around the page inset by 20 on all sides 
CGContextStrokeRect(pdfContext, CGRectMake(20, 20, pageRect.size.width - 40, pageRect.size.height - 40)); 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *saveDirectory = [paths objectAtIndex:0]; 
NSString *saveFileName = @"Picture.jpeg" ; 
NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName]; 

UIImage *newUIImage1 =[UIImage imageWithContentsOfFile:newFilePath]; 

NSData * data1 = UIImageJPEGRepresentation(newUIImage1, 0.95); 
UIImage * compressedImage1 = [[UIImage alloc] initWithData:data1]; 
CGImageRef image1 = [compressedImage1 CGImage]; 

CGContextDrawImage (pdfContext, CGRectMake(21, 21, 278, 303),image1); 

// We are done drawing to this page, let's end it 
// We could add as many pages as we wanted using CGContextBeginPage/CGContextEndPage 
CGContextEndPage (pdfContext); 

// We are done with our context now, so we release it 
CGContextRelease (pdfContext); 

} 

-(void)dealloc 
{ 
[super dealloc]; 
} 

@end 

我认为这会帮助你.. :)

+0

谢谢你接受我的回答。我会给你一个链接。如果您需要更多信息,请点击此链接。 http://www.raywenderlich.com/6581/how-to-create-a-pdf-with-quartz-2d-in-ios-5-tutorial-part-1 –