//生成PDF if (File.Exists(pdfPath)) { File.Delete(pdfPath); } using (FileStream desStream = new FileStream(pdfPath, FileMode.Create)) { PdfCopyFields copyFields = new PdfCopyFields(desStream); copyFields.SetFullCompression(); for (int item = 0; item < page; item++) { String temporaryPath = AppDomain.CurrentDomain.BaseDirectory + "testDes" + item.ToString() + ".pdf";//临时文件 //創建PdfStamper PdfReader reader = new PdfReader(TEMPLATE_NM); PdfStamper stamp = new PdfStamper(reader, new FileStream(temporaryPath, FileMode.OpenOrCreate)); // fill out the form template fields AcroFields fields = stamp.AcroFields; //ヘーダ fillHeaderField(fields, dt); //テーブル fillTableField(fields, listData, item + 1, 20); stamp.FormFlattening = true; stamp.Close(); //コピーpdf copyFields.AddDocument(new PdfReader(temporaryPath)); } copyFields.Close(); } fillHeaderField方法: //ヘーダ private void fillHeaderField(AcroFields fields, Dictionary<string, string> dt) { BaseFont font = getBaseFont(); foreach (KeyValuePair<string, string> pair in dt) { fields.SetFieldProperty(formNM+pair.Key, T_FONT, font, null); fields.SetField(pair.Key, pair.Value); } } fillTableField方法,用两种方法实现表格数据的套打 1)代码生成表格 2)套用模板的表格 //テーブル private void fillTableField(AcroFields fields, List<List<string>> list, int page, int pageInRows) { //1 //BaseFont font = getBaseFont(); //for (int i = 0; i < pageInRows; i++) //{ // if (i >= list.Count) break; // List<string> al = list[i]; // for (int j = 0; j < al.Count; j++) // { // StringBuilder sb = new StringBuilder(); // sb.Append("tblNaiyou"); // sb.Append(".Row" + (i + 1)); // sb.Append(".Cell" + (j + 1)); // fields.SetFieldProperty(sb.ToString(), T_FONT, font, null); // fields.SetField(sb.ToString(), al[j]); // } //} //2 //付加表 PdfPTable table = new PdfPTable(11); table.WidthPercentage = 450; table.SetTotalWidth(new float[11] { 45, 27, 36, 81, 54, 54, 54, 54, 54, 54, 34 }); table.DefaultCell.BorderWidth = 0; table.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT; //遍历原table的内容 for (int i = 0; i < pageInRows; i++) { if ((page - 1) * pageInRows + i >= list.Count) break; List<string> al = list[(page - 1) * pageInRows + i]; if (i % 10 == 0 && i != 0) { for (int j = 0; j < 11; j++) { if (j == 0 || j == 1 || j == 2 || j == 3) { Phrase phrase = new Phrase(al[j], font); PdfPCell pdfPCell = new PdfPCell(phrase); pdfPCell.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right pdfPCell.Border = Rectangle.TOP_BORDER; table.AddCell(pdfPCell); } else { Phrase phrase = new Phrase(al[j], font); PdfPCell pdfPCell = new PdfPCell(phrase); pdfPCell.HorizontalAlignment = 2; //0=Left, 1=Centre, 2=Right pdfPCell.Border = Rectangle.TOP_BORDER; table.AddCell(pdfPCell); } } } else { for (int j = 0; j < 11; j++) { if (j == 0 || j == 1 || j == 2 || j == 3) { Phrase phrase = new Phrase(al[j], font); PdfPCell pdfPCell = new PdfPCell(phrase); pdfPCell.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right pdfPCell.Border = Rectangle.NO_BORDER; table.AddCell(pdfPCell); } else { table.AddCell(new Phrase(al[j], font)); } } } } PdfContentByte underContent = stamp.GetOverContent(1);//.GetUnderContent(i); table.WriteSelectedRows(0, 19, 27, 697, underContent); }