ReportGenerater.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using iTextSharp.text;
  7. using iTextSharp.text.pdf;
  8. using System.IO;
  9. using LitJson;
  10. namespace WpfTest1.Toolkits
  11. {
  12. class ReportGenerater
  13. {
  14. public static void generateReport(int type, SQLite.Patient onePatient, SQLite.doctor oneDoctor, SQLite.Record oneRecord, string organization_name)
  15. {
  16. string fileName = Constants.reportPath + "\\" + oneRecord.r_id.ToString() + ".pdf";
  17. Document document = new Document(PageSize.A4);
  18. //中文字体
  19. string chinese = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "STSONG.TTF");
  20. //System.Console.WriteLine(chinese);
  21. BaseFont baseFont = BaseFont.CreateFont(chinese, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  22. //文字大小12,文字样式
  23. Font cn = new Font(baseFont, 12, Font.NORMAL);
  24. PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
  25. document.Open();
  26. //首先是标题
  27. var title = new Paragraph(String.Format("\n {0}检测报告", organization_name), new Font(baseFont, 16, Font.BOLD, BaseColor.BLACK));
  28. //居中
  29. title.Alignment = Element.ALIGN_CENTER;
  30. document.Add(title);
  31. //加一个空段预防遮挡
  32. document.Add(new iTextSharp.text.Paragraph(" "));
  33. var border = new Paragraph("------------------------------------------------------------------------------------------------------", new Font(baseFont, 16, Font.BOLD, BaseColor.BLACK));
  34. border.Alignment = Element.ALIGN_CENTER;
  35. document.Add(border);
  36. //然后是病例信息表格
  37. //添加表格
  38. PdfPTable table = new PdfPTable(6);
  39. DateTime now = DateTime.Today;
  40. int age = now.Year - onePatient.p_birthdate.Year;
  41. if (onePatient.p_birthdate > now.AddYears(-age))
  42. age--;
  43. string[] dataToBeFilled = new string[]
  44. {
  45. "病案号:",
  46. onePatient.p_record_id,
  47. "姓名:",
  48. onePatient.p_name,
  49. "性别:",
  50. onePatient.p_gender,
  51. "年龄:",
  52. age.ToString(),
  53. "身高:",
  54. onePatient.p_height.ToString() + "cm",
  55. "体重:",
  56. onePatient.p_weight.ToString() + "kg",
  57. "操作医师:",
  58. oneDoctor.name,
  59. "操作时间:",
  60. oneRecord.r_time.ToString("g"),
  61. " ",
  62. " "
  63. };
  64. foreach(string oneSentance in dataToBeFilled)
  65. {
  66. Paragraph p = new Paragraph(oneSentance, new Font(baseFont));
  67. PdfPCell cell = new PdfPCell(p);
  68. cell.Border = Rectangle.NO_BORDER;
  69. table.AddCell(cell);
  70. }
  71. table.HorizontalAlignment = Element.ALIGN_CENTER;
  72. //table.
  73. table.TotalWidth = 500;
  74. table.LockedWidth = true;
  75. float[] widths = new float[] { 100, 100, 100, 100, 50, 50 };//三列列宽不同若果是浮点数需要加f
  76. table.SetWidths(widths);
  77. document.Add(table);
  78. document.Add(border);
  79. //最后是文档
  80. var jsonUserSelections = oneRecord.r_selection;
  81. List<SQLite.UserSelection> uss = JsonMapper.ToObject<List<SQLite.UserSelection>>(jsonUserSelections);
  82. foreach(SQLite.UserSelection us in uss)
  83. {
  84. SQLite.Question qTemp = SQLite.SQLiteModel.getQuestionById(us.q_id);
  85. SQLite.Answer aTemp = SQLite.SQLiteModel.getAnswerById(us.a_id);
  86. //System.Console.WriteLine(String.Format("a_id:{0},next_qid:{1}", aTemp.a_id, aTemp.next_q_id));
  87. if(type == 2 && aTemp.next_q_id != 0)
  88. {
  89. //evaluation
  90. continue;
  91. }
  92. else
  93. {
  94. var questionTitle = new Paragraph(String.Format("{0}:{1}", qTemp.q_title, aTemp.a_content), new Font(baseFont, 13, Font.NORMAL, BaseColor.BLACK));
  95. document.Add(questionTitle);
  96. if (aTemp.a_description_img != "")
  97. {
  98. Image image = Image.GetInstance(Constants.imgPath + "\\" + aTemp.a_description_img);
  99. image.ScaleAbsoluteWidth(500);
  100. document.Add(image);
  101. }
  102. if (aTemp.a_description_text != "")
  103. {
  104. var description = new Paragraph(String.Format("问题说明:\n {0}", aTemp.a_description_text), new Font(baseFont, 13, Font.NORMAL, BaseColor.BLACK));
  105. document.Add(description);
  106. }
  107. if (aTemp.a_suggestion_img != "")
  108. {
  109. Image image = Image.GetInstance(Constants.imgPath + "\\" + aTemp.a_suggestion_img);
  110. image.ScaleAbsoluteWidth(500);
  111. document.Add(image);
  112. }
  113. if (aTemp.a_suggestion_text != "")
  114. {
  115. var suggestion = new Paragraph(String.Format("建议:\n {0}", aTemp.a_suggestion_text), new Font(baseFont, 13, Font.NORMAL, BaseColor.BLACK));
  116. document.Add(suggestion);
  117. }
  118. }
  119. }
  120. document.Close();
  121. //打开文件
  122. System.Diagnostics.Process.Start(fileName);
  123. }
  124. }
  125. }