How to display chinese characters in pdf file created with iTextSharp

让人想犯罪 __ 提交于 2019-12-02 01:32:02

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/14999403/how-to-display-chinese-characters-in-pdf-file-created-with-itextsharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!