-1

when I add some text in the textview, onclick of convert to pdf,then i have to convert textview text to pdf file.

can anyone give some solution for this problem.

Thanks for quick response

1 Answers1

0

Refer this code.

NSString *text = @"this is textField's text";
NSMutableData *pdfData = [[NSMutableData alloc] init];
UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0, 0, 320, 480), nil);
UIGraphicsBeginPDFPage();
[text drawInRect:UIGraphicsGetPDFContextBounds() withAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:17]}];
UIGraphicsEndPDFContext();
NSString *file = @"file.pdf"; //this is the file name.
NSArray<NSURL *> *dir = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
if (dir.count > 0){
    NSURL *path = [[dir firstObject] URLByAppendingPathComponent:file];
    [pdfData writeToFile:path.path atomically:false];
}

You can provide any formatting options you want in the withAttributes dictionary in the draw method.

And if you want multiple pages in your PDF just put this code in a loop

UIGraphicsBeginPDFPage();
[text drawInRect:UIGraphicsGetPDFContextBounds() withAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:17]}];
UIGraphicsEndPDFContext();

and change the text that draws. You can draw images too if you want them in your PDF. Check the created PDF enter image description here

Hope this helps.

nishith Singh
  • 2,968
  • 1
  • 15
  • 25