Mohit Shah
Mohit Shah

Reputation: 162

How to display chinese characters in pdf file created with iTextSharp

I have string data which contains some english characters and some chinese characters. I m creating a pdf file with this data using iTextSharp. After pdf file is created, when i open it, pdf contains only english characters. It is not showing chinese characters. Can you please tell me how to display chinese characters in pdf file?. Please note that the string data that i m writing to pdf contains dynamic language characters i.e sometimes english, somethimes chinese, sometimes japanese and so on.

Upvotes: 4

Views: 8732

Answers (2)

Mitko Keckaroski
Mitko Keckaroski

Reputation: 1040

"SIMSUN.ttf" works for me. You can download it from the web. https://www.wfonts.com/font/simsun

enter image description here

Upvotes: 0

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

This is explained in the iText(Sharp) documentation. When you have a String with glyphs from different languages, you need to use a FontSelector as shown in this example.

FontSelector selector = new FontSelector();
selector.AddFont(FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12));
selector.AddFont(FontFactory.GetFont("MSung-Light", "UniCNS-UCS2-H", BaseFont.NOT_EMBEDDED));
Phrase ph = selector.Process(TEXT);
document.Add(new Paragraph(ph)); 

In this case, I add Times Roman to the font selector first, following by MSung-Light. Now all the English characters in TEXT will be in Times Roman. The characters you say are missing will be rendered using MSung-Light. If you change the order of MSung-Light and Times Roman, the complete TEXT will be rendered in MSung-Light, so make sure you choose your fonts wisely. The order matters, and every character in your TEXT for which you didn't define a font will be missing.

Upvotes: 6

Related Questions