在iPhone中将UITextView转换为Pdf文件?

时间:2012-11-15 12:27:36

标签: ios pdf uitextview

我正在使用iPhone应用程序,使用UITextView从用户那里获取一些内容,然后我尝试从该UITextView转换pdf文件并存储在本地目录中,但我不知道这一点。

2 个答案:

答案 0 :(得分:3)

这是代码

- (void) generatePdfWithFilePath: (NSString *)thefilePath
{
    UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);

    NSInteger currentPage = 0;
    BOOL done = NO;
    do
    {
    // Mark the beginning of a new page.
        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);

    // Draw a page number at the bottom of each page.
    currentPage++;
    [self drawPageNumber:currentPage];

    //Draw a border for each page.
    [self drawBorder];

    //Draw text fo our header.
    [self drawHeader];

    //Draw a line below the header.
    [self drawLine];

    //Draw some text for the page.
    [self drawText];

    //Draw an image
    [self drawImage];
    done = YES;
}
   while (!done);

// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
}


- (void) drawText
{
    CGContextRef    currentContext = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);

    NSString *textToDraw = yourtextview.text // use your textfield or textview here

    UIFont *font = [UIFont systemFontOfSize:14.0];

    CGSize stringSize = [textToDraw sizeWithFont:font
                           constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset-    2*kMarginInset, pageSize.height - 2*kBorderInset - 2*kMarginInset)
                               lineBreakMode:UILineBreakModeWordWrap];

    CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset + 50.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height);

[textToDraw drawInRect:renderingRect
              withFont:font
         lineBreakMode:UILineBreakModeWordWrap
             alignment:UITextAlignmentLeft];
}

答案 1 :(得分:0)

您可以使用UIKit创建PDF文件。

Create PDF with UIKit