这次主要画的是心电图诊断报告,用iText7生成pdf。
iText7的相关jar包可在iText7官网下载。
代码如下所示:
package com.pdf;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
public class PDFtest {
public static void main(String[] args) throws IOException {
// 创建文档对象 ,设置文档大小为横向A4
Document document = new Document(PageSize.A4.rotate());
try {
// 关联文档对象与输出流
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("E:/ecgtest/firstPdf.pdf"));
// 打开文档
document.open();
// 获取内容
PdfContentByte cb = writer.getDirectContent();
// 设置文本,使用windox自带字体可显示中文
BaseFont bf = BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF",
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
cb.beginText();
cb.setFontAndSize(bf, 12);
// 设置波形名称位置
int a = 30, b = 14;
cb.setTextMatrix(a, 50 + b);
cb.showText(" V6");
cb.setTextMatrix(a, 50 + 4 * b);
cb.showText(" V5");
cb.setTextMatrix(a, 50 + 7 * b);
cb.showText(" V4");
cb.setTextMatrix(a, 50 + 10 * b);
cb.showText(" V3");
cb.setTextMatrix(a, 50 + 13 * b);
cb.showText(" V2");
cb.setTextMatrix(a, 50 + 16 * b);
cb.showText(" V1");
cb.setTextMatrix(a, 50 + 19 * b);
cb.showText("aVF");
cb.setTextMatrix(a, 50 + 22 * b);
cb.showText("aVL");
cb.setTextMatrix(a, 50 + 25 * b);
cb.showText("aVR");
cb.setTextMatrix(a, 50 + 28 * b);
cb.showText("III");
cb.setTextMatrix(a, 50 + 31 * b);
cb.showText(" II");
cb.setTextMatrix(a, 50 + 34 * b);
cb.showText(" I");
// 设置时间位置
int c = 64, d = 70;
cb.setTextMatrix(c, 40);
cb.showText("0s");
cb.setTextMatrix(c + d, 40);
cb.showText("1s");
cb.setTextMatrix(c + 2 * d, 40);
cb.showText("2s");
cb.setTextMatrix(c + 3 * d, 40);
cb.showText("3s");
cb.setTextMatrix(c + 4 * d, 40);
cb.showText("4s");
cb.setTextMatrix(c + 5 * d, 40);
cb.showText("5s");
cb.setTextMatrix(c + 6 * d, 40);
cb.showText("6s");
cb.setTextMatrix(c + 7 * d, 40);
cb.showText("7s");
cb.setTextMatrix(c + 8 * d, 40);
cb.showText("8s");
cb.setTextMatrix(c + 9 * d, 40);
cb.showText("9s");
// 设置其他文字
cb.setFontAndSize(bf, 20);
cb.setTextMatrix(250, 570);
cb.showText("十二导联同步心电图检查报告");
cb.setFontAndSize(bf, 12);
cb.setTextMatrix(50, 20);
cb.showText("*诊断结果只对本次检查有效");
cb.setTextMatrix(550, 20);
cb.showText("报告编号:2002017040109086");
cb.setFontAndSize(bf, 15);
cb.setTextMatrix(710, 480);
cb.showText("心电图信息:");
cb.setFontAndSize(bf, 13);
cb.setTextMatrix(700, 450);
cb.showText("HR: 67 bpm");
cb.setTextMatrix(700, 425);
cb.showText("R-R: 877 ms");
cb.setTextMatrix(700, 400);
cb.showText("P-R: 227 ms");
cb.setTextMatrix(700, 375);
cb.showText("QT: 411 ms");
cb.setTextMatrix(700, 350);
cb.showText("QTc: 439 ms");
cb.setTextMatrix(700, 325);
cb.showText("P/QRS/T电轴: 57'89'43");
cb.setTextMatrix(700, 300);
cb.showText("RV5/SV1:0.74mV 0.77mV");
cb.setTextMatrix(700, 275);
cb.showText("R+S: 1.5l");
cb.endText();
//设置坐标系
// 设置画线的颜色
cb.setColorStroke(BaseColor.PINK);
// 绘制起点坐标
cb.moveTo(50, 50);
cb.lineTo(50, 554);
// 绘制终点坐标
cb.moveTo(50, 50);
cb.lineTo(694, 50);
// 绘制竖线
float x = 50, y = 50;
for (int i = 0; i < 46; i++) {
x = (float) (x + 14);
cb.moveTo(x, y);
cb.lineTo(x, 554);
}
// 绘制横线
x = 50;
for (int i = 0; i < 36; i++) {
y = (float) (y + 14);
cb.moveTo(x, y);
cb.lineTo(694, y);
}
// 确认直线的绘制
cb.stroke();
document.newPage();
// 关闭文档
document.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
生成pdf如图所示: