2 starwar2012 starwar2012 于 2012.11.16 14:24 提问

在iphone中将 UITextView转换为pdf文件

本人是开发iPhone应用的,现在用户给了一些UITextView,我需要把它转换成PDF文件然后存储在本地,不知道怎么做?谢谢大家的帮忙。

2个回答

gaoXxxing
gaoXxxing   2012.11.16 14:44
已采纳

用UIKit可以把UITextView转为PDF文件的

具体的用法你可以去网上搜一下,也可以看看这个说明链接

starwar2012
starwar2012 谢谢
接近 5 年之前 回复
prettYYoyic
prettYYoyic   2012.11.16 14:58

试试代码:

- (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];
}
Csdn user default icon
上传中...
上传图片
插入图片