【问题标题】:Creating a PDF From UITextView从 UITextView 创建 PDF
【发布时间】:2013-07-13 13:00:12
【问题描述】:

我目前正在创建和保存 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,但我也不知道该怎么做。

【问题讨论】:

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


    【解决方案1】:

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

    - (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 边界值将 PDF 文档的默认页面大小设置为 8.5 x 11 英寸(612 x 792 磅)。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 谢谢。我已经参考该文档几个小时了,但由于某种原因它没有点击。您知道是否有任何与从 UITextView、保存的文本或字符串创建 PDF 相关的示例?
      • 清单 4-2 使用 CoreText 和 NSString 类在页面上绘制文本。如果您的文本在 NSString 对象中,您可以使用 drawInRect:withFont: 方法在 PDF 页面上绘制它。
      • 谢谢。你知道这个代码是否有任何工作示例?我会继续并将其标记为正确,即使它超出了我的想象,我相信这是正确的答案。
      • 正是我想要的。有几点需要注意。 1.添加coretext框架。 2.#import 3.用currentRange = [self renderPage:currentPage withTextRange:currentRange andFramesetter:framesetter];修复错误
      猜你喜欢
      • 2013-02-10
      • 1970-01-01
      • 2014-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-22
      相关资源
      最近更新 更多