如何在用iTextSharp创建的pdf文件中显示中文字符 [英] How to display chinese characters in pdf file created with iTextSharp

查看:1265
本文介绍了如何在用iTextSharp创建的pdf文件中显示中文字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字符串数据,其中包含一些英文字符和一些中文字符。我正在使用iTextSharp创建包含此数据的pdf文件。创建pdf文件后,当我打开它时,pdf只包含英文字符。它没有显示汉字。
你能告诉我如何在pdf文件中显示中文字符吗?请注意,我写给pdf的字符串数据包含动态语言字符,有时是英语,有时是中文,有时是日语等。

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.

推荐答案

这在iText(夏普)文档中有说明。当你有一个包含不同语言的字形的字符串时,你需要使用<$ h $ => http://sourceforge.net/p/itextsharp/中的 FontSelector 。 code / HEAD / tree / trunk / book / iTextExamplesWeb / iTextExamplesWeb / iTextInAction2Ed / Chapter11 / FontSelectionExample.csrel =nofollow>这个例子。

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)); 

在这种情况下,我首先将Times Roman添加到字体选择器,然后是MSung-Light。现在 TEXT 中的所有英文字符都将出现在Times Roman中。您说缺少的字符将使用MSung-Light呈现。如果您更改了MSung-Light和Times Roman的顺序,完整的 TEXT 将在MSung-Light中呈现,因此请确保明智地选择字体。订单很重要,你没有定义字体的 TEXT 中的每个字符都将丢失。

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.

这篇关于如何在用iTextSharp创建的pdf文件中显示中文字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆