-3
我有一個圖像或任何文字。我想創建一個PDF文件,如test.pdf
。 創建pdf文件後,我想用MailComposer
郵寄該文件。 請有人可以建議如何在ios編碼中創建.pdf
文件?如何在ios中創建.pdf文件?
我有一個圖像或任何文字。我想創建一個PDF文件,如test.pdf
。 創建pdf文件後,我想用MailComposer
郵寄該文件。 請有人可以建議如何在ios編碼中創建.pdf
文件?如何在ios中創建.pdf文件?
在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
我認爲這會幫助你.. :)
謝謝你接受我的回答。我會給你一個鏈接。如果您需要更多信息,請點擊此鏈接。 http://www.raywenderlich.com/6581/how-to-create-a-pdf-with-quartz-2d-in-ios-5-tutorial-part-1 –
你做任何的搜索?你至少看過相關問題的清單嗎? – rmaddy
請你可以編輯我的問題,我是新的ios和stackoverflow也。請不要對我的問題給予負面投票,如果可能的話,您可以編輯我的問題 – user104
如果您希望避免被拒絕投票,請點擊上面的幫助鏈接,閱讀如何提出正確的問題。你的問題太廣泛了,你沒有做任何研究。這不是「我懶得做任何工作,請給我代碼」網站。 – rmaddy