PDFGenerate.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. using iTextSharp.text;
  2. using iTextSharp.text.pdf;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. namespace MvvmScaffoldFrame48.DLL.FileTools
  7. {
  8. public class PDFGenerate
  9. {
  10. #region 成员变量
  11. /// <summary>
  12. /// 是否保存标志量
  13. /// </summary>
  14. public bool IsSave = false;
  15. /// <summary>
  16. /// 字体位置
  17. /// </summary>
  18. string fontPath = @"C:\Windows\Fonts\MSYH.ttc,0"; // 微软雅黑
  19. #endregion
  20. #region 实例
  21. /// <summary>
  22. /// 文档对象
  23. /// </summary>
  24. private Document document = new Document(PageSize.A4);
  25. /// <summary>
  26. /// 字体实例
  27. /// </summary>
  28. BaseFont baseFont = null;
  29. #endregion
  30. #region 构造函数
  31. /// <summary>
  32. /// 构造函数
  33. /// </summary>
  34. /// <param name="outputPath">PDF生成位置</param>
  35. public PDFGenerate(string outputPath)
  36. {
  37. PdfWriter.GetInstance(document, new FileStream(outputPath, FileMode.Create));
  38. document.Open();
  39. baseFont = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  40. }
  41. #endregion
  42. #region 公共方法
  43. /// <summary>
  44. /// 插入文本
  45. /// </summary>
  46. /// <param name="StrText">插入的文本</param>
  47. public void InsertText(string StrText)
  48. {
  49. if (IsSave)
  50. {
  51. return;
  52. }
  53. Font FontValue = new Font(baseFont, 12);
  54. Paragraph TextValue = new Paragraph(StrText, FontValue);
  55. TextValue.Alignment = Element.ALIGN_CENTER;
  56. document.Add(TextValue);
  57. }
  58. /// <summary>
  59. /// 插入文本
  60. /// </summary>
  61. /// <param name="StrText">插入的文本</param>
  62. /// <param name="FontSize">字体的字号</param>
  63. public void InsertText(string StrText, int FontSize)
  64. {
  65. if (IsSave)
  66. {
  67. return;
  68. }
  69. Font FontValue = new Font(baseFont, FontSize);
  70. Paragraph TextValue = new Paragraph(StrText, FontValue);
  71. TextValue.Alignment = Element.ALIGN_CENTER;
  72. document.Add(TextValue);
  73. }
  74. /// <summary>
  75. /// 插入文本
  76. /// </summary>
  77. /// <param name="StrText">插入的文本</param>
  78. /// <param name="FontSize">字体的字号</param>
  79. /// <param name="Alignment">对齐方式</param>
  80. public void InsertText(string StrText, int FontSize, int Alignment)
  81. {
  82. if (IsSave)
  83. {
  84. return;
  85. }
  86. Font FontValue = new Font(baseFont, FontSize);
  87. Paragraph TextValue = new Paragraph(StrText, FontValue);
  88. if (Alignment == 1)
  89. {
  90. TextValue.Alignment = Element.ALIGN_LEFT;
  91. }
  92. else if (Alignment == 2)
  93. {
  94. TextValue.Alignment = Element.ALIGN_RIGHT;
  95. }
  96. else if (Alignment == 3)
  97. {
  98. TextValue.Alignment = Element.ALIGN_CENTER;
  99. }
  100. else
  101. {
  102. Console.WriteLine("未定义对齐方式");
  103. }
  104. document.Add(TextValue);
  105. }
  106. /// <summary>
  107. /// 插入图片
  108. /// </summary>
  109. /// <param name="imagePath">图片路径</param>
  110. public void InsertImage(string imagePath)
  111. {
  112. if (IsSave)
  113. {
  114. return;
  115. }
  116. Font FontValue = new Font(baseFont, 12);
  117. // 插入图片
  118. try
  119. {
  120. Image img = Image.GetInstance(imagePath);
  121. img.ScaleToFit(500, 300);
  122. img.Alignment = Element.ALIGN_CENTER;
  123. document.Add(img);
  124. }
  125. catch (Exception ex)
  126. {
  127. document.Add(new Paragraph("图片加载失败: " + ex.Message));
  128. }
  129. }
  130. /// <summary>
  131. /// 插入图片
  132. /// </summary>
  133. /// <param name="imagePath">图片路径</param>
  134. public void InsertImage(byte[] imageBytes)
  135. {
  136. if (IsSave)
  137. {
  138. return;
  139. }
  140. Font FontValue = new Font(baseFont, 12);
  141. // 插入图片
  142. try
  143. {
  144. Image img = Image.GetInstance(imageBytes);
  145. img.ScaleToFit(500, 300);
  146. img.Alignment = Element.ALIGN_CENTER;
  147. document.Add(img);
  148. }
  149. catch (Exception ex)
  150. {
  151. document.Add(new Paragraph("图片加载失败: " + ex.Message));
  152. }
  153. }
  154. /// <summary>
  155. /// 插入图片
  156. /// </summary>
  157. /// <param name="imagePath">图片路径</param>
  158. /// <param name="width">图片宽度</param>
  159. /// <param name="height">图片高度</param>
  160. public void InsertImage(string imagePath, float width, float height)
  161. {
  162. if (IsSave)
  163. {
  164. return;
  165. }
  166. Font FontValue = new Font(baseFont, 12);
  167. // 插入图片
  168. try
  169. {
  170. Image img = Image.GetInstance(imagePath);
  171. img.ScaleToFit(width, height);
  172. img.Alignment = Element.ALIGN_CENTER;
  173. document.Add(img);
  174. }
  175. catch (Exception ex)
  176. {
  177. document.Add(new Paragraph("图片加载失败: " + ex.Message));
  178. }
  179. }
  180. /// <summary>
  181. /// 插入图片
  182. /// </summary>
  183. /// <param name="imagePath">图片路径</param>
  184. /// <param name="width">图片宽度</param>
  185. /// <param name="height">图片高度</param>
  186. public void InsertImage(byte[] imageBytes, float width, float height)
  187. {
  188. if (IsSave)
  189. {
  190. return;
  191. }
  192. Font FontValue = new Font(baseFont, 12);
  193. // 插入图片
  194. try
  195. {
  196. Image img = Image.GetInstance(imageBytes);
  197. img.ScaleToFit(width, height);
  198. img.Alignment = Element.ALIGN_CENTER;
  199. document.Add(img);
  200. }
  201. catch (Exception ex)
  202. {
  203. document.Add(new Paragraph("图片加载失败: " + ex.Message));
  204. }
  205. }
  206. /// <summary>
  207. /// 插入表格
  208. /// </summary>
  209. /// <param name="dataList">数据列表</param>
  210. /// <param name="tableName">表名</param>
  211. public void InsertTable<T>(List<T> dataList, string tableName = "表头") where T : class
  212. {
  213. if (IsSave)
  214. {
  215. return;
  216. }
  217. if (dataList == null || dataList.Count == 0)
  218. {
  219. return; // 如果数据为空则直接返回
  220. }
  221. Font FontValue = new Font(baseFont, 12);
  222. // 使用反射获取类的属性作为列
  223. var properties = typeof(T).GetProperties();
  224. PdfPTable table = new PdfPTable(properties.Length);
  225. table.WidthPercentage = 100; // 表格宽度为页面宽度的100%
  226. // 添加表名作为表头(跨所有列)
  227. PdfPCell cell = new PdfPCell(new Phrase(tableName, FontValue));
  228. cell.Colspan = properties.Length;
  229. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  230. table.AddCell(cell);
  231. // 添加属性名作为列标题
  232. foreach (var prop in properties)
  233. {
  234. PdfPCell headerCell = new PdfPCell(new Phrase(prop.Name, FontValue));
  235. headerCell.HorizontalAlignment = Element.ALIGN_CENTER;
  236. table.AddCell(headerCell);
  237. }
  238. // 添加数据行
  239. foreach (var data in dataList)
  240. {
  241. foreach (var prop in properties)
  242. {
  243. var value = prop.GetValue(data)?.ToString() ?? "";
  244. table.AddCell(new Phrase(value, FontValue));
  245. }
  246. }
  247. // 将表格添加到文档
  248. document.Add(table);
  249. }
  250. /// <summary>
  251. /// 插入表格
  252. /// </summary>
  253. /// <param name="dataList">数据列表</param>
  254. /// <param name="tableName">表名</param>
  255. public void InsertTable<T>(List<T> dataList, List<string> Colspan, string tableName = "表头") where T : class
  256. {
  257. if (IsSave)
  258. {
  259. return;
  260. }
  261. if (dataList == null || dataList.Count == 0)
  262. {
  263. return; // 如果数据为空则直接返回
  264. }
  265. Font FontValue = new Font(baseFont, 12);
  266. // 使用反射获取类的属性作为列
  267. var properties = typeof(T).GetProperties();
  268. if (properties.Length != Colspan.Count) { return; }
  269. PdfPTable table = new PdfPTable(Colspan.Count);
  270. table.WidthPercentage = 100; // 表格宽度为页面宽度的100%
  271. // 添加表名作为表头(跨所有列)
  272. PdfPCell cell = new PdfPCell(new Phrase(tableName, FontValue));
  273. cell.Colspan = Colspan.Count;
  274. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  275. table.AddCell(cell);
  276. // 添加属性名作为列标题
  277. foreach (var prop in Colspan)
  278. {
  279. PdfPCell headerCell = new PdfPCell(new Phrase(prop, FontValue));
  280. headerCell.HorizontalAlignment = Element.ALIGN_CENTER;
  281. table.AddCell(headerCell);
  282. }
  283. // 添加数据行
  284. foreach (var data in dataList)
  285. {
  286. foreach (var prop in properties)
  287. {
  288. var value = prop.GetValue(data)?.ToString() ?? "";
  289. table.AddCell(new Phrase(value, FontValue));
  290. }
  291. }
  292. // 将表格添加到文档
  293. document.Add(table);
  294. }
  295. /// <summary>
  296. /// 插入表格
  297. /// </summary>
  298. /// <param name="dataList">数据列表</param>
  299. /// <param name="tableName">表名</param>
  300. public void InsertTable<T>(T data, string tableName = "表头") where T : class
  301. {
  302. if (IsSave)
  303. {
  304. return;
  305. }
  306. if (data == null)
  307. {
  308. return; // 如果数据为空则直接返回
  309. }
  310. Font FontValue = new Font(baseFont, 12);
  311. // 使用反射获取类的属性作为列
  312. var properties = typeof(T).GetProperties();
  313. PdfPTable table = new PdfPTable(2);
  314. table.WidthPercentage = 100; // 表格宽度为页面宽度的100%
  315. // 添加表名作为表头(跨所有列)
  316. PdfPCell cell = new PdfPCell(new Phrase(tableName, FontValue));
  317. cell.Colspan = 2;
  318. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  319. table.AddCell(cell);
  320. PdfPCell headerCell1 = new PdfPCell(new Phrase("参数名", FontValue));
  321. headerCell1.HorizontalAlignment = Element.ALIGN_CENTER;
  322. table.AddCell(headerCell1);
  323. PdfPCell headerCell2 = new PdfPCell(new Phrase("参数值", FontValue));
  324. headerCell2.HorizontalAlignment = Element.ALIGN_CENTER;
  325. table.AddCell(headerCell2);
  326. //// 添加属性名作为列标题
  327. foreach (var prop in properties)
  328. {
  329. PdfPCell headerCell = new PdfPCell(new Phrase(prop.Name, FontValue));
  330. headerCell.HorizontalAlignment = Element.ALIGN_CENTER;
  331. table.AddCell(headerCell);
  332. var value = prop.GetValue(data)?.ToString() ?? "";
  333. PdfPCell RowValueCell = new PdfPCell(new Phrase(value, FontValue));
  334. RowValueCell.HorizontalAlignment = Element.ALIGN_CENTER;
  335. table.AddCell(RowValueCell);
  336. }
  337. // 将表格添加到文档
  338. document.Add(table);
  339. }
  340. /// <summary>
  341. /// 插入表格
  342. /// </summary>
  343. /// <param name="dataList">数据列表</param>
  344. /// <param name="tableName">表名</param>
  345. public bool InsertTable<T>(T data, List<string> Rowspan, string tableName = "表头") where T : class
  346. {
  347. bool Result = false;
  348. if (IsSave)
  349. {
  350. return false;
  351. }
  352. if (data == null)
  353. {
  354. return false; // 如果数据为空则直接返回
  355. }
  356. Font FontValue = new Font(baseFont, 12);
  357. // 使用反射获取类的属性作为列
  358. var properties = typeof(T).GetProperties();
  359. PdfPTable table = new PdfPTable(2);
  360. table.WidthPercentage = 100; // 表格宽度为页面宽度的100%
  361. // 添加表名作为表头(跨所有列)
  362. PdfPCell cell = new PdfPCell(new Phrase(tableName, FontValue));
  363. cell.Colspan = 2;
  364. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  365. table.AddCell(cell);
  366. PdfPCell headerCell1 = new PdfPCell(new Phrase("参数名", FontValue));
  367. headerCell1.HorizontalAlignment = Element.ALIGN_CENTER;
  368. table.AddCell(headerCell1);
  369. PdfPCell headerCell2 = new PdfPCell(new Phrase("参数值", FontValue));
  370. headerCell2.HorizontalAlignment = Element.ALIGN_CENTER;
  371. table.AddCell(headerCell2);
  372. if (Rowspan.Count != properties.Length) { return false; }
  373. for (int i = 0; i < properties.Length; i++)
  374. {
  375. PdfPCell RowNameCell = new PdfPCell(new Phrase(Rowspan[i], FontValue));
  376. RowNameCell.HorizontalAlignment = Element.ALIGN_CENTER;
  377. table.AddCell(RowNameCell);
  378. var value = properties[i].GetValue(data)?.ToString() ?? "";
  379. PdfPCell RowValueCell = new PdfPCell(new Phrase(value, FontValue));
  380. RowValueCell.HorizontalAlignment = Element.ALIGN_CENTER;
  381. table.AddCell(RowValueCell);
  382. }
  383. // 将表格添加到文档
  384. document.Add(table);
  385. Result = true;
  386. return Result;
  387. }
  388. /// <summary>
  389. /// 插入空白行
  390. /// </summary>
  391. public void InsertNewLine()
  392. {
  393. if (IsSave)
  394. {
  395. return;
  396. }
  397. document.Add(Chunk.NEWLINE);
  398. }
  399. /// <summary>
  400. /// 保存PDF文件
  401. /// </summary>
  402. public void SavePDF()
  403. {
  404. document.Close();
  405. IsSave = true;
  406. }
  407. #endregion
  408. }
  409. }