c#生成PDF准考证
项目中需要做一个生成PDF准考证的功能,在这里跟大家分享一下思路。。
1.首先是下载Adobe Acrobat 9 Pro,安装破解(高版本的貌似破解,不了,自带正版意识的略过。。随意下载)
2.新建模板。。下图是个演示版本,客户要求在一页显示四个准考证, 我实际上是做了4个模板,分别有1,2,3,4个准考证模板在上面
注意的是:一定不要设置错域属性,开发的时候,不小心把座位号 设置成了 图片域,死活填充不上数据其余都可以,。。。应该设置为文本域
3.查询数据库拿到考生信息,放到一个List集合中
4.以4为单位进行循环,分别找对应的准考证模板,进行数据填充,特别注意的是每次都要根据个数找到对应的模板进行填充
5.每次生成一个PDF文件,记录文件名
6.最后进行文件的合并。。
7.用到的插件 itextsharp,大家抖动
8.核心代码。。
1.传递考生信息进去,返回需要填充的数据。。。(这个地方,我创建的模板上 命名规则都是统一的,第一个准考证序号是 name1,第二个是name2....以此类推)
public Dictionary<string, string> ReadForm(List<ExamInfoEntity> list) { string pdfTemplate = string.Empty; string baseTemplatePath = @"D:\wangmj\fileserver\pdftemplate"; if (CheckUtil.CheckList(list))//根据传递集合元素个数,对应不同的PDF模板文件 { pdfTemplate = Path.Combine(baseTemplatePath, "zkztemplate" + list.Count + ".pdf"); } Dictionary<string, string> dic = new Dictionary<string, string>(); string basePath = ConfigurationManager.AppSettings["picpath"];// @"D:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0\webapps\pichouse\pic\pfile"; PdfReader pdfReader = null; try { pdfReader = new PdfReader(pdfTemplate); AcroFields pdfFormFields = pdfReader.AcroFields; string name = string.Empty;//姓名 string zkzh = string.Empty;//准考证号 string kaochang = string.Empty;//考场 string zhiyegongzhong = string.Empty; string jinengdengji = string.Empty; string zuoweihao = string.Empty; string identify = string.Empty; string workunit = string.Empty; string kstime = string.Empty; string ksaddress = string.Empty; string Image = string.Empty; string photopath = string.Empty; for (int i = 0; i < list.Count; ++i) { // name = string.Format("表单1[0].#subform[0].name{0}[0]", i + 1); zkzh = string.Format("表单1[0].#subform[0].zkzh{0}[0]", i + 1); kaochang = string.Format("表单1[0].#subform[0].kaochang{0}[0]", i + 1); zhiyegongzhong = string.Format("表单1[0].#subform[0].zhiyegongzhong{0}[0]", i + 1); jinengdengji = string.Format("表单1[0].#subform[0].jinengdengji{0}[0]", i + 1); zuoweihao = string.Format("表单1[0].#subform[0].zuoweihao{0}[0]", i + 1); identify = string.Format("表单1[0].#subform[0].identify{0}[0]", i + 1); workunit = string.Format("表单1[0].#subform[0].workunit{0}[0]", i + 1); kstime = string.Format("表单1[0].#subform[0].kstime{0}[0]", i + 1); ksaddress = string.Format("表单1[0].#subform[0].ksaddress{0}[0]", i + 1); Image = string.Format("表单1[0].#subform[0].Image{0}[0]", i + 1); dic.Add(name, list[i].name); dic.Add(zkzh, list[i].zkzh); dic.Add(kaochang, list[i].kaochang); dic.Add(zhiyegongzhong, list[i].zhiyegongzhong); dic.Add(jinengdengji, list[i].jinengdengji); dic.Add(zuoweihao, list[i].seat); dic.Add(identify, list[i].identify); dic.Add(workunit, list[i].workunit); dic.Add(kstime, list[i].kstime); dic.Add(ksaddress, list[i].ksaddress); photopath = Path.Combine(basePath, list[i].photo); dic.Add(Image, photopath); } } catch (Exception ex) { //LogHelper.Error(ex.Message); } finally { if (pdfReader != null) { pdfReader.Close(); } } return dic; }
2.填充pdf模板。。
/// <summary> /// 填充模板 /// </summary> /// <param name="pdfTemplate">pdf模板的全路径</param> /// <param name="newFile">生成的pdf路径</param> /// <param name="dic"></param> public static void FillForm(string pdfTemplate, string newFile, Dictionary<string, string> dic) { PdfReader pdfReader = null; PdfStamper pdfStamper = null; try { pdfReader = new PdfReader(pdfTemplate); pdfStamper = new PdfStamper(pdfReader, new FileStream( newFile, FileMode.Create)); AcroFields pdfFormFields = pdfStamper.AcroFields; //设置支持中文字体 BaseFont baseFont = BaseFont.CreateFont("C:\\WINDOWS\\FONTS\\STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); pdfFormFields.AddSubstitutionFont(baseFont); foreach (KeyValuePair<string, string> de in dic) { if (de.Key.Contains("Image")) { insertImage(pdfStamper, pdfFormFields, de.Key, de.Value); } else { pdfFormFields.SetField(de.Key, de.Value); } } pdfStamper.FormFlattening = true; } catch (Exception ex) { //LogHelper.Error(ex.Message); } finally { if (pdfStamper != null) { pdfStamper.Close(); } if (pdfReader != null) { pdfReader.Close(); } } }
3.填充图片的方法
/// <summary> /// 填充图片 /// </summary> /// <param name="ps"></param> /// <param name="s">域集合?</param> /// <param name="filedname">图片域的名称</param> public static void insertImage(PdfStamper ps, AcroFields s, string filedname, string imagefilename) { try { List<AcroFields.FieldPosition> list = (List<AcroFields.FieldPosition>)s.GetFieldPositions(filedname); Rectangle signRect = list[0].position; Image image = Image.GetInstance(imagefilename); PushbuttonField pushbuttonField = s.GetNewPushbuttonFromField(filedname); pushbuttonField.Image = (image); PdfFormField editFormField = pushbuttonField.Field; s.ReplacePushbuttonField(filedname, editFormField); } catch (Exception e) { // TODO Auto-generated catch block } }
合并PDF文件的方法
private string MergePDF(List<string> list) { string basePath = @"D:\fileserver\pdfzkz"; string save_path = Path.Combine(basePath, "准考证.pdf"); Document document = new Document(); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(save_path, FileMode.Create)); document.Open(); PdfContentByte cb = writer.DirectContent; PdfImportedPage newPage; PdfReader readert = null; List<PdfReader> readerList = new List<PdfReader>(); for (int k = 0; k < list.Count; k++) { readert = new PdfReader(list[k]); document.NewPage(); int iPageNum = readert.NumberOfPages; for (int j = 1; j <= iPageNum; j++) { document.NewPage(); newPage = writer.GetImportedPage(readert, j); cb.AddTemplate(newPage, 0, 0); } readerList.Add(readert); } try { if (document.IsOpen()) { document.Close(); } foreach (var rd in readerList)//清理占用 { rd.Dispose(); } } catch(Exception ex) { } return save_path; }