2012-11-15 30 views
0

我正在iPhone應用程序工作,使用UITextView從用戶獲取一些內容,然後我試圖從該UITextView轉換PDF文件並存儲在本地目錄中,但我不知道這一點。在iPhone中將UITextView轉換爲PDF文件?

+1

示例代碼甚至不會編譯,執行更你說你想要的。 –

+0

感謝您的回覆 – SampathKumar

+0

如何將其從textview轉換爲pdf文件。 – SampathKumar

回答

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]; 
} 
+0

感謝您的回覆 – SampathKumar

+0

嗨請接受並upvote我的答案,如果它對你有用:) – abhishekkharwar