c#调用itextsharp进行Pdf报表设计
2010-05-18 10:06
381 查看
//生成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); }
相关文章推荐
- C# 使用 itextsharp 对pdf文件进行分割的几种方法(1)——按页码拷贝分割
- C#调用iTextSharp组件生成PDF文件
- 用itextsharp进行PDF报表
- C#使用itextsharp对pdf文件进行分割的几种方法
- C#调用iTextSharp组件生成PDF文件, 在VS2005下已经调试通过!
- c# 调用 pdf2swf.exe
- c#中读水晶报表转为pdf的方法
- C#+Arcengine调用GP进行裁剪
- 如何在c#中进行调用vc制作的dll中的类
- 【译】在Asp.Net中操作PDF - iTextSharp - 利用列进行排版
- C#使用itextsharp生成PDF文件
- c# 基础之水晶报表设计(手动连接到数据库)
- C#对指定的文件调用RAR进行压缩保存
- C#利用iTextSharp 将jpg转换为pdf的方法
- C#调用Process进行mysql数据库备份和还原
- 使用C#和Excel进行报表开发(三)-生成统计图(Chart)1
- 使用C#和Excel进行报表开发(一)--开始
- 使用C#和Excel进行报表开发(八)-用程序绑定数据源
- C#中的异步调用及异步设计模式(一)
- 使用C#和Excel进行报表开发(3)