PDFGenerateTools.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using iTextSharp.text;
  2. using iTextSharp.text.pdf;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace CCDCount.DLL.Tools
  10. {
  11. public class PDFGenerateTools
  12. {
  13. /// <summary>
  14. /// 是否保存标志量
  15. /// </summary>
  16. public bool IsSave = false;
  17. /// <summary>
  18. /// 文档对象
  19. /// </summary>
  20. private Document document = new Document(PageSize.A4);
  21. /// <summary>
  22. /// 字体位置
  23. /// </summary>
  24. string fontPath = @"C:\Windows\Fonts\MSYH.ttc,0"; // 微软雅黑
  25. /// <summary>
  26. /// 字体实例
  27. /// </summary>
  28. BaseFont baseFont = null;
  29. /// <summary>
  30. /// 构造函数
  31. /// </summary>
  32. /// <param name="outputPath">PDF生成位置</param>
  33. public PDFGenerateTools(string outputPath)
  34. {
  35. PdfWriter.GetInstance(document, new FileStream(outputPath, FileMode.Create));
  36. document.Open();
  37. baseFont = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  38. }
  39. /// <summary>
  40. /// 插入文本
  41. /// </summary>
  42. /// <param name="StrText">插入的文本</param>
  43. public void InsertText(string StrText)
  44. {
  45. if (IsSave)
  46. {
  47. return;
  48. }
  49. Font FontValue = new Font(baseFont, 12);
  50. Paragraph TextValue = new Paragraph(StrText, FontValue);
  51. TextValue.Alignment = Element.ALIGN_CENTER;
  52. document.Add(TextValue);
  53. }
  54. /// <summary>
  55. /// 插入文本
  56. /// </summary>
  57. /// <param name="StrText">插入的文本</param>
  58. /// <param name="FontSize">字体的字号</param>
  59. public void InsertText(string StrText, int FontSize)
  60. {
  61. if (IsSave)
  62. {
  63. return;
  64. }
  65. Font FontValue = new Font(baseFont, FontSize);
  66. Paragraph TextValue = new Paragraph(StrText, FontValue);
  67. TextValue.Alignment = Element.ALIGN_CENTER;
  68. document.Add(TextValue);
  69. }
  70. /// <summary>
  71. /// 插入文本
  72. /// </summary>
  73. /// <param name="StrText">插入的文本</param>
  74. /// <param name="FontSize">字体的字号</param>
  75. /// <param name="Alignment">对齐方式</param>
  76. public void InsertText(string StrText, int FontSize, int Alignment)
  77. {
  78. if (IsSave)
  79. {
  80. return;
  81. }
  82. Font FontValue = new Font(baseFont, FontSize);
  83. Paragraph TextValue = new Paragraph(StrText, FontValue);
  84. if (Alignment == 1)
  85. {
  86. TextValue.Alignment = Element.ALIGN_LEFT;
  87. }
  88. else if (Alignment == 2)
  89. {
  90. TextValue.Alignment = Element.ALIGN_RIGHT;
  91. }
  92. else if (Alignment == 3)
  93. {
  94. TextValue.Alignment = Element.ALIGN_CENTER;
  95. }
  96. else
  97. {
  98. Console.WriteLine("未定义对齐方式");
  99. }
  100. document.Add(TextValue);
  101. }
  102. /// <summary>
  103. /// 插入图片
  104. /// </summary>
  105. /// <param name="imagePath">图片路径</param>
  106. public void InsertImage(string imagePath)
  107. {
  108. if (IsSave)
  109. {
  110. return;
  111. }
  112. Font FontValue = new Font(baseFont, 12);
  113. // 插入图片
  114. try
  115. {
  116. Image img = Image.GetInstance(imagePath);
  117. img.ScaleToFit(500, 300);
  118. img.Alignment = Element.ALIGN_CENTER;
  119. document.Add(img);
  120. }
  121. catch (Exception ex)
  122. {
  123. document.Add(new Paragraph("图片加载失败: " + ex.Message));
  124. }
  125. }
  126. /// <summary>
  127. /// 插入表格
  128. /// </summary>
  129. /// <param name="dataList">数据列表</param>
  130. /// <param name="tableName">表名</param>
  131. public void InsertTable<T>(List<T> dataList, string tableName = "表头") where T : class
  132. {
  133. if (IsSave)
  134. {
  135. return;
  136. }
  137. if (dataList == null || dataList.Count == 0)
  138. {
  139. return; // 如果数据为空则直接返回
  140. }
  141. Font FontValue = new Font(baseFont, 12);
  142. // 使用反射获取类的属性作为列
  143. var properties = typeof(T).GetProperties();
  144. PdfPTable table = new PdfPTable(properties.Length);
  145. table.WidthPercentage = 100; // 表格宽度为页面宽度的100%
  146. // 添加表名作为表头(跨所有列)
  147. PdfPCell cell = new PdfPCell(new Phrase(tableName, FontValue));
  148. cell.Colspan = properties.Length;
  149. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  150. table.AddCell(cell);
  151. // 添加属性名作为列标题
  152. foreach (var prop in properties)
  153. {
  154. PdfPCell headerCell = new PdfPCell(new Phrase(prop.Name, FontValue));
  155. headerCell.HorizontalAlignment = Element.ALIGN_CENTER;
  156. table.AddCell(headerCell);
  157. }
  158. // 添加数据行
  159. foreach (var data in dataList)
  160. {
  161. foreach (var prop in properties)
  162. {
  163. var value = prop.GetValue(data)?.ToString() ?? "";
  164. table.AddCell(new Phrase(value, FontValue));
  165. }
  166. }
  167. // 将表格添加到文档
  168. document.Add(table);
  169. }
  170. /// <summary>
  171. /// 插入空白行
  172. /// </summary>
  173. public void InsertNewLine()
  174. {
  175. if (IsSave)
  176. {
  177. return;
  178. }
  179. document.Add(Chunk.NEWLINE);
  180. }
  181. /// <summary>
  182. /// 保存PDF文件
  183. /// </summary>
  184. public void SavePDF()
  185. {
  186. document.Close();
  187. IsSave = true;
  188. }
  189. }
  190. }