从UITextView创建PDF

时间:2013-07-15 15:32:10

标签: ios cocoa-touch pdf quartz-2d

我目前正在创建和保存UIView的PDF。它工作得很好,但现在我正在尝试从保存到磁盘而不是UIView的文本数据创建PDF。理想情况下,我想基于UITextView中的文本创建PDF。下面的代码很好地编写了文本视图的第一部分,但它没有从整个文本视图创建PDF。这是相当多的文本,因此它只根据适合视图的文本量创建PDF。我目前使用的代码是:

- (void)createPDFfromUIView:(UITextView*)view saveToDocumentsWithFileName:(NSString*)aFilename
{

   NSMutableDictionary *myDictionary = CFBridgingRelease(CFDictionaryCreateMutable(NULL, 0,
                                             &kCFTypeDictionaryKeyCallBacks,
                                             &kCFTypeDictionaryValueCallBacks));

    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = [NSMutableData data];

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, textview.bounds, myDictionary);
    UIGraphicsBeginPDFPage();

    CGContextRef pdfContext = UIGraphicsGetCurrentContext();


    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData

    [textview.layer renderInContext:pdfContext]; // this line

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:@"filename"];

    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
}

要调用它/保存它,我把这条消息发送到IBAction:[self createPDFfromUIView:textview saveToDocumentsWithFileName:@"pdflocation"];

现在,我已经解析了几个小时的代码和文档,并且无法弄清楚如何基于保存的文本文件而不是视图来创建PDF。我还是编程的新手,所以很多这都是我的想法。有谁知道如何更改上面的代码,让它根据保存的文本而不是视图创建PDF?

选项:我可以根据NSString的输出创建PDF。我也可以将保存的文本放入UITextView并根据(原始计划)创建PDF但我不知道如何做。

2 个答案:

答案 0 :(得分:5)

Apple的这篇文章解释了如何在Pdf页面上创建Pdf文件和绘制文本。

答案 1 :(得分:0)

对于那些仍在寻找示例代码的人来说,这可行:

- (NSData *)createPDFforText: (NSString *)text
{
    NSMutableData *pdfData = [NSMutableData data];
    NSMutableDictionary* attributes = [NSMutableDictionary dictionary];
    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

    UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);
    UIGraphicsBeginPDFPage();

    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentLeft;

    [attributes setObject: paragraphStyle forKey: NSParagraphStyleAttributeName];
    [attributes setObject: [UIFont fontWithName: LATO_REGULAR size: 20.f] forKey: NSFontAttributeName];
    [attributes setObject: [UIColor blackColor] forKey: NSForegroundColorAttributeName];
    [text drawAtPoint: CGPointMake(20.f, 44.f) withAttributes: attributes];

    UIGraphicsEndPDFContext();

    return pdfData;

}

CGRectZero bounds值将PDF文档设置为默认页面大小8.5 x 11英寸(612 x 792点)。