using iTextSharp.text; using iTextSharp.text.pdf; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CCDCount.DLL.Tools { public class PDFGenerateTools { /// /// 是否保存标志量 /// public bool IsSave = false; /// /// 文档对象 /// private Document document = new Document(PageSize.A4); /// /// 字体位置 /// string fontPath = @"C:\Windows\Fonts\MSYH.ttc,0"; // 微软雅黑 /// /// 字体实例 /// BaseFont baseFont = null; /// /// 构造函数 /// /// PDF生成位置 public PDFGenerateTools(string outputPath) { PdfWriter.GetInstance(document, new FileStream(outputPath, FileMode.Create)); document.Open(); baseFont = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); } /// /// 插入文本 /// /// 插入的文本 public void InsertText(string StrText) { if (IsSave) { return; } Font FontValue = new Font(baseFont, 12); Paragraph TextValue = new Paragraph(StrText, FontValue); TextValue.Alignment = Element.ALIGN_CENTER; document.Add(TextValue); } /// /// 插入文本 /// /// 插入的文本 /// 字体的字号 public void InsertText(string StrText, int FontSize) { if (IsSave) { return; } Font FontValue = new Font(baseFont, FontSize); Paragraph TextValue = new Paragraph(StrText, FontValue); TextValue.Alignment = Element.ALIGN_CENTER; document.Add(TextValue); } /// /// 插入文本 /// /// 插入的文本 /// 字体的字号 /// 对齐方式 public void InsertText(string StrText, int FontSize, int Alignment) { if (IsSave) { return; } Font FontValue = new Font(baseFont, FontSize); Paragraph TextValue = new Paragraph(StrText, FontValue); if (Alignment == 1) { TextValue.Alignment = Element.ALIGN_LEFT; } else if (Alignment == 2) { TextValue.Alignment = Element.ALIGN_RIGHT; } else if (Alignment == 3) { TextValue.Alignment = Element.ALIGN_CENTER; } else { Console.WriteLine("未定义对齐方式"); } document.Add(TextValue); } /// /// 插入图片 /// /// 图片路径 public void InsertImage(string imagePath) { if (IsSave) { return; } Font FontValue = new Font(baseFont, 12); // 插入图片 try { Image img = Image.GetInstance(imagePath); img.ScaleToFit(500, 300); img.Alignment = Element.ALIGN_CENTER; document.Add(img); } catch (Exception ex) { document.Add(new Paragraph("图片加载失败: " + ex.Message)); } } /// /// 插入表格 /// /// 数据列表 /// 表名 public void InsertTable(List dataList, string tableName = "表头") where T : class { if (IsSave) { return; } if (dataList == null || dataList.Count == 0) { return; // 如果数据为空则直接返回 } Font FontValue = new Font(baseFont, 12); // 使用反射获取类的属性作为列 var properties = typeof(T).GetProperties(); PdfPTable table = new PdfPTable(properties.Length); table.WidthPercentage = 100; // 表格宽度为页面宽度的100% // 添加表名作为表头(跨所有列) PdfPCell cell = new PdfPCell(new Phrase(tableName, FontValue)); cell.Colspan = properties.Length; cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(cell); // 添加属性名作为列标题 foreach (var prop in properties) { PdfPCell headerCell = new PdfPCell(new Phrase(prop.Name, FontValue)); headerCell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(headerCell); } // 添加数据行 foreach (var data in dataList) { foreach (var prop in properties) { var value = prop.GetValue(data)?.ToString() ?? ""; table.AddCell(new Phrase(value, FontValue)); } } // 将表格添加到文档 document.Add(table); } /// /// 插入空白行 /// public void InsertNewLine() { if (IsSave) { return; } document.Add(Chunk.NEWLINE); } /// /// 保存PDF文件 /// public void SavePDF() { document.Close(); IsSave = true; } } }