【问题标题】:CGContextDrawPDFPage displays white or garbled textCGContextDrawPDFPage 显示白色或乱码文本
【发布时间】:2010-06-02 12:53:30
【问题描述】:

在更新我的 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);

如果有人有任何建议,我将不胜感激。

感谢您的宝贵时间。

罗伯

【问题讨论】:

    标签: pdf text ipad cgcontext


    【解决方案1】:

    在图像上下文中绘图不会造成问题(文本显示正确)。

    我要做的是创建一个 -new- PDF 文件,其中仅包含原始 PDF 中的几页。由于某种原因,文本似乎没有正确绘制到新文件中。

    信息在那里(我可以通过“猜测”它应该在预览中的位置来选择文本)但它不会呈现。我假设 CGContextDrawPDFPage 将字符串写入 PDF 文件,但不绘制它,因为它不知道该字体的字符“看起来像”什么?

    我认为 PDF 中嵌入字体的意义在于,即使系统(在本例中为 iPad)上未安装该字体,程序也能够执行此类操作。这是格式的限制,还是 Quartz 框架的限制?

    【讨论】:

      【解决方案2】:

      你想在屏幕上渲染吗?我认为不需要 UIGraphicsBeginPDFContextToFile? 但是,要在屏幕上呈现,您可以使用以下内容:

      pageReference = CGPDFDocumentGetPage(pdfReference, page);
      
        CGContextRef context = UIGraphicsGetCurrentContext();
        @try {
          CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
          CGContextScaleCTM(context, scale, -scale);
      
          CGContextSaveGState(context);
          @try {
            CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(pageReference, kCGPDFCropBox, self.bounds, 0, true);
            CGContextConcatCTM(context, pdfTransform);
            CGContextDrawPDFPage(context, pageReference);
          }
          @finally {
            CGContextRestoreGState(context);
          }
        }
        @finally {
          UIGraphicsEndImageContext();
        }
      

      【讨论】:

      • “在更新我的 iPad 应用程序的过程中,我一直在尝试将现有 PDF 文档中的页面绘制到 Core Graphics 上下文中,然后将其另存为新的 PDF,[...]”
      • 早上 8 点 15 分对我来说真的很安静 。说真的,你看过《Quartz 2d 编程指南:PDF 文档创建、查看和转换》文档吗?如果没有,这里有一个关于如何创建文档、将数据呈现到其上下文并保存该文件的代码示例。
      猜你喜欢
      • 2015-08-09
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      • 2021-01-02
      • 1970-01-01
      • 2019-12-14
      相关资源
      最近更新 更多