坦白地說,在以編程方式繪制PDF文件時,使用CoreGraphics框架是一項繁瑣的工作。我想以編程方式創建PDF,使用整個應用程序視圖中的各種對象。我很想知道iOS SDK是否有不錯的PDF教程,也許是庫中的一個。我看過本教程PDF Creation Tutorial,但是它主要是用C編寫的。尋找更多的Objective-C風格。這似乎是寫入PDF文件的一種荒謬方法,必須計算線和其他對象的放置位置。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 回答

縹緲止盈
TA貢獻2041條經驗 獲得超4個贊
iOS上的PDF函數都是基于CoreGraphics的,這是有道理的,因為iOS中的繪制基元也是基于CoreGraphics的。如果要直接將2D基本體渲染到PDF中,則必須使用CoreGraphics。
UIKit中也存在一些對象的快捷方式,例如圖像。將PNG繪制到PDF上下文仍然需要調用CGContextDrawImage,但是您可以使用可以從UIImage獲取的CGImage來實現,例如:
UIImage * myPNG = [UIImage imageNamed:@"mypng.png"];
CGContextDrawImage (pdfContext, CGRectMake(200, 200, 207, 385), [myPNG CGImage]);
如果您需要有關總體設計的更多指導,請更明確地說明“整個應用程序中的各種對象”的含義以及您要實現的目標。
- 3 回答
- 0 關注
- 481 瀏覽
添加回答
舉報
0/150
提交
取消