itextsharp解决"STSong-Light' with 'UniGB-UCS2-H' is not recognized"问题

36 篇文章 1 订阅

使用iTextSharp根据PDF模板生成复杂格式的PDF文件

itextsharp解决"STSong-Light' with 'UniGB-UCS2-H' is not recognized"问题

使用最新的版本解决。

设置PDF模板时,文本域注意字体,不要使用默认的字体,我是使用黑体字。文本域使用无边框。

//代码参考网络上的

 string tempFilePath = "D:\\mydata.pdf";//最终的PDF文件
            iTextSharp.text.pdf.PdfDocument document = new iTextSharp.text.pdf.PdfDocument();

            iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(@"D:\d.pdf");//模板文件
            PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(tempFilePath, FileMode.OpenOrCreate));
            AcroFields pdfFormFields = pdfStamper.AcroFields;
            pdfStamper.FormFlattening = true;

            //BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            BaseFont simheiBase = BaseFont.CreateFont(@"C:\Windows\Fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

            pdfFormFields.AddSubstitutionFont(simheiBase);
            pdfStamper.AcroFields.SetField("Names", "李朝强");
            pdfStamper.AcroFields.SetField("chk", "yes");//对象名称不存在时不会报错,返回false.
            pdfStamper.Close();
            pdfReader.Close();

//为防止中途代码错误造成文件被锁定,可以使用using语句,关闭释放文件。

尝试使用宋体没有成功。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值