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

使用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语句,关闭释放文件。

尝试使用宋体没有成功。

发布了181 篇原创文章 · 获赞 35 · 访问量 76万+

猜你喜欢

转载自blog.csdn.net/dacong/article/details/89433271