c#使用itextsharp输出pdf(动态填充表单内容,显示中文)
相关链接:
示例代码:
static void Main(string[] args) { BaseFont font = BaseFont.CreateFont(@"E:\Test Demo\Practice\PDF\Content\fonts\simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //获取PDF模板文件 string templateFile = @"E:\Test Demo\Practice\PDF\New_Blank_Document.pdf"; //输出生成的PDF文件 string tempPDF = @"E:\Test Demo\Practice\PDF\output.pdf"; //创建 PdfReader using (PdfReader reader = new PdfReader(templateFile)) { using (PdfStamper stamper = new PdfStamper(reader, new FileStream(tempPDF, FileMode.Create))) { stamper.Writer.CloseStream = false; //进行PDF字段操作 AcroFields form = stamper.AcroFields; //填充PDF里的字段内容 form.SetFieldProperty("Name", "textfont", font, null); form.SetField("Name", "水电费"); stamper.FormFlattening = true; } } Console.WriteLine("ok"); Console.ReadKey(); }
Demo链接