坦白地說(shuō),在以編程方式繪制PDF文件時(shí),使用CoreGraphics框架是一項(xiàng)繁瑣的工作。我想以編程方式創(chuàng)建PDF,使用整個(gè)應(yīng)用程序視圖中的各種對(duì)象。我很想知道iOS SDK是否有不錯(cuò)的PDF教程,也許是庫(kù)中的一個(gè)。我看過(guò)本教程PDF Creation Tutorial,但是它主要是用C編寫的。尋找更多的Objective-C風(fēng)格。這似乎是寫入PDF文件的一種荒謬方法,必須計(jì)算線和其他對(duì)象的放置位置。void CreatePDFFile (CGRect pageRect, const char *filename) { // 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); 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 50 on all sides CGContextStrokeRect(pdfContext, CGRectMake(50, 50, pageRect.size.width - 100, pageRect.size.height - 100));
3 回答

大話西游666
TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊
iOS上的PDF函數(shù)都是基于CoreGraphics的,這是有道理的,因?yàn)閕OS中的繪制基元也是基于CoreGraphics的。如果要直接將2D基本體渲染到PDF中,則必須使用CoreGraphics。
UIKit中也存在一些對(duì)象的快捷方式,例如圖像。將PNG繪制到PDF上下文仍然需要調(diào)用CGContextDrawImage,但是您可以使用可以從UIImage獲取的CGImage來(lái)實(shí)現(xiàn),例如:
UIImage * myPNG = [UIImage imageNamed:@"mypng.png"];
CGContextDrawImage (pdfContext, CGRectMake(200, 200, 207, 385), [myPNG CGImage]);
如果您需要有關(guān)總體設(shè)計(jì)的更多指導(dǎo),請(qǐng)更明確地說(shuō)明“整個(gè)應(yīng)用程序中的各種對(duì)象”的含義以及您要實(shí)現(xiàn)的目標(biāo)。
- 3 回答
- 0 關(guān)注
- 782 瀏覽
添加回答
舉報(bào)
0/150
提交
取消