坦白地說(shuō),在以編程方式繪制PDF文件時(shí),使用CoreGraphics框架是一項(xiàng)繁瑣的工作。我想以編程方式創(chuàng)建PDF,使用整個(gè)應(yīng)用程序視圖中的各種對(duì)象。我很想知道iOS SDK是否有不錯(cuò)的PDF教程,也許是庫(kù)中的一個(gè)。我看過本教程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));
iOS SDK-以編程方式生成PDF文件
胡說(shuō)叔叔
2019-12-10 09:08:36