itext pdf 导出中缺少汉字

问题描述 投票:0回答:0

我一直面临在 Java 中使用 itext(itext 版本 2.1.7,jre 版本 - 1.8.0_282)在 PDF 导出中导出多语言文本的问题,代码的当前实现只设置了一种基于语言环境的字体,如下所示

Font font = new Font(Font.HELVETICA);
String pdfFont = i18nText(KEY_PDF_FONT);
BaseFont baseFont = BaseFont.createFont(pdfFont, pdfEncoding, BaseFont.NOT_EMBEDDED);

产生如下输出 Korean text used in this case, any text other than English(based on locale) was displayed with ??

我已将当前实现更改为使用 itext 的 FontSelector 以支持在同一 pdf 中导出多语言,并将缺少的 itext-asian-2.1.7.jar 文件包含到应用程序的 lib 路径中,并如下实现以支持英语, 中文, 日文, 韩文和希腊文

FontSelector selector=new FontSelector();
Font enFont = new Font(Font.HELVETICA);
BaseFont jpBaseFont = BaseFont.createFont("KozMinPro-Regular","UniJIS-UCS2-H", BaseFont.EMBEDDED);
BaseFont cBaseFont = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.EMBEDDED);
BaseFont kBaseFont = BaseFont.createFont("HYSMyeongJo-Medium","UniKS-UCS2-H", BaseFont.EMBEDDED);
Font jpFont = new Font(jpBaseFont);
Font cFont = new Font(cBaseFont);
Font kFont = new Font(kBaseFont);
selector.addFont(enFont);
selector.addFont(jpFont);
selector.addFont(cFont);
selector.addFont(kFont);


selector.process(text);

//将短语输出到文档中。

字体被添加到选择器并按预期进行处理,除了汉字,其中一些字体打印为空白。

正文 - 您的帐户已成功登录。

输出 Selected Text in line 3 and 4 after

如果我尝试从网上下载 jar 中嵌入的相同字体 - STSONG-Light 并像下面那样添加它

BaseFont cbf = BaseFont.createFont("STSong-light.ttf", "Identity-H", true);

渲染按预期进行 enter image description here 粗体字符是否是从中文字体以外的其他包含字体呈现的? 关于如何解决这个问题的任何想法?

使用 Microsoft edge 检查 PDF,因为我不允许访问 adobe acrobat。

java itext pdf-generation
© www.soinside.com 2019 - 2024. All rights reserved.