使用子集化TrueType字体时PDFBox表单字段显示乱码问题求助
使用子集化TrueType字体时PDFBox表单字段显示乱码问题求助
问题背景
我在使用Apache PDFBox(2.x/3.x版本)创建带表单字段的PDF时卡壳了:当我嵌入自定义TrueType字体(比如FreeSans.ttf)并启用子集化后,给文本字段设置的值会显示成乱码。但如果用PDF标准的14种字体就完全没问题,只要是外部加载的字体就会出状况。
可复现代码
下面是能100%重现问题的完整代码:
package org.example; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDResources; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.font.PDType0Font; import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm; import org.apache.pdfbox.pdmodel.interactive.form.PDTextField; import java.io.InputStream; public class Main { public static void main(String[] args) { try (PDDocument document = new PDDocument()) { PDPage page = new PDPage(PDRectangle.A4); document.addPage(page); PDAcroForm acroForm = new PDAcroForm(document); document.getDocumentCatalog().setAcroForm(acroForm); // 加载外部TrueType字体并启用子集化 PDType0Font font; try (InputStream fontStream = Main.class.getResourceAsStream("/FreeSans.ttf")) { if (fontStream == null) { throw new RuntimeException("FreeSans.ttf not found in resources"); } font = PDType0Font.load(document, fontStream, true); } // 将字体添加到表单资源 PDResources formResources = new PDResources(); String fontResourceName = formResources.add(font).getName(); acroForm.setDefaultResources(formResources); acroForm.setDefaultAppearance("/" + fontResourceName + " 12 Tf 0 0 0 rg"); // 创建带值的文本字段 PDTextField textField = new PDTextField(acroForm); textField.setPartialName("testField"); textField.setDefaultAppearance("/" + fontResourceName + " 12 Tf 0 0 0 rg"); // 设置组件矩形并添加到页面 textField.getWidgets().get(0).setRectangle(new PDRectangle(100, 700, 200, 20)); textField.getWidgets().get(0).setPage(page); page.getAnnotations().add(textField.getWidgets().get(0)); acroForm.getFields().add(textField); textField.setValue("Hello, this is a filled form field!"); acroForm.setNeedAppearances(false); acroForm.refreshAppearances(); document.save("output.pdf"); } catch (Exception e) { e.printStackTrace(); } } }
控制台警告信息
运行代码时控制台会弹出这两条警告:
WARNING: Using fallback font LiberationSans for CID-keyed TrueType font FreeSans WARNING: widget of field testField has no rectangle, no appearance stream created
我排查过的方向
- 确认字体文件确实在资源目录里,能正常被加载到
- 试过关闭子集化(把
PDType0Font.load的第三个参数改成false),此时乱码问题直接消失,但我想保留子集化来压缩PDF体积 - 反复检查了默认外观字符串的格式,看起来是符合规范的:
/[字体资源名] 12 Tf 0 0 0 rg
有没有大佬能帮我捋捋问题出在哪?怎么才能在启用子集化的前提下,让外部TrueType字体的表单字段正常显示内容?




