PDFBox 嵌入的 TTF 字体不起作用 [英] PDFBox embedded TTF fonts not working

查看:109
本文介绍了PDFBox 嵌入的 TTF 字体不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PDFBox 从现有的 PDF 模板构建文档,因此它会打开文件,向其中添加文本并保存.它运行良好,除非尝试使用外部 TTF 字体.我尝试了不同的方法并搜索了 2 天的解决方案,但 PDFBox 上的内容并不多.

I am using PDFBox to build a document from an existing PDF template, so it opens the file, adds text to it, and saves it. It works well, except when trying to use external TTF fonts. I have tried different things and searched for 2 days for solutions, but there's not much out there on PDFBox.

这是一些代码,使用字体 "Tardy Kid" 因为它不会被误认为是其他任何东西,也不可能是任何标准库的一部分.

Here's some code, using the font "Tardy Kid" because it can't be mistaken for anything else, and is not likely to be part of any standard library .

代码执行良好,显示 println 中的TardyKid"(显示字体已加载且名称可获取),并显示文本 -- 但它是 Helvetica.使用 getStringWidth() 计算宽度的代码中更复杂的部分似乎也表明宽度表已成功加载.它只是无法正确显示.

The code executes fine, displays "TardyKid" from the println (showing that the font is loaded and the name is gettable), and displays the text -- but it's in Helvetica. More sophisticated parts of the code that use getStringWidth() to calculate width seem to indicate successful loading of the width tables too. It just doesn't display correctly.

该代码在打开现有 PDF 文档(模板)并向其添加文本的更大程序的上下文中运行.除了

The code runs in the context of a larger program that opens an existing PDF document (a template) and adds text to it. It all seems to work fine except for

 public void setText ( PDDocument document, String text ) throws IOException {
     int lastPage = document.getNumberOfPages() - 1;
     PDPage page = (PDPage) document.getDocumentCatalog().getAllPages().get(lastPage);
     PDPageContentStream contentStream = null;
     try {
         contentStream = new PDPageContentStream(document,page,true,true,false);
         File fontFile = new File(m_fontDir, "Tardy_Kid.ttf");
         PDFont font = PDTrueTypeFont.loadTTF(document, fontFile);
         Color color =  new Color(196, 18, 47);
         float x = 100f, y = 700f;
         System.out.println(font.getBaseFont());
         contentStream.setFont(font, 32);
         contentStream.setNonStrokingColor(color);
         contentStream.beginText();
         contentStream.moveTextPositionByAmount(x,y);
         contentStream.drawString(text);
         contentStream.endText();
     } finally {
         if (contentStream != null) {
             contentStream.close();
         }
     }
 }

推荐答案

我找到了答案.我不确定这是否是 PDFBox 中的错误,但如果您在同一页面上多次打开/关闭内容流(由 PDPageContentStream 返回),则无法正常工作.因此,当在页面上多次调用该例程时,在 setText 例程中打开/关闭内容流不起作用.将流移出例程并为整个页面打开/关闭一次似乎解决了这个问题(以及其他几个问题).

I have found the answer. I am not sure if this is a bug in PDFBox or not, but if you open/close a content stream (returned by PDPageContentStream) more than once on the same page, it doesn't work correctly. So having the content stream open/close inside the setText routine did not work when the routine was called more than once on a page. Moving the stream outside the routine and opening/closing it once for the whole page seemed to clear up this problem (and a couple of others).

这在文档或示例代码的任何地方都没有提到,充其量是非常微妙的.我将其称为错误,特别是因为它有效"(不抛出任何异常)但会在页面上产生不确定和/或错误的结果.

This is not mentioned anywhere in the documentation or example code, and is very subtle at best. I would call it a bug, especially since it "works" (does not throw any exceptions) but creates indeterminate and/or wrong results on the page.

这篇关于PDFBox 嵌入的 TTF 字体不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆