3

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

4

2 回答 2

5

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

于 2013-07-15T18:44:23.367 回答
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 边界值将 PDF 文档的默认页面大小设置为 8.5 x 11 英寸(612 x 792 磅)。

于 2017-12-26T22:16:04.867 回答