Christopher Guray
Christopher Guray

Reputation: 223

iText to generate PDF with Chinese characters, but not displayed

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?

Upvotes: 5

Views: 7028

Answers (2)

Yisu Zhang
Yisu Zhang

Reputation: 89

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

This works for me, you can have a try.

Upvotes: 0

mkl
mkl

Reputation: 95898

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.

Upvotes: 2

Related Questions