C# 用unicode字符填充pdf表单

C# 用unicode字符填充pdf表单,c#,unicode,itextsharp,C#,Unicode,Itextsharp,我试图用c#在PDF表单中插入一些unicode字符(阿拉伯语),我使用了iTextSharp库,但是当我插入字符并将字符保存在PDF文件中时,unicode字符不会显示出来,直到我双击应该显示的字符的位置 string pdfTemplate = @"c:\po.pdf"; string newFile = @"g:\test\completed_fw4.pdf"; PdfReader pdfReader = new PdfReader(pdfTemplate); PdfStamper pdf

我试图用c#在PDF表单中插入一些unicode字符(阿拉伯语),我使用了iTextSharp库,但是当我插入字符并将字符保存在PDF文件中时,unicode字符不会显示出来,直到我双击应该显示的字符的位置

string pdfTemplate = @"c:\po.pdf";
string newFile = @"g:\test\completed_fw4.pdf";
PdfReader pdfReader = new PdfReader(pdfTemplate);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
pdfFormFields.SetField("position", TextBox1.Text);
pdfStamper.FormFlattening = false;
// close the pdf
pdfStamper.Close(); 

有几种方法可以解决这个问题,但最终需要指定一种能够呈现Unicode内容的字体

首先,创建一个指向Unicode字体的
BaseFont
对象,下面我使用的是Arial Unicode:

var arialFontPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
var arialBaseFont = BaseFont.CreateFont(arialFontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
然后,您可以在每个字段上分别设置字体属性:

pdfFormFields.SetFieldProperty("position", "textfont", arialBaseFont, null);
也可以添加文档范围的替换字体:

pdfFormFields.AddSubstitutionFont(arialBaseFont);

这使得pdf文件非常大,从2MB跳到17MB。Arial Unicode MS支持50000+字形,这就是它如此大的原因。PDF标准(不仅仅是iText)没有为非英语语言提供字形,因此您需要提供一种这样的字体。你可以选择任何你想要的字体,我只是以Arial Unicode MS为例。有什么办法吗?是的,选择不同的字体。奇怪的是,一方面,操作系统中可用的字体需要嵌入到应用程序中才能使用,另一方面,这样的字体(显然)在最常用的PDF生成框架中,使用字体的简单任务非常复杂(应该理解为不简单)。我的意思是,谢谢!:)