PDFGenerateTools.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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="imagePath">图片路径</param>
  130. public void InsertImage(byte[] imageBytes)
  131. {
  132. if (IsSave)
  133. {
  134. return;
  135. }
  136. Font FontValue = new Font(baseFont, 12);
  137. // 插入图片
  138. try
  139. {
  140. Image img = Image.GetInstance(imageBytes);
  141. img.ScaleToFit(500, 300);
  142. img.Alignment = Element.ALIGN_CENTER;
  143. document.Add(img);
  144. }
  145. catch (Exception ex)
  146. {
  147. document.Add(new Paragraph("图片加载失败: " + ex.Message));
  148. }
  149. }
  150. /// <summary>
  151. /// 插入图片
  152. /// </summary>
  153. /// <param name="imagePath">图片路径</param>
  154. /// <param name="width">图片宽度</param>
  155. /// <param name="height">图片高度</param>
  156. public void InsertImage(string imagePath,float width,float height)
  157. {
  158. if (IsSave)
  159. {
  160. return;
  161. }
  162. Font FontValue = new Font(baseFont, 12);
  163. // 插入图片
  164. try
  165. {
  166. Image img = Image.GetInstance(imagePath);
  167. img.ScaleToFit(width, height);
  168. img.Alignment = Element.ALIGN_CENTER;
  169. document.Add(img);
  170. }
  171. catch (Exception ex)
  172. {
  173. document.Add(new Paragraph("图片加载失败: " + ex.Message));
  174. }
  175. }
  176. /// <summary>
  177. /// 插入图片
  178. /// </summary>
  179. /// <param name="imagePath">图片路径</param>
  180. /// <param name="width">图片宽度</param>
  181. /// <param name="height">图片高度</param>
  182. public void InsertImage(byte[] imageBytes, float width, float height)
  183. {
  184. if (IsSave)
  185. {
  186. return;
  187. }
  188. Font FontValue = new Font(baseFont, 12);
  189. // 插入图片
  190. try
  191. {
  192. Image img = Image.GetInstance(imageBytes);
  193. img.ScaleToFit(width, height);
  194. img.Alignment = Element.ALIGN_CENTER;
  195. document.Add(img);
  196. }
  197. catch (Exception ex)
  198. {
  199. document.Add(new Paragraph("图片加载失败: " + ex.Message));
  200. }
  201. }
  202. /// <summary>
  203. /// 插入表格
  204. /// </summary>
  205. /// <param name="dataList">数据列表</param>
  206. /// <param name="tableName">表名</param>
  207. public void InsertTable<T>(List<T> dataList, string tableName = "表头") where T : class
  208. {
  209. if (IsSave)
  210. {
  211. return;
  212. }
  213. if (dataList == null || dataList.Count == 0)
  214. {
  215. return; // 如果数据为空则直接返回
  216. }
  217. Font FontValue = new Font(baseFont, 12);
  218. // 使用反射获取类的属性作为列
  219. var properties = typeof(T).GetProperties();
  220. PdfPTable table = new PdfPTable(properties.Length);
  221. table.WidthPercentage = 100; // 表格宽度为页面宽度的100%
  222. // 添加表名作为表头(跨所有列)
  223. PdfPCell cell = new PdfPCell(new Phrase(tableName, FontValue));
  224. cell.Colspan = properties.Length;
  225. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  226. table.AddCell(cell);
  227. // 添加属性名作为列标题
  228. foreach (var prop in properties)
  229. {
  230. PdfPCell headerCell = new PdfPCell(new Phrase(prop.Name, FontValue));
  231. headerCell.HorizontalAlignment = Element.ALIGN_CENTER;
  232. table.AddCell(headerCell);
  233. }
  234. // 添加数据行
  235. foreach (var data in dataList)
  236. {
  237. foreach (var prop in properties)
  238. {
  239. var value = prop.GetValue(data)?.ToString() ?? "";
  240. table.AddCell(new Phrase(value, FontValue));
  241. }
  242. }
  243. // 将表格添加到文档
  244. document.Add(table);
  245. }
  246. /// <summary>
  247. /// 插入表格
  248. /// </summary>
  249. /// <param name="dataList">数据列表</param>
  250. /// <param name="tableName">表名</param>
  251. public void InsertTable<T>(List<T> dataList,List<string> Colspan,string tableName = "表头") where T : class
  252. {
  253. if (IsSave)
  254. {
  255. return;
  256. }
  257. if (dataList == null || dataList.Count == 0)
  258. {
  259. return; // 如果数据为空则直接返回
  260. }
  261. Font FontValue = new Font(baseFont, 12);
  262. // 使用反射获取类的属性作为列
  263. var properties = typeof(T).GetProperties();
  264. if(properties.Length != Colspan.Count) { return; }
  265. PdfPTable table = new PdfPTable(Colspan.Count);
  266. table.WidthPercentage = 100; // 表格宽度为页面宽度的100%
  267. // 添加表名作为表头(跨所有列)
  268. PdfPCell cell = new PdfPCell(new Phrase(tableName, FontValue));
  269. cell.Colspan = Colspan.Count;
  270. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  271. table.AddCell(cell);
  272. // 添加属性名作为列标题
  273. foreach (var prop in Colspan)
  274. {
  275. PdfPCell headerCell = new PdfPCell(new Phrase(prop, FontValue));
  276. headerCell.HorizontalAlignment = Element.ALIGN_CENTER;
  277. table.AddCell(headerCell);
  278. }
  279. // 添加数据行
  280. foreach (var data in dataList)
  281. {
  282. foreach (var prop in properties)
  283. {
  284. var value = prop.GetValue(data)?.ToString() ?? "";
  285. table.AddCell(new Phrase(value, FontValue));
  286. }
  287. }
  288. // 将表格添加到文档
  289. document.Add(table);
  290. }
  291. /// <summary>
  292. /// 插入表格
  293. /// </summary>
  294. /// <param name="dataList">数据列表</param>
  295. /// <param name="tableName">表名</param>
  296. public void InsertTable<T>(List<T> dataList, List<string> Colspan,float[] ColWidth, string tableName = "表头") where T : class
  297. {
  298. if (IsSave)
  299. {
  300. return;
  301. }
  302. if (dataList == null || dataList.Count == 0)
  303. {
  304. return; // 如果数据为空则直接返回
  305. }
  306. Font FontValue = new Font(baseFont, 12);
  307. // 使用反射获取类的属性作为列
  308. var properties = typeof(T).GetProperties();
  309. if (properties.Length != Colspan.Count) { return; }
  310. PdfPTable table = new PdfPTable(Colspan.Count);
  311. table.SetWidths(ColWidth);
  312. table.WidthPercentage = 100; // 表格宽度为页面宽度的100%
  313. // 添加表名作为表头(跨所有列)
  314. PdfPCell cell = new PdfPCell(new Phrase(tableName, FontValue));
  315. cell.Colspan = Colspan.Count;
  316. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  317. table.AddCell(cell);
  318. // 添加属性名作为列标题
  319. foreach (var prop in Colspan)
  320. {
  321. PdfPCell headerCell = new PdfPCell(new Phrase(prop, FontValue));
  322. headerCell.HorizontalAlignment = Element.ALIGN_CENTER;
  323. table.AddCell(headerCell);
  324. }
  325. // 添加数据行
  326. foreach (var data in dataList)
  327. {
  328. foreach (var prop in properties)
  329. {
  330. var value = prop.GetValue(data)?.ToString() ?? "";
  331. table.AddCell(new Phrase(value, FontValue));
  332. }
  333. }
  334. // 将表格添加到文档
  335. document.Add(table);
  336. }
  337. /// <summary>
  338. /// 插入表格
  339. /// </summary>
  340. /// <param name="dataList">数据列表</param>
  341. /// <param name="tableName">表名</param>
  342. public void InsertTable<T>(T data, string tableName = "表头") where T : class
  343. {
  344. if (IsSave)
  345. {
  346. return;
  347. }
  348. if (data == null)
  349. {
  350. return; // 如果数据为空则直接返回
  351. }
  352. Font FontValue = new Font(baseFont, 12);
  353. // 使用反射获取类的属性作为列
  354. var properties = typeof(T).GetProperties();
  355. PdfPTable table = new PdfPTable(2);
  356. table.WidthPercentage = 100; // 表格宽度为页面宽度的100%
  357. // 添加表名作为表头(跨所有列)
  358. PdfPCell cell = new PdfPCell(new Phrase(tableName, FontValue));
  359. cell.Colspan = 2;
  360. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  361. table.AddCell(cell);
  362. PdfPCell headerCell1 = new PdfPCell(new Phrase("参数名", FontValue));
  363. headerCell1.HorizontalAlignment = Element.ALIGN_CENTER;
  364. table.AddCell(headerCell1);
  365. PdfPCell headerCell2 = new PdfPCell(new Phrase("参数值", FontValue));
  366. headerCell2.HorizontalAlignment = Element.ALIGN_CENTER;
  367. table.AddCell(headerCell2);
  368. //// 添加属性名作为列标题
  369. foreach (var prop in properties)
  370. {
  371. PdfPCell headerCell = new PdfPCell(new Phrase(prop.Name, FontValue));
  372. headerCell.HorizontalAlignment = Element.ALIGN_CENTER;
  373. table.AddCell(headerCell);
  374. var value = prop.GetValue(data)?.ToString() ?? "";
  375. PdfPCell RowValueCell = new PdfPCell(new Phrase(value, FontValue));
  376. RowValueCell.HorizontalAlignment = Element.ALIGN_CENTER;
  377. table.AddCell(RowValueCell);
  378. }
  379. // 将表格添加到文档
  380. document.Add(table);
  381. }
  382. /// <summary>
  383. /// 插入表格
  384. /// </summary>
  385. /// <param name="dataList">数据列表</param>
  386. /// <param name="tableName">表名</param>
  387. public bool InsertTable<T>(T data, List<string> Rowspan, string tableName = "表头") where T : class
  388. {
  389. bool Result = false;
  390. if (IsSave)
  391. {
  392. return false;
  393. }
  394. if (data == null)
  395. {
  396. return false; // 如果数据为空则直接返回
  397. }
  398. Font FontValue = new Font(baseFont, 12);
  399. // 使用反射获取类的属性作为列
  400. var properties = typeof(T).GetProperties();
  401. PdfPTable table = new PdfPTable(2);
  402. table.WidthPercentage = 100; // 表格宽度为页面宽度的100%
  403. // 添加表名作为表头(跨所有列)
  404. PdfPCell cell = new PdfPCell(new Phrase(tableName, FontValue));
  405. cell.Colspan = 2;
  406. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  407. table.AddCell(cell);
  408. PdfPCell headerCell1 = new PdfPCell(new Phrase("参数名", FontValue));
  409. headerCell1.HorizontalAlignment = Element.ALIGN_CENTER;
  410. table.AddCell(headerCell1);
  411. PdfPCell headerCell2 = new PdfPCell(new Phrase("参数值", FontValue));
  412. headerCell2.HorizontalAlignment = Element.ALIGN_CENTER;
  413. table.AddCell(headerCell2);
  414. if (Rowspan.Count != properties.Length) { return false; }
  415. for (int i=0;i<properties.Length;i++)
  416. {
  417. PdfPCell RowNameCell = new PdfPCell(new Phrase(Rowspan[i], FontValue));
  418. RowNameCell.HorizontalAlignment = Element.ALIGN_CENTER;
  419. table.AddCell(RowNameCell);
  420. var value = properties[i].GetValue(data)?.ToString() ?? "";
  421. PdfPCell RowValueCell = new PdfPCell(new Phrase(value, FontValue));
  422. RowValueCell.HorizontalAlignment = Element.ALIGN_CENTER;
  423. table.AddCell(RowValueCell);
  424. }
  425. // 将表格添加到文档
  426. document.Add(table);
  427. Result = true;
  428. return Result;
  429. }
  430. /// <summary>
  431. /// 插入空白行
  432. /// </summary>
  433. public void InsertNewLine()
  434. {
  435. if (IsSave)
  436. {
  437. return;
  438. }
  439. document.Add(Chunk.NEWLINE);
  440. }
  441. /// <summary>
  442. /// 保存PDF文件
  443. /// </summary>
  444. public void SavePDF()
  445. {
  446. document.Close();
  447. IsSave = true;
  448. }
  449. }
  450. }