Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 908
Christopher Guray Avatar asked Nov 03 '22 21:11

Christopher Guray


1 Answers

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.

like image 66
mkl Avatar answered Nov 08 '22 03:11

mkl