首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iText不渲染中文/朝鲜语字符

iText不渲染中文/朝鲜语字符
EN

Stack Overflow用户
提问于 2015-01-10 00:52:16
回答 2查看 2.8K关注 0票数 1

我有一个包含中文/韩文字符的HTML字符串。我想转换的超文本标记语言到PDF使用iText。我读到我们需要将字体嵌入到PDF中,以使unicode字符显示在PDF中。

当我尝试嵌入wts11.ttf (编码为IDENTITY_H)或STSong-Light(编码为Light UCS2-H)时,我只能看到中文字符,但看不到韩文字符。我尝试使用arialuni.ttf (使用IDENTITY_H编码),但仍然只能看到中文字符,而不能看到韩语。

有没有人能告诉我什么是确切的字体。或者我是不是错过了什么。

以下是代码片段:

代码语言:javascript
运行
复制
Document document = new Document();
Paragraph paragraph=new Paragraph();
PdfWriter.getInstance(document, baos);
document.open();
BaseFont bff = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
Font f = new Font(bff);

// FontFactory.registerDirectories(); 
// Font f = FontFactory.getFont("Arial Unicode MS", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

document.add(new Paragraph());
HTMLWorker htmlWorker = new HTMLWorker(document);

List<Element> objects=htmlWorker.parseToList(new StringReader(message),null);
paragraph.setFont(f);
for (Element elem : objects) {
    paragraph.add(elem);
}
document.add(paragraph);
EN

回答 2

Stack Overflow用户

发布于 2015-01-10 02:44:03

如果您升级到使用XML Worker,则有不同的方法来解决此问题。

我重用了官方示例中的代码,更具体地说是ParseHtmlAsian示例,并将用作此示例的源的超文本标记语言改编如下:

代码语言:javascript
运行
复制
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
    <p><span style="font-size:12.0pt; font-family:MS Mincho">長空</span>
    <span style="font-size:12.0pt; font-family:Times New Roman,serif">(Broken Sword),</span>
    <span style="font-size:12.0pt; font-family:MS Mincho">秦王殘劍</span>
    <span style="font-size:12.0pt; font-family:Times New Roman,serif">(Flying Snow),</span>
    <span style="font-size:12.0pt; font-family:MS Mincho">飛雪</span>
    <span style="font-size:12.0pt; font-family:Times New Roman,serif">(Moon), </span>
    <span style="font-size:12.0pt; font-family:MS Mincho">如月</span>
    <span style="font-size:12.0pt; font-family:Times New Roman,serif">(the King), and</span>
    <span style="font-size:12.0pt; font-family:MS Mincho">秦王</span>
    <span style="font-size:12.0pt; font-family:Times New Roman,serif">(Sky).</span></p>
    <p style="font-size: 12.0pt; font-family:Batang">빈집</p>
    <p>Test</p>
    </body>
</html>

结果如下所示:

如你所见,所有文本都被正确渲染,所以请不要散布诸如"iText未渲染中文/韩文字符“等错误消息;-)

请将此答案转发给您的管理层,以便您的首席技术官明白,花费时间购买旧的iText版本比购买使用新的iText版本的许可证更昂贵。

票数 1
EN

Stack Overflow用户

发布于 2016-02-05 20:15:30

  1. 下载Malgun-Gothic-Bold_29380.ttf字体。
  2. 将其存储在资产->字体->Malgun-Gothic Bold_29380.ttf font
  3. 此代码将适用于中日韩、英语和vitenames

字体fontbold=FontFactory.getFont("assets/fonts/Malgun-Gothic-Bold_29380.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED,12);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27865360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档