将PDF文本写入iOS中的PDFContext [英] Write PDF text to a PDFContext in iOS
问题描述
通过以下方式绘制到pdf上下文非常简单:
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...
但是文字是明确栅格化...是否还有将 pdf 样式文本添加到pdf文件?所以用户可以选择它,复制它等。可能通过核心图形以外的库?
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?
推荐答案
这绝对是可能的。 这是一个教程,展示了如何在iOS 5中生成和显示带有文本的PDF文档。
It's definitely possible. Here's a tutorial that shows how to generate and display a PDF document, with text, in iOS 5.
当我下载并运行项目时,放大时,文本似乎没有光栅化。当我在OS X中预览中打开生成的PDF时,我可以选择并复制文本。
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.
此示例使用用于将文本绘制到CGContext中的核心文本。应该很容易改变 + [PDFRenderer drawText]
尝试其他方法,如果你想试验。
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];
这篇关于将PDF文本写入iOS中的PDFContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!