CGContextDrawPDFPage 显示白色或乱码文本

发布于 2024-09-03 20:40:53 字数 965 浏览 7 评论 0原文

在更新我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光的暖冬 2024-09-10 20:40:53

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

我想做的是创建一个新的 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?

吻风 2024-09-10 20:40:53

您想在屏幕上渲染吗?我不认为需要 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();
  }

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:

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();
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文