微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 使用unicode字符填写pdf表单

我试图用c#插入一些unicode字符(阿拉伯语)到PDF格式我使用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);

原文地址:https://www.jb51.cc/csharp/91362.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐