package com.ltkj.web.controller.his;
|
|
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.json.JSONArray;
|
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONUtil;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
import com.itextpdf.text.*;
|
import com.itextpdf.text.pdf.*;
|
import com.itextpdf.text.pdf.draw.LineSeparator;
|
import com.ltkj.common.core.domain.AjaxResult;
|
import com.ltkj.common.core.domain.entity.SysDictData;
|
import com.ltkj.common.utils.DateUtils;
|
import com.ltkj.framework.config.MatchUtils;
|
import com.ltkj.hosp.domain.*;
|
import com.ltkj.hosp.service.ITjCustomerService;
|
import com.ltkj.hosp.service.ITjOrderService;
|
import com.ltkj.hosp.service.ITjReportService;
|
import com.ltkj.hosp.service.ITjReportTemplateService;
|
import com.ltkj.mall.mallOrderUtils.TjConstants;
|
import com.ltkj.system.service.ISysDictDataService;
|
import com.ltkj.web.config.pdfutils.PdfUtils;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Component;
|
|
import java.io.*;
|
import java.lang.reflect.Field;
|
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.Method;
|
import java.util.*;
|
import java.util.List;
|
|
import static com.ltkj.web.config.pdfutils.PDFBinaryUtil.getPDFBinary;
|
|
/**
|
* 检查 检验报告pdf生成
|
*/
|
@Slf4j
|
@Component
|
public class HisPDFUtil {
|
@Autowired
|
private HisApiGetMethodService apiGetMethodService;
|
@Autowired
|
private ITjOrderService tjOrderService;
|
@Autowired
|
private ITjCustomerService tjCustomerService;
|
@Autowired
|
private ITjReportService tjReportService;
|
@Value("${path.filePath}")
|
private String value;
|
@Value("${path.reportServer}")
|
private String urlValue;
|
|
public AjaxResult execHisRequest(HashMap<String, Object> pdfInfoMap, Map<String, Object> params,TjOrder tjOrder,TjReportTemplate tjReportTemplate) {
|
List<Map<String, Object>> list;
|
String hisRegistrationId = params.get("his_registration_id").toString();
|
AjaxResult getlabreportinfo = apiGetMethodService.getHISDataNew("Getlabreportinfo", params);
|
list = makePDFInfo(pdfInfoMap, getlabreportinfo, "获取检验报告记录信息");
|
JSONArray jsonArray = JSONUtil.createArray();
|
if (list != null) {
|
params.clear();
|
for (Map<String, Object> stringObjectMap : list) {
|
if (stringObjectMap == null)
|
continue;
|
JSONObject obj = JSONUtil.createObj();
|
obj.putOpt("报告记录", stringObjectMap);
|
String jybgid = stringObjectMap.get("JYBGID").toString();
|
params.put("jybgid", jybgid);
|
// 获取检验结果记录信息
|
AjaxResult getlabdetailinfo = apiGetMethodService.getHISDataNew("Getlabdetailinfo", params);
|
putData(obj, getlabdetailinfo, "检验结果记录");
|
// 获取微生物报告记录信息
|
AjaxResult getlabgermrepinfo = apiGetMethodService.getHISDataNew("Getlabgermrepinfo", params);
|
putData(obj, getlabgermrepinfo, "微生物报告记录");
|
// 获取微生物药敏信息
|
AjaxResult getlabgermdetailinfo = apiGetMethodService.getHISDataNew("Getlabgermdetailinfo", params);
|
putData(obj, getlabgermdetailinfo, "微生物药敏信息");
|
// 获取危急值信息
|
AjaxResult getlaburgentinfo = apiGetMethodService.getHISDataNew("Getlaburgentinfo", params);
|
putData(obj, getlaburgentinfo, "危机值信息");
|
jsonArray.put(obj);
|
}
|
// 获取检查报告信息
|
params.clear();
|
params.put("his_registration_id", hisRegistrationId);
|
params.put("ksmc", "");
|
params.put("pationid", "");
|
params.put("ksrq", "");
|
params.put("jsrq", "");
|
params.put("pagecount", 100);
|
params.put("page", 1);
|
params.put("status", "");
|
AjaxResult getexamreportinfo = apiGetMethodService.getHISDataNew("Getexamreportinfo", params);
|
JSONObject object = JSONUtil.parseObj(getexamreportinfo.get("result"));
|
JSONObject response = JSONUtil.parseObj(object.get("Response"));
|
if (!response.getStr("ResultCode").equals("0")) {
|
if (!response.getStr("ResultContent").contains("查询失败"))
|
return AjaxResult.error("生成超时,请重试");
|
}
|
JSONObject obj = JSONUtil.createObj();
|
putData(obj, getexamreportinfo, "检查报告");
|
jsonArray.put(obj);
|
System.out.println(JSONUtil.toJsonStr(jsonArray));
|
if (makePDF(hisRegistrationId, jsonArray,tjOrder,tjReportTemplate))
|
return AjaxResult.success("已生成报告!可直接点击预览!");
|
else return AjaxResult.error();
|
} else {
|
return AjaxResult.error("未获取到检验报告记录信息");
|
}
|
}
|
|
private List<Map<String, Object>> makePDFInfo(HashMap<String, Object> map, AjaxResult ajaxResult, String key) {
|
List<Map<String, Object>> data = null;
|
if (Integer.parseInt(ajaxResult.get("code").toString()) == 200) {
|
data = (List<Map<String, Object>>) ajaxResult.get("data");
|
List<Map<String, Object>> parent = (List<Map<String, Object>>) map.get(key);
|
if (parent != null) {
|
parent.addAll(data);
|
} else {
|
map.put(key, data);
|
}
|
}
|
return data;
|
}
|
|
public void putData(JSONObject jsonObject, AjaxResult result, String key) {
|
if (Integer.parseInt(result.get("code").toString()) == 200) {
|
List<Map<String, Object>> data = (List<Map<String, Object>>) result.get("data");
|
jsonObject.putOpt(key, data);
|
}
|
}
|
|
public Boolean makePDF(String cardId, JSONArray jsonArray,TjOrder tjOrder,TjReportTemplate reportTemplate) {
|
String tjNumber = tjOrder.getTjNumber();
|
String template = reportTemplate.getTemplate();
|
LambdaQueryWrapper<TjCustomer> wq1 = new LambdaQueryWrapper<>();
|
wq1.eq(TjCustomer::getCusId, tjOrder.getUserId());
|
TjCustomer tjCustomer = tjCustomerService.getOne(wq1);
|
Long userId = tjCustomer.getCusId();
|
// 生成pdf
|
try {
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
PdfReader reader = new PdfReader(new ByteArrayInputStream(Base64.getDecoder().decode(template)));
|
PdfStamper pdfStamper = new PdfStamper(reader, byteArrayOutputStream);
|
// 获取pdf表单
|
AcroFields acroFields = pdfStamper.getAcroFields();
|
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
|
acroFields.addSubstitutionFont(bf);
|
HashMap<String, Object> data = new HashMap<>();
|
// 设置用户信息
|
setUserInfo(tjNumber, tjOrder, tjCustomer, data);
|
//异常项目
|
// 遍历data 给pdf表单表格赋值
|
for (String key : data.keySet()) {
|
acroFields.setField(key, data.get(key).toString());
|
}
|
//设置为无法编辑
|
pdfStamper.setFormFlattening(true);
|
pdfStamper.close();
|
reader.close();
|
// 将修改后的PDF内容写入
|
byte[] bytes = byteArrayOutputStream.toByteArray();
|
// 创建新的PDF文档
|
Document document = new Document(PageSize.A4);
|
ByteArrayOutputStream finalOutPut = new ByteArrayOutputStream();
|
PdfCopy copy = new PdfCopy(document, finalOutPut);
|
document.open();
|
reader = new PdfReader(bytes);
|
int numberOfPages = reader.getNumberOfPages();
|
for (int i = 1; i <= numberOfPages; i++) {
|
PdfImportedPage page = copy.getImportedPage(reader, i);
|
copy.addPage(page);
|
}
|
// 添加详情页
|
document.newPage();
|
ByteArrayOutputStream byteArrayOutputStream1 = new ByteArrayOutputStream();
|
Document document1 = new Document(PageSize.A4);
|
PdfWriter pdfWriter = PdfWriter.getInstance(document1, byteArrayOutputStream1);
|
document1.setMargins(70, 70, 40, 40);
|
document1.open();
|
List<ByteArrayOutputStream> jianchas = new ArrayList<>();
|
for (Object o : jsonArray) {
|
JSONObject jsonObject = (JSONObject) o;
|
// 检验报告以及结果信息
|
JSONObject reportInfo = jsonObject.getJSONObject("报告记录");
|
String titleName = "";
|
if (reportInfo != null) {
|
titleName = reportInfo.getStr("JYXMMC").trim();
|
}
|
JSONArray reportDetails = jsonObject.getJSONArray("检验结果记录");
|
ArrayList<TjPdfVO> list = new ArrayList<>();
|
String[] titles = null;
|
float[] titlesWidth = null;
|
// 结果信息
|
if (reportDetails != null && reportDetails.size() > 0) {
|
for (Object reportDetail : reportDetails) {
|
JSONObject detail = (JSONObject) reportDetail;
|
TjPdfVO tjPdfVO = new TjPdfVO();
|
tjPdfVO.setProName(detail.getStr("JCZBMC"));
|
tjPdfVO.setProResult(detail.getStr("JCZBJG"));
|
tjPdfVO.setVal1(detail.getStr("JLDW"));
|
tjPdfVO.setVal2(detail.getStr("CKZFW"));
|
list.add(tjPdfVO);
|
}
|
titles = new String[]{"体检项目", "体检结果", "单位", "参考范围"};
|
titlesWidth = new float[]{120, 120, 120, 120};
|
addTable(document1, titleName, titles, titlesWidth, list);
|
// 主检医师
|
String bgysqm = reportInfo.getStr("BGYSQM");
|
String doctorName = "主检医师:" + bgysqm;
|
Font doctorFonts = PdfUtils.setFont(9);
|
Paragraph df = PdfUtils.setParagraph(doctorFonts, doctorName);
|
df.setAlignment(Element.ALIGN_RIGHT);
|
document1.add(df);
|
//分割线
|
LineSeparator objectName = new LineSeparator();
|
document1.add(objectName);
|
}
|
// 微生物报告记录信息
|
JSONArray labgermrepInfos = jsonObject.getJSONArray("微生物报告记录");
|
if (labgermrepInfos != null && labgermrepInfos.size() > 0) {
|
list = new ArrayList<>();
|
for (Object info : labgermrepInfos) {
|
JSONObject object = (JSONObject) info;
|
TjPdfVO tjPdfVO = new TjPdfVO();
|
tjPdfVO.setProName(object.getStr("XJMC"));
|
tjPdfVO.setProResult(object.getStr("JCJG"));
|
list.add(tjPdfVO);
|
}
|
titles = new String[]{"体检项目", "体检结果"};
|
titlesWidth = new float[]{120, 120};
|
addTable(document1, titleName, titles, titlesWidth, list);
|
//分割线
|
LineSeparator objectName = new LineSeparator();
|
document1.add(objectName);
|
}
|
// 微生物药敏信息
|
JSONArray labgermdetailinfos = jsonObject.getJSONArray("微生物药敏信息");
|
if (labgermdetailinfos != null && labgermdetailinfos.size() > 0) {
|
list = new ArrayList<>();
|
for (Object info : labgermdetailinfos) {
|
JSONObject object = (JSONObject) info;
|
TjPdfVO tjPdfVO = new TjPdfVO();
|
tjPdfVO.setProName(object.getStr("YMMC"));
|
tjPdfVO.setProResult(object.getStr("JCJG"));
|
tjPdfVO.setVal1(object.getStr("ZPHYL"));
|
tjPdfVO.setVal2(object.getStr("YJND"));
|
tjPdfVO.setVal3(object.getStr("YJHZJ"));
|
list.add(tjPdfVO);
|
}
|
titles = new String[]{"药敏名称", "检查结果", "纸片含药量", "抑菌浓度", "抑菌环直径"};
|
titlesWidth = new float[]{90, 90, 90, 90, 90};
|
addTable(document1, titleName, titles, titlesWidth, list);
|
//分割线
|
LineSeparator objectName = new LineSeparator();
|
document1.add(objectName);
|
}
|
// 危急值信息
|
JSONArray laburgentinfos = jsonObject.getJSONArray("危机值信息");
|
if (laburgentinfos != null && laburgentinfos.size() > 0) {
|
list = new ArrayList<>();
|
for (Object info : laburgentinfos) {
|
JSONObject object = (JSONObject) info;
|
TjPdfVO tjPdfVO = new TjPdfVO();
|
tjPdfVO.setProName(object.getStr("zbmc"));
|
tjPdfVO.setProResult(object.getStr("jg"));
|
tjPdfVO.setVal1(object.getStr("clfs"));
|
tjPdfVO.setVal2(object.getStr("bgcljg"));
|
list.add(tjPdfVO);
|
}
|
titles = new String[]{"指标名称", "危急值结果", "处理方式", "处理意见"};
|
titlesWidth = new float[]{120, 120, 120, 120};
|
addTable(document1, titleName, titles, titlesWidth, list);
|
//分割线
|
LineSeparator objectName = new LineSeparator();
|
document1.add(objectName);
|
}
|
JSONArray object = jsonObject.getJSONArray("检查报告");
|
if (object != null && object.size() > 0) {
|
for (Object info : object) {
|
JSONObject obj = (JSONObject) info;
|
String xm = obj.getStr("HZXM");
|
String xb = obj.getStr("HZXB").equals("2") ? "女" : "男";
|
String nl = obj.getStr("HZNL");
|
if (StrUtil.isNotBlank(nl) && nl.contains(".")) {
|
String[] split = nl.split("\\.");
|
nl = split[0];
|
}
|
String jcks = obj.getStr("JCKSMC");
|
String zyh = "";
|
String sqks = obj.getStr("SQKSMC");
|
String sjks = obj.getStr("ZDJGMC");
|
String jcsb = obj.getStr("SBBM");
|
String jch = obj.getStr("JCH");
|
String jcxm = obj.getStr("JCXMMC");
|
String ms = obj.getStr("YXBX");
|
String ts = obj.getStr("YXZD");
|
String ch = obj.getStr("BZBCH");
|
// 报告医师签名
|
String bgysqm = obj.getStr("BGYSQM");
|
// 审核医师签名
|
String shysqm = obj.getStr("SHYSQM");
|
// 申请人
|
String sqrxm = obj.getStr("SQRXM");
|
String footer = "申请人:"+sqrxm + " 审核医师:"+shysqm+" "+bgysqm;
|
ByteArrayOutputStream outputStream = makeJcPDFByteArrayStream(xm, xb, nl, jcks, zyh, ch, sjks, jcsb, jch, jcxm, ms, ts, null,footer);
|
jianchas.add(outputStream);
|
}
|
}
|
}
|
document1.close();
|
pdfWriter.close();
|
reader = new PdfReader(byteArrayOutputStream1.toByteArray());
|
int numberOfPages1 = reader.getNumberOfPages();
|
for (int i = 1; i <= numberOfPages1; i++) {
|
PdfImportedPage page = copy.getImportedPage(reader, i);
|
copy.addPage(page);
|
}
|
for (ByteArrayOutputStream jiancha : jianchas) {
|
reader = new PdfReader(jiancha.toByteArray());
|
numberOfPages1 = reader.getNumberOfPages();
|
for (int i = 1; i <= numberOfPages1; i++) {
|
PdfImportedPage page = copy.getImportedPage(reader, i);
|
copy.addPage(page);
|
}
|
jiancha.close();
|
}
|
byteArrayOutputStream1.close();
|
document.close();
|
copy.close();
|
reader.close();
|
// String outputPath1 = "/Users/chacca/开发相关/代码/";
|
String outputPath1 = value+"\\";
|
String outputFileName1 = userId + tjNumber + tjCustomer.getCusName() + "_报告.pdf";
|
File file = new File(outputPath1 + outputFileName1);
|
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
|
outputStream.write(finalOutPut.toByteArray());
|
outputStream.flush();
|
outputStream.close();
|
finalOutPut.close();
|
reader.close();
|
// 保存数据
|
TjReport tjReport = new TjReport();
|
tjReport.setTjNumber(String.valueOf(tjNumber));
|
tjReport.setPath(urlValue + outputFileName1);
|
String pdfBinary = getPDFBinary(outputPath1 + outputFileName1);
|
tjReport.setReport(pdfBinary);
|
tjReport.setType("体检报告");
|
tjReport.setPrint("pdf");
|
tjReportService.save(tjReport);
|
//修改order表中的打印报告时间为当前时间
|
LambdaUpdateWrapper<TjOrder> updateWrapper = new LambdaUpdateWrapper<>();
|
updateWrapper.eq(TjOrder::getTjNumber, tjNumber);
|
updateWrapper.set(TjOrder::getReportTime, new Date());
|
updateWrapper.set(TjOrder::getPrintLastTime, new Date());
|
updateWrapper.set(TjOrder::getDownloadLastTime, new Date());
|
updateWrapper.set(TjOrder::getStatus, TjConstants.TJ_END);
|
tjOrderService.update(updateWrapper);
|
} catch (IOException | DocumentException e) {
|
e.printStackTrace();
|
return false;
|
}
|
return true;
|
}
|
|
/**
|
* 设置用户信息
|
*
|
* @param tjNumber
|
* @param tjOrder
|
* @param tjCustomer
|
* @param data
|
*/
|
private void setUserInfo(String tjNumber, TjOrder tjOrder, TjCustomer tjCustomer, Map<String, Object> data) {
|
//填充性别:判断男女
|
Long cusSex = tjCustomer.getCusSex();
|
String sex;
|
String name1;
|
String tjtype;
|
if (cusSex == 0) {
|
sex = "男";
|
name1 = tjCustomer.getCusName() + "先生";
|
} else {
|
sex = "女";
|
name1 = tjCustomer.getCusName() + "女士";
|
}
|
data.put("name", tjCustomer.getCusName());
|
data.put("cusId", tjCustomer.getCusId());
|
data.put("name1", name1);
|
if ("1".equals(tjOrder.getTjType())) {
|
tjtype = "团队";
|
data.put("tjtype", tjtype);
|
} else {
|
tjtype = "个人";
|
data.put("tjtype", tjtype);
|
}
|
data.put("dstype", "统一");
|
data.put("sex", sex);
|
data.put("age", DateUtil.ageOfNow(tjCustomer.getCusBrithday()));
|
data.put("tjnumber", tjNumber);
|
data.put("tjdate", DateUtils.parseDateToStr("yyyy/MM/dd", tjOrder.getCreateTime()));
|
data.put("phone", tjCustomer.getCusPhone());
|
data.put("idcard", MatchUtils.hideIdCardNum(tjCustomer.getCusIdcard()));
|
data.put("num2", 0);
|
data.put("num3", 4);
|
}
|
|
private static void addTable(Document document, String titleName, String[] titles, float[] titlesWidth, ArrayList<TjPdfVO> list) throws DocumentException {
|
Font titleFont = PdfUtils.setFont(9);
|
Paragraph paragraph = new Paragraph(titleName, titleFont);
|
// 设置文字居中
|
paragraph.setAlignment(Element.ALIGN_LEFT);
|
// 行间距
|
// paragraph.setLeading(5f);
|
// 设置段落上空白
|
paragraph.setSpacingBefore(10f);
|
// 设置段落下空白
|
paragraph.setSpacingAfter(10f);
|
document.add(paragraph);
|
// 创建一个表格并添加到文档
|
Font headFont = PdfUtils.setFont(9);
|
Font textFont = PdfUtils.setFont(9);
|
PdfPTable table = new PdfPTable(titlesWidth);
|
table.setTotalWidth(PageSize.A4.getWidth() - 100); // 设置表格总宽度
|
table.setLockedWidth(true); // 锁定表格宽度
|
for (String title : titles) {
|
PdfPCell cell = new PdfPCell(new Paragraph(title, headFont));
|
cell.setBorder(Rectangle.NO_BORDER); // 移除单元格边框
|
table.addCell(cell);
|
}
|
for (TjPdfVO item : list) {
|
Class<? extends TjPdfVO> aClass = item.getClass();
|
Field[] fields = aClass.getDeclaredFields();
|
ArrayList<String> values = new ArrayList<>();
|
for (Field field : fields) {
|
String fieldName = field.getName();
|
String getterName = "get" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
|
try {
|
Method method = aClass.getDeclaredMethod(getterName);
|
if (method != null) {
|
Object invoke = method.invoke(item);
|
if (invoke != null) {
|
values.add(String.valueOf(invoke));
|
}
|
}
|
} catch (NoSuchMethodException e) {
|
log.error("根据字段获取get方法异常 ->{}", e.getMessage());
|
} catch (InvocationTargetException | IllegalAccessException e) {
|
log.error("反射获取类字段异常 ->{}", e.getMessage());
|
}
|
}
|
for (int i = 0; i < values.size(); i++) {
|
String value = String.valueOf(values.get(i));
|
Paragraph elements = null;
|
if (StrUtil.isBlank(value) || value.equals("null")) {
|
elements = new Paragraph("", textFont);
|
} else {
|
elements = new Paragraph(value, textFont);
|
}
|
PdfPCell nameCell = new PdfPCell(elements);
|
nameCell.setBorder(Rectangle.NO_BORDER); // 移除单元格边框
|
table.addCell(nameCell);
|
}
|
}
|
// 将表格添加到文档
|
document.add(table);
|
}
|
|
/**
|
* 创建检查pdf
|
* @param xm
|
* @param xb
|
* @param nl
|
* @param jcks
|
* @param zyh
|
* @param ch
|
* @param sjks
|
* @param jcsb
|
* @param jch
|
* @param jcxm
|
* @param ms
|
* @param ts
|
* @param files
|
* @param footer
|
* @return bytearrayoutputstream
|
*/
|
public static ByteArrayOutputStream makeJcPDFByteArrayStream(String xm, String xb, String nl, String jcks, String zyh, String ch, String sjks, String jcsb, String jch, String jcxm, String ms, String ts, File[] files,String footer) {
|
try {
|
Document document = new Document(PageSize.A4);
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
PdfWriter writer = PdfWriter.getInstance(document, baos);
|
writer.setPageEvent(new Footer(footer));
|
document.open();
|
document.setMargins(70, 70, 40, 40);
|
|
// 创建第一个表格:图片之前的内容
|
PdfPTable table1 = new PdfPTable(10);
|
table1.setWidthPercentage(100);
|
table1.setSpacingBefore(10f);
|
table1.setSpacingAfter(10f);
|
|
Font titleFont = PdfUtils.setFont(12);
|
PdfPCell mergedCell = new PdfPCell(new Phrase("泾 川 县 人 民 医 院", titleFont));
|
mergedCell.setBorder(Rectangle.NO_BORDER);
|
mergedCell.setColspan(10);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase(" "));
|
mergedCell.setBorder(Rectangle.NO_BORDER);
|
mergedCell.setColspan(10);
|
mergedCell.setFixedHeight(1f);
|
table1.addCell(mergedCell);
|
|
titleFont = PdfUtils.setFont(15);
|
mergedCell = new PdfPCell(new Phrase("检查报告单", titleFont));
|
mergedCell.setBorder(Rectangle.NO_BORDER);
|
mergedCell.setColspan(10);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase(" "));
|
mergedCell.setBorder(Rectangle.NO_BORDER);
|
mergedCell.setColspan(10);
|
table1.addCell(mergedCell);
|
|
titleFont = PdfUtils.setFont(10);
|
PdfPCell textFieldCell = new PdfPCell();
|
|
mergedCell = new PdfPCell(new Phrase("姓 名:", titleFont));
|
mergedCell.setBorder(Rectangle.TOP);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(1);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase(xm, titleFont));
|
mergedCell.setBorder(Rectangle.TOP);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(1);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase("性 别:", titleFont));
|
mergedCell.setBorder(Rectangle.TOP);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(1);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase(xb, titleFont));
|
mergedCell.setBorder(Rectangle.TOP);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(1);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase("年 龄:", titleFont));
|
mergedCell.setBorder(Rectangle.TOP);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(1);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase(nl, titleFont));
|
mergedCell.setBorder(Rectangle.TOP);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(1);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase("检查科室:", titleFont));
|
mergedCell.setBorder(Rectangle.TOP);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(1);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase(jcks, titleFont));
|
mergedCell.setBorder(Rectangle.TOP);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(3);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase("住 院 号:", titleFont));
|
mergedCell.setBorder(Rectangle.NO_BORDER);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(1);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase(zyh, titleFont));
|
mergedCell.setBorder(Rectangle.NO_BORDER);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(1);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase("床 号:", titleFont));
|
mergedCell.setBorder(Rectangle.NO_BORDER);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(1);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase(ch, titleFont));
|
mergedCell.setBorder(Rectangle.NO_BORDER);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(1);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase("送检科室:", titleFont));
|
mergedCell.setBorder(Rectangle.NO_BORDER);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(1);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase(sjks, titleFont));
|
mergedCell.setBorder(Rectangle.NO_BORDER);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(2);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase("检查设备:", titleFont));
|
mergedCell.setBorder(Rectangle.NO_BORDER);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(1);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase(jcsb, titleFont));
|
mergedCell.setBorder(Rectangle.NO_BORDER);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(2);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase("检查号:", titleFont));
|
mergedCell.setBorder(Rectangle.BOTTOM);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(1);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase(jch, titleFont));
|
mergedCell.setBorder(Rectangle.BOTTOM);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(2);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase("检查项目:", titleFont));
|
mergedCell.setBorder(Rectangle.BOTTOM);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(1);
|
table1.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase(jcxm, titleFont));
|
mergedCell.setBorder(Rectangle.BOTTOM);
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setColspan(6);
|
table1.addCell(mergedCell);
|
|
// 创建第二个表格:显示图片部分
|
PdfPTable table2 = new PdfPTable(10);
|
float[] columnWidths = {0.8f, 0.8f, 0.8f, 0.8f, 0.1f, 0.1f, 0.8f, 0.8f, 0.8f, 0.8f};
|
table2.setWidths(columnWidths);
|
|
if(files != null){
|
int imageIndex = 0;
|
while (imageIndex < files.length) {
|
// 第一张图片占位符
|
if (imageIndex < files.length) {
|
PdfPCell imageCell = createImageCell(files[imageIndex].getPath());
|
imageCell.setColspan(4);
|
table2.addCell(imageCell);
|
imageIndex++;
|
}
|
// 调整这个单元格占用的宽度 不影响其他行
|
PdfPCell middleBlankCell = new PdfPCell(new Phrase(""));
|
middleBlankCell.setColspan(2);
|
middleBlankCell.setBorder(Rectangle.NO_BORDER);
|
table2.addCell(middleBlankCell);
|
// 第二张图片占位符
|
if (imageIndex < files.length) {
|
PdfPCell imageCell = createImageCell(files[imageIndex].getPath());
|
imageCell.setColspan(4);
|
table2.addCell(imageCell);
|
imageIndex++;
|
}
|
// 完成当前行
|
table2.completeRow();
|
}
|
}
|
// 创建第三个表格:图片之后的内容
|
PdfPTable table3 = new PdfPTable(10);
|
table3.setWidthPercentage(100);
|
table3.setSpacingBefore(10f);
|
table3.setSpacingAfter(10f);
|
|
mergedCell = new PdfPCell(new Phrase("描述:", titleFont));
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setBorder(Rectangle.NO_BORDER);
|
table3.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase(""));
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setBorder(Rectangle.NO_BORDER);
|
mergedCell.setColspan(9);
|
table3.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase("", titleFont));
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setBorder(Rectangle.NO_BORDER);
|
table3.addCell(mergedCell);
|
|
Phrase phrase = new Phrase(ms, titleFont);
|
PdfPCell textCell = new PdfPCell(phrase);
|
textCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
textCell.setBorder(Rectangle.NO_BORDER);
|
textCell.setColspan(10);
|
table3.addCell(textCell);
|
|
mergedCell = new PdfPCell(new Phrase("提示:", titleFont));
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setBorder(Rectangle.NO_BORDER);
|
table3.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase(""));
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setBorder(Rectangle.NO_BORDER);
|
mergedCell.setColspan(9);
|
table3.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase("", titleFont));
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setBorder(Rectangle.NO_BORDER);
|
table3.addCell(mergedCell);
|
|
mergedCell = new PdfPCell(new Phrase(ts, titleFont));
|
mergedCell.setHorizontalAlignment(Element.ALIGN_LEFT);
|
mergedCell.setBorder(Rectangle.NO_BORDER);
|
mergedCell.setColspan(9);
|
table3.addCell(mergedCell);
|
|
// 添加表格到文档
|
document.add(table1);
|
document.add(table2);
|
// 在添加表格前,检查当前页是否有足够空间,如果没有则添加新页。
|
if (writer.getVerticalPosition(true) - table3.getTotalHeight() < document.bottomMargin()) {
|
document.newPage();
|
}
|
document.add(table3);
|
document.close();
|
writer.close();
|
return baos;
|
} catch (DocumentException | IOException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
private static PdfPCell createImageCell(String path) throws IOException, BadElementException {
|
Image image = Image.getInstance(path);
|
image.scaleToFit(150, 150);
|
PdfPCell cell = new PdfPCell(image);
|
cell.setBorder(Rectangle.NO_BORDER);
|
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
|
// cell.setPadding(5);
|
return cell;
|
}
|
|
static class Footer extends PdfPageEventHelper {
|
private String footerText;
|
private Font footerFont = PdfUtils.setFont(10);
|
|
public Footer(String footerText) {
|
this.footerText = footerText;
|
}
|
|
@Override
|
public void onEndPage(PdfWriter writer, Document document) {
|
PdfPTable footer = new PdfPTable(1);
|
footer.setTotalWidth(523);
|
footer.setWidthPercentage(100);
|
footer.getDefaultCell().setBorder(Rectangle.NO_BORDER);
|
footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
|
footer.addCell(new Phrase(footerText, footerFont));
|
|
footer.writeSelectedRows(0, -1, 36, 36, writer.getDirectContent());
|
}
|
}
|
}
|