CGContextDrawPDFPage 显示白色或乱码文本
在更新我的 iPad 应用程序的过程中,我一直在尝试将现有 PDF 文档中的页面绘制到 Core Graphics 上下文中,然后将其另存为新的 PDF,但我很难让文本正确显示。新创建的 PDF 中的图像看起来很棒,但文本很少正确显示:更常见的是,它显示为白色/不可见或乱码。当文本不可见时,我仍然能够选择它应该在的位置并将其正确复制/粘贴到文本编辑器中。这是与 iPad 上可用字体数量有限有关的问题吗?
我的代码如下:
CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(dataProvider);
CGPDFPageRef page = CGPDFDocumentGetPage(document, pageNumberToRetrieve);
CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
UIGraphicsBeginPDFContextToFile(pathToFile, pageRect, nil);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPage(context, NULL);
// I don't think this line is necessary, but I have tried both with and without it.
CGContextSetTextDrawingMode (context, kCGTextFill);
CGContextDrawPDFPage(context, page);
CGContextEndPage(context);
UIGraphicsEndPDFContext();
CGDataProviderRelease(dataProvider);
CGPDFDocumentRelease(document);
如果有人有任何建议,我将不胜感激。
感谢您抽出时间。
抢
In the process of updating my iPad app I've been attempting to draw a page from an existing PDF document into a Core Graphics context then save it as a new PDF, but am having difficulty getting the text to display properly. Images in the newly-created PDF look great, but text rarely appears correctly: more often that not it appears white/invisible or garbled. When the text is invisible, I am still able to to select where it -should- be and copy/paste correctly into a text editor. Is this an issue related to the limited number of fonts available on the iPad?
My code is as follows:
CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(dataProvider);
CGPDFPageRef page = CGPDFDocumentGetPage(document, pageNumberToRetrieve);
CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
UIGraphicsBeginPDFContextToFile(pathToFile, pageRect, nil);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPage(context, NULL);
// I don't think this line is necessary, but I have tried both with and without it.
CGContextSetTextDrawingMode (context, kCGTextFill);
CGContextDrawPDFPage(context, page);
CGContextEndPage(context);
UIGraphicsEndPDFContext();
CGDataProviderRelease(dataProvider);
CGPDFDocumentRelease(document);
If anyone has any suggestions I would greatly appreciate hearing them.
Thanks for your time.
Rob
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
绘制到图像上下文中不会造成问题(文本显示正确)。
我想做的是创建一个新的 PDF 文件,其中仅包含原始 PDF 的几页。由于某种原因,文本似乎无法正确绘制到新文件中。
信息就在那里(我可以通过“猜测”文本应该在预览中的位置来选择文本),但它不会呈现。我假设 CGContextDrawPDFPage 将字符串写入 PDF 文件,但不绘制它,因为它不知道该字体的字符“看起来像什么”?
我认为 PDF 中嵌入字体的意义在于,即使系统(在本例中为 iPad)上未安装该字体,程序也能够执行此类操作。这是格式的限制,还是 Quartz 框架的限制?
Drawing into an image context does not pose a problem (text displays correctly).
What I am trying to do is create a -new- PDF file containing just a few pages from the original PDF. It seems that text does not draw correctly into the new file for some reason.
The information is there (I can select text by 'guessing' where it should be in Preview) but it doesn't render. I assume CGContextDrawPDFPage writes the string to the PDF file, but doesn't draw it because it doesn't know what the characters of that font 'look like'?
I thought the point of embedded fonts in PDFs was that programs would be able to perform these sorts of manipulations even if that font wasn't installed on the system (in this case, the iPad). Is this a limitation of the format, or the Quartz framework?
您想在屏幕上渲染吗?我不认为需要 UIGraphicsBeginPDFContextToFile?
但是,要在屏幕上渲染,您可以使用如下内容:
Do you want to render on the screen? I don't see the need for UIGraphicsBeginPDFContextToFile?
However, to render on the screen, you can use something like this: