private String printSMTZJLD() {
//所有数据
List<Map<String, String>> maps = new ArrayList<>();
//一行数据
Map<String, String> map = new HashMap<>();
//表头集合
ArrayList<String> biaotou = new ArrayList<>();
//拼装表头数据
biaotou.add("表头1");
biaotou.add("表头2");
biaotou.add("表头3");
biaotou.add("表头4");
//拼装一行数据
map.put("表头1","数据1");
map.put("表头2","数据2");
map.put("表头3","数据3");
map.put("表头4","数据4");
//拼装表数据
maps.add(map);
//生成pdf
String pdfPath = null;
FileOutputStream outputStream = null;
PdfWriter writer = null;
Document document = null;
try {
//设置纸张为A4,上下左右页边距分别为20,20,20,20
document = new Document(PageSize.A4,20,20,20,20);
//设置纸张默认为纵版,默认为纵版;PageSize.A4.rotate()为横版纸张
document.setPageSize(PageSize.A4);
//文件名称
String fileName ="smtzjld" + aimsForm.getPatientId() + "pd.pdf";
//存储路径
String url="D:\\/pdf";
pdfPath = url + fileName;
outputStream = new FileOutputStream(pdfPath);
//建立一个书写器与Document对象关联,通过书写器将文档写入磁盘
writer = PdfWriter.getInstance(document, outputStream);
writer.setViewerPreferences(writer.HideToolbar);
document.open();
//获取系统字体
BaseFont bfChi = this.getChineseFont();
//字体大小设置
Font font1 = new Font(bfChi, 8);
Font font2 = new Font(bfChi, 16);
//创建一个table开始放数据
PdfPTable table = new PdfPTable(biaotou.size());
//设置其宽度为百分百
table.setWidthPercentage(100);
//设定表头为前5行
table.setHeaderRows(2);
//设置第一行为文件标题
PdfPCell cell = new PdfPCell(new Paragraph("标题", font2));
//合并所有表头列为一个单元格,可以根据自己的需求合并相应列( 合并上下两个单元格的方法为setRowspan)
cell.setColspan(biaotou.size());
tableHead1(table,cell);
//自动生成的表头信息放入文件
for (String s : biaotou) {
tableHead01(table, new PdfPCell(new Paragraph(s, font1)));
}
//将数据放入表格
if (!maps.isEmpty()){
for (Map<String, String> stringStringMap : maps) {
for (String s : biaotou) {
tableHead01(table, new PdfPCell(new Paragraph(stringStringMap.get(s), font1)));
}
}
}
//放入空白行
document.add(table);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (document != null) {
try {
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return pdfPath;
}
public BaseFont getChineseFont() throws DocumentException, IOException {
BaseFont bfChi = null;
if (System.getProperty("os.name").contains("Mac")) {
bfChi = BaseFont.createFont("//Library//Fonts//simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
} else {
try {
bfChi = BaseFont.createFont("c://windows//fonts//simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
} catch (Exception e) {
// simsunb.ttf
bfChi = BaseFont.createFont("c://windows//fonts//simsunb.ttf,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
}
}
return bfChi;
}
//行高15
public void tableHead01(PdfPTable table, PdfPCell cell) {
// cell.setBorder(0);
cell.setPaddingBottom(1);
cell.setPaddingTop(1);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setFixedHeight(15);
table.addCell(cell);
}
//表格头部样式
public void tableHead(PdfPTable table, PdfPCell cell) {
// cell.setBorder(0);
cell.setPaddingBottom(1);
cell.setPaddingTop(1);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setFixedHeight(15);
table.addCell(cell);
}
//表格头部样式
public void tableHead1(PdfPTable table, PdfPCell cell) {
cell.setBorder(0);
cell.setPaddingBottom(3);
cell.setPaddingTop(3);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setFixedHeight(30);
table.addCell(cell);
}
注意:table放入数据可以理解为每个一个表格行从左往右放数据,当这一行数据放满之后才会跳到下一行再放数据。合并单元格之后需要格外注意,避免数据窜行情况的发生,对于没有值的数据在生成数据集合的时候最好用空字符串占位。
若需要放入空包行将整个纸张全部占满可以进行如下操作,
例:总行数为18行,除了放入的数据外其他的为空白行
//添加空白行
for (int i = maps.size()-1; i < 18; i++) {
for (String s : biaotou) {
tableHead01(table, new PdfPCell(new Paragraph("", font1)));
}
}