一下载此软件Adobe Acrobat X Pro 编辑PDF,将控件拖拽到页面,然后使用下面工作方法生成模板。 提示在生成过程中看不到替换值问题,其实就是模板设置字体问题。换个字体就好了 里面有2个方法 处理中文那个很重要,对于我们中文用户来说。其中iTextAsianCmaps.dll是不需要VS里添加引用的。是在代码调用的时候动态添加 关于iTextAsianCmaps.dll,iTextSharp.dll,iTextAsian.dll下载问题在下面链接里面查看操作 http://www.360doc.com/showWeb/0/0/526823341.aspx 关于效果 ![]() using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using iTextSharp.text.pdf; using System.IO; using iTextSharp.text; namespace WebApplication2 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { var templatePath = Server.MapPath("~/File/合同评审表.pdf"); var newFilePath = Server.MapPath("~/File/88888.pdf"); var dic = new Dictionary<string, string>(); dic.Add("Text1", "123"); dic.Add("Text2", "我是好好的"); dic.Add("Text3", "我在测试"); GetEnPdf(templatePath, newFilePath, dic); } /// <summary> /// 根据模板生成PDF /// </summary> /// <param name="templatePath">模板文件(全路径 E:\)</param> /// <param name="newFilePath">新生成文件(全路径E:\)</param> /// <param name="parameters">数据字典</param> public void GetEnPdf(string templatePath, string newFilePath, Dictionary<string, string> parameters) { PdfReader pdfReader = new PdfReader(templatePath); PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFilePath, FileMode.Create)); AcroFields pdfFormFields = pdfStamper.AcroFields; // AcroFields s = pdfStamper.get //为需要赋值的域设置值; 19: foreach (KeyValuePair<string, string> parameter in parameters) { pdfFormFields.SetField(parameter.Key, parameter.Value,parameter.Value); //pdfFormFields.SetFieldRichValue(parameter.Key, parameter.Value); } pdfStamper.FormFlattening = false; pdfStamper.Close(); pdfReader.Close(); } /// <summary> /// 处理中文 /// </summary> /// <param name="templatePath"></param> /// <param name="newFilePath"></param> /// <param name="iTextAsianCmapsPath"></param> /// <param name="parameters"></param> public static void GetChPdf(string templatePath, string newFilePath, string iTextAsianCmapsPath, Dictionary<string, string> parameters) { PdfReader pdfReader = new PdfReader(templatePath); PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFilePath, FileMode.Create)); //获取域的集合; 34: AcroFields pdfFormFields = pdfStamper.AcroFields; BaseFont.AddToResourceSearch(iTextAsianCmapsPath); //创建中文字体,第一个参数是中文字体的路径,第二个参数表示文字方向水平,第三个貌似是字体嵌入PDF文件; BaseFont baseFT = BaseFont.CreateFont(@"C:\Windows\Fonts\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); foreach (KeyValuePair<string, string> parameter in parameters) { //要输入中文就要设置域的字体; pdfFormFields.SetFieldProperty(parameter.Key, "textfont", baseFT, null); //为需要赋值的域设置值; 44: pdfFormFields.SetField(parameter.Key, parameter.Value); } //这句很重要,如果为false那么生成的PDF文件还能编辑,一定要设为true; pdfStamper.FormFlattening = true; pdfStamper.Close(); pdfReader.Close(); } /// <summary> /// 打开pdf /// </summary> /// <param name="filename"></param> public void OutFile(string filename) { System.IO.FileInfo file = new System.IO.FileInfo(filename); Response.Clear(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); Response.AddHeader("Content-Length", file.Length.ToString()); Response.ContentType = "application/x-bittorrent"; Response.WriteFile(file.FullName); Response.End(); } protected void Button2_Click(object sender, EventArgs e) { var templatePath = Server.MapPath("~/File/合同评审表.pdf"); var newFilePath = Server.MapPath("~/File/88888999.pdf"); var dic = new Dictionary<string, string>(); dic.Add("Text1", "123"); dic.Add("Text2", "我是好好的"); dic.Add("Text3", "我在测试"); // string template = Server.MapPath("~/PDFTemplate/ch.pdf"); //string newFile = Server.MapPath("~/PDFTemplate") + "//" + Session["UserID"].ToString() + ".pdf"; string iTextAsianCmaps = Server.MapPath("~/Libs/iTextAsianCmaps.dll"); GetChPdf(templatePath, newFilePath, iTextAsianCmaps, dic); OutFile(newFilePath); } } } |
|
来自: 实力决定地位 > 《C# 相关文件操作》