我有一个包含中文/韩文字符的HTML字符串。我想转换的超文本标记语言到PDF使用iText。我读到我们需要将字体嵌入到PDF中,以使unicode字符显示在PDF中。
当我尝试嵌入wts11.ttf (编码为IDENTITY_H)或STSong-Light(编码为Light UCS2-H)时,我只能看到中文字符,但看不到韩文字符。我尝试使用arialuni.ttf (使用IDENTITY_H编码),但仍然只能看到中文字符,而不能看到韩语。
有没有人能告诉我什么是确切的字体。或者我是不是错过了什么。
以下是代码片段:
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);
发布于 2015-01-10 02:44:03
如果您升级到使用XML Worker,则有不同的方法来解决此问题。
我重用了官方示例中的代码,更具体地说是ParseHtmlAsian示例,并将用作此示例的源的超文本标记语言改编如下:
<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版本的许可证更昂贵。
发布于 2016-02-05 20:15:30
字体fontbold=FontFactory.getFont("assets/fonts/Malgun-Gothic-Bold_29380.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED,12);
https://stackoverflow.com/questions/27865360
复制相似问题