加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 04:29:31 所属栏目:百科 来源:网络整理
导读:我试图用c#插入一些unicode字符(阿拉伯语)到PDF格式我使用iTextSharp库但是当我插入字符并在PDF文件中保存字符时,unicode字符不会显示,直到我双击字符的位置应该出现. string pdfTemplate = @"c:po.pdf";string newFile = @"g:testcompleted_fw4.pdf";Pdf
我试图用c#插入一些unicode字符(阿拉伯语)到PDF格式我使用iTextSharp库但是当我插入字符并在PDF文件中保存字符时,unicode字符不会显示,直到我双击字符的位置应该出现.
string pdfTemplate = @"c:po.pdf";
string newFile = @"g:testcompleted_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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读