iOS SDK-以编程方式生成PDF文件

坦白地说,在以编程方式绘制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));

Qyouu
浏览 733回答 3
3回答

大话西游666

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]);如果您需要有关总体设计的更多指导,请更明确地说明“整个应用程序中的各种对象”的含义以及您要实现的目标。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS