123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- using System.IO;
- using LitJson;
- namespace WpfTest1.Toolkits
- {
- class ReportGenerater
- {
- public static void generateReport(int type, SQLite.Patient onePatient, SQLite.doctor oneDoctor, SQLite.Record oneRecord, string organization_name)
- {
- string fileName = Constants.reportPath + "\\" + oneRecord.r_id.ToString() + ".pdf";
- Document document = new Document(PageSize.A4);
- //中文字体
- string chinese = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "STSONG.TTF");
- //System.Console.WriteLine(chinese);
- BaseFont baseFont = BaseFont.CreateFont(chinese, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- //文字大小12,文字样式
- Font cn = new Font(baseFont, 12, Font.NORMAL);
- PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
- document.Open();
- //首先是标题
- var title = new Paragraph(String.Format("\n {0}检测报告", organization_name), new Font(baseFont, 16, Font.BOLD, BaseColor.BLACK));
- //居中
- title.Alignment = Element.ALIGN_CENTER;
- document.Add(title);
- //加一个空段预防遮挡
- document.Add(new iTextSharp.text.Paragraph(" "));
- var border = new Paragraph("------------------------------------------------------------------------------------------------------", new Font(baseFont, 16, Font.BOLD, BaseColor.BLACK));
- border.Alignment = Element.ALIGN_CENTER;
- document.Add(border);
- //然后是病例信息表格
- //添加表格
- PdfPTable table = new PdfPTable(6);
-
- DateTime now = DateTime.Today;
- int age = now.Year - onePatient.p_birthdate.Year;
- if (onePatient.p_birthdate > now.AddYears(-age))
- age--;
- string[] dataToBeFilled = new string[]
- {
- "病案号:",
- onePatient.p_record_id,
- "姓名:",
- onePatient.p_name,
- "性别:",
- onePatient.p_gender,
- "年龄:",
- age.ToString(),
- "身高:",
- onePatient.p_height.ToString() + "cm",
- "体重:",
- onePatient.p_weight.ToString() + "kg",
- "操作医师:",
- oneDoctor.name,
- "操作时间:",
- oneRecord.r_time.ToString("g"),
- " ",
- " "
- };
- foreach(string oneSentance in dataToBeFilled)
- {
- Paragraph p = new Paragraph(oneSentance, new Font(baseFont));
- PdfPCell cell = new PdfPCell(p);
-
- cell.Border = Rectangle.NO_BORDER;
- table.AddCell(cell);
- }
- table.HorizontalAlignment = Element.ALIGN_CENTER;
- //table.
- table.TotalWidth = 500;
- table.LockedWidth = true;
- float[] widths = new float[] { 100, 100, 100, 100, 50, 50 };//三列列宽不同若果是浮点数需要加f
- table.SetWidths(widths);
- document.Add(table);
- document.Add(border);
-
- //最后是文档
-
- var jsonUserSelections = oneRecord.r_selection;
- List<SQLite.UserSelection> uss = JsonMapper.ToObject<List<SQLite.UserSelection>>(jsonUserSelections);
- foreach(SQLite.UserSelection us in uss)
- {
- SQLite.Question qTemp = SQLite.SQLiteModel.getQuestionById(us.q_id);
- SQLite.Answer aTemp = SQLite.SQLiteModel.getAnswerById(us.a_id);
- //System.Console.WriteLine(String.Format("a_id:{0},next_qid:{1}", aTemp.a_id, aTemp.next_q_id));
- if(type == 2 && aTemp.next_q_id != 0)
- {
- //evaluation
- continue;
- }
- else
- {
- //判断PDF模板(0,1,2分)是否存在,如存在则直接调用模板
- string templateName = Constants.template + "\\" + aTemp.a_id.ToString() + ".pdf";
- if (File.Exists(templateName))
- {
- PdfReader readerTemp = new PdfReader(templateName);
- PdfImportedPage newPage;
- PdfContentByte cb = writer.DirectContent;
- int iPageNum = readerTemp.NumberOfPages;
- for (int j = 1; j <= iPageNum; j++)
- {
- //document.NewPage();
- newPage = writer.GetImportedPage(readerTemp, j);
- cb.AddTemplate(newPage, 0, 0);
- }
- }
- else
- {
- //document.NewPage();
- var questionTitle = new Paragraph(String.Format("{0}:{1}", qTemp.q_title, aTemp.a_content), new Font(baseFont, 13, Font.NORMAL, BaseColor.BLACK));
- document.Add(questionTitle);
- if (aTemp.a_description_text != "")
- {
- var description = new Paragraph(String.Format("问题说明:\n {0}", aTemp.a_description_text), new Font(baseFont, 13, Font.NORMAL, BaseColor.BLACK));
- document.Add(description);
- }
- if (aTemp.a_suggestion_text != "")
- {
- var suggestion = new Paragraph(String.Format("建议:\n {0}", aTemp.a_suggestion_text), new Font(baseFont, 13, Font.NORMAL, BaseColor.BLACK));
- document.Add(suggestion);
- }
- }
-
- }
- }
- document.Close();
- //打开文件
- System.Diagnostics.Process.Start(fileName);
- }
- }
- }
|