ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PDF 根据模板生成PDF

2021-04-08 11:03:22  阅读:11  来源: 互联网

标签:pdfStamper pdfReader BaseFont 生成 dict new PDF 模板


  /// <summary>
        /// 根据模板生成PDF
        /// </summary>
        /// <param name="templetPdf">PDF模板</param>
        /// <param name="newPdf">新生成的PDF</param>
        /// <param name="dict">字典</param>
        public static void CreatePdfByTemplet(string templetPdf, string newPdf,
            Dictionary<string, object> dict)
        {
            iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(templetPdf);
            FileStream fs = new FileStream(newPdf, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            PdfStamper pdfStamper = new PdfStamper(pdfReader, fs);
            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);
            foreach (var item in dict.Keys)
            {
                var value = dict[item];
                if (value != null)
                {
                    pdfStamper.AcroFields.SetField(item, value.ToString());

                }
            }
            pdfStamper.Close();
            pdfReader.Close();


        }

 <package id="iTextSharp" version="5.0.5" targetFramework="net472" /> 高版本的会报错 写法不一样

标签:pdfStamper,pdfReader,BaseFont,生成,dict,new,PDF,模板
来源: https://www.cnblogs.com/tangchun/p/14631217.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有