Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
550 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...