2012-11-15 5 views
0

iPhone 응용 프로그램에서 UITextView를 사용하여 사용자의 일부 콘텐츠를 얻은 다음 해당 UITextView에서 PDF 파일을 변환하고 로컬 디렉토리에 저장하려고했지만이 사실을 알지 못했습니다.UITextView를 iPhone의 Pdf 파일로 변환 하시겠습니까?

+1

귀하의 샘플 코드도 훨씬 적은 당신이 원하는 무슨 말, 컴파일되지 않습니다. –

+0

답장을 보내 주셔서 감사합니다 – SampathKumar

+0

텍스트 파일을 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

안녕하세요, 받아 들일 수 내 대답은 당신을 위해 유용합니다 :) – abhishekkharwar

관련 문제