Java|Springboot集成ItextPdf 7.2.4

1、添加Gradle依赖

        如图所示添加itextpdf 7.2.4核心包依赖,implementation 'com.itextpdf:itext7-core:7.2.4','com.itextpdf:html2pdf:5.0.0'为HTML转PDF,业务需要则添加。

2、创建PDF

A、生成PDF文件到本地 

@GetMapping("createPDF")
	public void createPDF(HttpServletResponse response){
		String fileName= fileNameEncode("病案首页");
		//创建字节输出流
		ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
		try {
			//初始化PDF
			initPDF(fileName,byteArrayOutputStream);
			FileOutputStream fileOutputStream=new FileOutputStream("C:\\Users\\DJW\\Desktop\\1.pdf");
			fileOutputStream.write(byteArrayOutputStream.toByteArray());
			fileOutputStream.flush();
			byteArrayOutputStream.close();
			fileOutputStream.close();
		}  catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

 B、返回PDF字节流

@GetMapping("createPDF")
	public void createPDF(HttpServletResponse response){
		String fileName= fileNameEncode("病案首页");
		response.setHeader("Access-Control-Allow-Origin","*");
		//设置响应内容为PDF
		response.setContentType("application/pdf;charset=UTF-8");
		//设置文件名称
		response.setHeader("Content-Disposition", "inline;filename="+fileName+".pdf");
		//创建字节输出流
		ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
		//初始化PDF
		initPDF(fileName,byteArrayOutputStream);
		try {
			response.getOutputStream().write(byteArrayOutputStream.toByteArray());
			byteArrayOutputStream.flush();
			byteArrayOutputStream.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

1、初始化PDF,创建PDF的Document,设置PDF标题、作者等信息。 

 CUSTOM_SONGTI_FONT为引用的字体路径,如图所示:

void initPDF(String pdfTitle,ByteArrayOutputStream byteArrayOutputStream){
		//创建PDF写入流对象
		PdfWriter pdfWriter=new PdfWriter(byteArrayOutputStream);
		//创建PDF文档
		PdfDocument pdfDocument=new PdfDocument(pdfWriter);
		//获取pdf文档信息
		PdfDocumentInfo pdfDocumentInfo= pdfDocument.getDocumentInfo();
		//设置pdf的标题
		pdfDocumentInfo.setTitle(fileNameDecode(pdfTitle));
		//设置pdf作者
		pdfDocumentInfo.setAuthor("丁家伟");
		//创建文档对象
		Document document=new Document(pdfDocument);
		pdfDocument.setDefaultPageSize(PageSize.A4);
		//添加水印
		pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE,new WaterMarkEventHandler(WATER_MARK_TEXT));
		//添加页码
		pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, new PageEventHandler());
		//整个文档设置中文字体支持
		FontSet fontSet=new FontSet();
		//ttc字体必须加,0
		fontSet.addFont(CUSTOM_SONGTI_FONT+",0");

		FontProvider fontProvider=new FontProvider(fontSet);
		document.setFontProvider(fontProvider);
		//文档设置全局字体:宋体
		document.setFontFamily("SIMSUN");
		//pdfFontSongTi = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", pdfDocument);
		writePDF(document, pdfDocument);
		//组建PDF
		try {
			document.close();
			pdfWriter.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

 2、编辑PDF段落

void writePDF(Document document,PdfDocument pdfDocument) {
		//在pdf文档中添加1个A4纸大小的页面
		pdfDocument.addNewPage(PageSize.A4);
		//机构标题
		Paragraph paragraph0=new Paragraph("XXXXXXX医院");
		Paragraph paragraph1=new Paragraph("门诊病历");
		paragraph0.setMultipliedLeading(1.0f);
		paragraph1.setMultipliedLeading(1.0f);
		paragraph0.addStyle(PdfStyle.titleStyle());
		paragraph1.addStyle(PdfStyle.titleStyle());
		document.add(paragraph0);
		document.add(paragraph1);
		//姓名、性别、年龄
		Paragraph basicInfoP1=new Paragraph();
		basicInfoP1.setMultipliedLeading(1.0f);
		basicInfoP1.addStyle(PdfStyle.percent100Style());
		Paragraph rowP11=new Paragraph();
		rowP11.setMultipliedLeading(1.0f);
		rowP11.addStyle(PdfStyle.col3Style());
		Text t11Left=new Text("姓名:");
		Text t11Right=new Text("测试");
		t11Right.addStyle(PdfStyle.resultStyle());
		rowP11.add(t11Left);
		rowP11.add(t11Right);
		Paragraph rowP12=new Paragraph();
		rowP12.setMultipliedLeading(1.0f);
		rowP12.addStyle(PdfStyle.col3Style());
		Text t12Left=new Text("年龄:");
		Text t12Right=new Text("38岁");
		t12Right.addStyle(PdfStyle.resultStyle());
		rowP12.add(t12Left);
		rowP12.add(t12Right);
		Paragraph rowP13=new Paragraph();
		rowP13.setMultipliedLeading(1.0f);
		rowP13.addStyle(PdfStyle.col3Style());
		Text t13Left=new Text("性别:");
		Text t13Right=new Text("女");
		t13Right.addStyle(PdfStyle.resultStyle());
		rowP13.add(t13Left);
		rowP13.add(t13Right);
		basicInfoP1.setMultipliedLeading(1.0f);
		basicInfoP1.add(rowP11);
		basicInfoP1.add(rowP12);
		basicInfoP1.add(rowP13);
		document.add(basicInfoP1);
		//就诊号 就诊科室 就诊日期
		Paragraph basicInfoP2=new Paragraph();
		basicInfoP2.setMultipliedLeading(1.0f);
		basicInfoP2.addStyle(PdfStyle.percent100Style());
		Paragraph rowP21=new Paragraph();
		rowP21.addStyle(PdfStyle.col3Style());
		Text t21Left=new Text("病历号:");
		Text t21Right=new Text("2403010000541");
		t21Right.addStyle(PdfStyle.resultStyle());
		rowP21.add(t21Left);
		rowP21.add(t21Right);
		Paragraph rowP22=new Paragraph();
		rowP22.addStyle(PdfStyle.col3Style());
		Text t22Left=new Text("就诊科室:");
		Text t22Right=new Text("肛肠科");
		t22Right.addStyle(PdfStyle.resultStyle());
		rowP22.add(t22Left);
		rowP22.add(t22Right);
		Paragraph rowP23=new Paragraph();
		rowP23.addStyle(PdfStyle.col3Style());
		Text t23Left=new Text("就诊时间:");
		Text t23Right=new Text("2024-03-01 15:17:18");
		t23Right.addStyle(PdfStyle.resultStyle());
		rowP23.add(t23Left);
		rowP23.add(t23Right);
		basicInfoP2.setMultipliedLeading(1.0f);
		basicInfoP2.add(rowP21);
		basicInfoP2.add(rowP22);
		basicInfoP2.add(rowP23);
		document.add(basicInfoP2);
		//诊断
		Paragraph basicInfoP3=new Paragraph();
		basicInfoP3.setMultipliedLeading(1.0f);
		basicInfoP3.addStyle(PdfStyle.percent100Style());
		Text t31=new Text("");
		t31.setText("诊断:混合痔;肛周脓肿;");
		basicInfoP3.add(t31);
		document.add(basicInfoP3);
		//主要内容
		Paragraph paragraphMain=new Paragraph();
		paragraphMain.addStyle(PdfStyle.mainStyle());
		//设置上边框
		Border mainTopBorder=new SolidBorder(0.5f);
		paragraphMain.setBorderTop(mainTopBorder);
		//编辑主要内容
		Paragraph zsParagraph=new Paragraph();
		zsParagraph.setMultipliedLeading(1.0f);
		Text zsParagraphTitle=new Text("主诉:");
		zsParagraphTitle.setBold();
		Text zsParagraphContent=new Text("患者诉3天前和朋友在海底捞火锅店就餐,要求店家上了特特特辣锅,就餐完毕回家突感胃疼,且腹泻,大便糖稀,且带血,随即前往药店购买马应龙痔疮膏涂抹无效,今日来我院肛肠科就诊。");
		zsParagraph.add(zsParagraphTitle);
		zsParagraph.add(zsParagraphContent);
		paragraphMain.add(zsParagraph);
		//现病史
		Paragraph xbsParagraph=new Paragraph();
		xbsParagraph.setMultipliedLeading(1.0f);
		Text xbsParagraphTitle=new Text("现病史:");
		xbsParagraphTitle.setBold();
		Text xbsParagraphContent=new Text("腹泻,肛门疼痛。");
		xbsParagraph.add(xbsParagraphTitle);
		xbsParagraph.add(xbsParagraphContent);
		paragraphMain.add(xbsParagraph);
		//体格检查
		Paragraph ctParagraph=new Paragraph();
		ctParagraph.setMultipliedLeading(1.0f);
		Text ctParagraphTitle=new Text("体格检查:");
		ctParagraphTitle.setBold();
		Text ctParagraphContent=new Text("℃p82次/分r21次/分bp220/110mmhg。急性病容,步行入院,步态正常,神志清楚,对答切题,。查体合作。皮肤粘膜无黄染、皮疹、出血点。全身浅表淋巴结未触及明显肿大。双侧瞳孔等大等园,直径约2毫米,对光反射及调节均灵敏。口唇无苍白,颊粘膜无破溃,舌苔正常,舌质淡红,伸舌居中,咽充血明显,后壁见滤泡,双侧扁桃体无肿大。双肺呼吸音运动正常,触觉语颤对称,未触及皮下捻发感及胸膜摩擦感,双肺叩诊呈清音,呼吸音粗,双肺未闻及干湿性啰音,无胸膜摩擦音;心前区无隆起,心尖搏动位于左侧第v间锁骨中线内厘米处,未触及震颤,心界无明显扩大,心率82次/分,律齐,各瓣膜听诊区未闻及明显病理性杂音。");
		ctParagraph.add(ctParagraphTitle);
		ctParagraph.add(ctParagraphContent);
		paragraphMain.add(ctParagraph);
		//辅助检查
		Paragraph fzParagraph=new Paragraph();
		fzParagraph.setMultipliedLeading(1.0f);
		Text fzParagraphTitle=new Text("辅助检查:");
		fzParagraphTitle.setBold();
		Text fzParagraphContent=new Text("白细胞∕l中性细胞百分比74% 淋巴细胞比率%红细胞∕l 血红蛋白140g∕l 血小板15710^9∕l,生化全套 r-谷氨转肽酶59iu∕l 钾∕l 二氧化碳 mmol∕l 阴离子间隙 mmol∕l 尿酸 umol∕l 总胆固醇 mmol∕l 余大致正常。 x线示心影增大。8月3日胸部ct示1两肺下叶炎症伴双侧胸腔少量积液;2心脏增大,左侧冠脉钙化。8月6日电解质钠 mmol∕l。");
		fzParagraph.add(fzParagraphTitle);
		fzParagraph.add(fzParagraphContent);
		paragraphMain.add(fzParagraph);
		//既往史
		Paragraph jwsParagraph=new Paragraph();
		jwsParagraph.setMultipliedLeading(1.0f);
		Text jwsParagraphTitle=new Text("既往史:");
		jwsParagraphTitle.setBold();
		Text jwsParagraphContent=new Text("无。");
		jwsParagraph.add(jwsParagraphTitle);
		jwsParagraph.add(jwsParagraphContent);
		paragraphMain.add(jwsParagraph);
		//医生签名
		Paragraph doctorSignParagraph=new Paragraph();
		doctorSignParagraph.setMarginRight(40f);
		doctorSignParagraph.addStyle(PdfStyle.percent100Style());
		doctorSignParagraph.setTextAlignment(TextAlignment.RIGHT);
		doctorSignParagraph.setMultipliedLeading(1.5f);
		Text doctorSignTitle=new Text("医生签名:");
		doctorSignTitle.setBold();
		doctorSignParagraph.add(doctorSignTitle);
		paragraphMain.add(doctorSignParagraph);
		document.add(paragraphMain);
	}

 3、通过浏览器调用方法查看效果

  • 14
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值