我有一个带有中文/韩文字符的 HTML 字符串。我想使用 iText 将 HTML 转换为 PDF。我已经读到我们需要将 FONT 嵌入到 PDF 中以使 unicode 字符显示在 PDF 上。
当我尝试嵌入 wts11.ttf(使用编码 IDENTITY_H)或 STSong-Light(使用 encodingUniGB-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);