5
votes

I'm using iText to create a PDF with Chinese characters. The Font I specified is MS Mincho which I had loaded using the code,

FontFactory.registerDirectory("c:/windows/Fonts/");

new Phrase("Asian 汉字/漢字 characters", FontFactory.getFont("MS Mincho", 16, Font.NORMAL));

The code below retrieves appropriately the MS Mincho font (i.e. not null),

FontFactory.getFont("MS Mincho", 16, Font.NORMAL)

However, the generated PDF only displays the ASCII text "Asian characters", i.e. the chinese characters are not displayed on the PDF.

Any idea as to why the chinese characters are missing on the generated PDF?

2
You should make sure that you a) use the font with an appropriate encoding (BaseFont.IDENTITY_H) and b) embed it (BaseFont.EMBEDDED). Be inspired by the samples from chaptrer 11 of iText in Action — 2nd Edition.mkl
Thanks. The generated PDF now displays the Chinese characters properly. No need to call the FontFactory.registerDirectory(). Just use BaseFont.createFont("c:/windows/Fonts/MSMINCHO.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED) and instantiate Font(BaseFont, size). FontFactory.getFont("MS Mincho") does not work with Chinese characters.Christopher Guray
Actually FontFactory can likewise be used to create appropriately parameterized font objects. But true, you don't need it.mkl

2 Answers

2
votes

As the issue has been resolved in comments, here the resolution:

You should make sure that you a) use the font with an appropriate encoding (BaseFont.IDENTITY_H) and b) embed it (BaseFont.EMBEDDED). Be inspired by the samples from chapter 11 of iText in Action — 2nd Edition.

BaseFont bf = BaseFont.createFont("c:/windows/Fonts/MSMINCHO.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font f = Font(bf, size);

Now use this Font f.

0
votes
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

This works for me, you can have a try.