PDFGenerateTools.cs 6.2 KB

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