Ralphleon
Ralphleon

Reputation: 4108

Write PDF text to a PDFContext in iOS

It's pretty trivial to draw to a pdf context via:

UIGraphicsBeginPDFContextToFile(pdfFile, CGRectZero, nil);
UIGraphicsBeginPDFPageWithInfo(sheet.frame, nil);    
CGContextRef ctx = UIGraphicsGetCurrentContext();

CGRect text_rect = ...
NSFont *font = ...
NSString * str = @"foo";
[str drawInRect:text_rect withFont:font];

...draw to context...

However the text is clearly rasterized... Is there anyway to actually add pdf style text to a pdf file? So the user can select it, copy it, etc. Possibly through a library other than core graphics?

Upvotes: 3

Views: 3873

Answers (1)

Kurt Revis
Kurt Revis

Reputation: 27984

It's definitely possible. Here's a tutorial that shows how to generate and display a PDF document, with text, in iOS 5.

When I downloaded and ran the project, the text did not appear to be rasterized when I zoomed in. When I opened the generated PDF in Preview in OS X, I could select and copy the text.

This example uses Core Text to draw the text into the CGContext. Should be easy to change +[PDFRenderer drawText] to try other methods, if you'd like to experiment.

For instance, I changed it to use code similar to yours, and it worked fine:

CGRect text_rect = { 0, 100, 300, 50 };
UIFont *font = [UIFont systemFontOfSize:12];
NSString * str = @"Added foo bar";
[str drawInRect:text_rect withFont:font];

Upvotes: 1

Related Questions