一、添加依赖
<dependency>
<groupId>com.lowagiegroupId>
<artifactId>itextartifactId>
<version>2.1.5version>
dependency>
二、实现示例代码
- 如下代码中使用了 【SIMYOU.TTF】幼圆字体,根据需要可以自行下载
package com.lyp;
import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
public class downLoadPDF {
public static void main(String[] args) {
String fileName = "test.pdf";
String path = "D:pdf/";
try {
Document document = new Document();
BaseFont bfChinese = BaseFont.createFont("/font/SIMYOU.TTF",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font dateTitle = new Font(bfChinese,7,Font.NORMAL);
Font title = new Font(bfChinese,12,Font.BOLD);
Font textFont = new Font(bfChinese,7,Font.NORMAL);
PdfWriter.getInstance(document, new FileOutputStream(path+fileName));
document.open();
PdfPTable tableTitle = new PdfPTable(3);
PdfPTable codeTitle = new PdfPTable(1);
PdfPTable table = new PdfPTable(7);
float[] widthsHeaderTitle = {1f,1f,1f};
float[] widthsCodeTitle = {1f};
float[] widthsHeader = {1f,1f,1f,1f,1f,1f,2f};
float lineHeight = (float)20.0;
tableTitle.setWidths(widthsHeaderTitle);
codeTitle.setWidths(widthsCodeTitle);
table.setWidths(widthsHeader);
tableTitle.setWidthPercentage(100);
codeTitle.setWidthPercentage(100);
table.setWidthPercentage(100);
int colSpan = 1;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
createTableCellLeft("导出人:行如火", textFont, tableTitle, lineHeight, colSpan);
createTableCellCenter("确认出差记录表", title, tableTitle, lineHeight, colSpan);
createTableCellRight(formatter.format(date), dateTitle, tableTitle, lineHeight, colSpan);
document.add(tableTitle);
createTableCellRight("功能码: XXXXXXXXX",textFont, codeTitle, lineHeight, colSpan);
document.add(codeTitle);
document.add(new Paragraph("\n"));
String[] array = {"报表生成日期 ","状态 ", "经办人员 ", "提交时间 ","复核人员 ", "复核时间 ", "情况补充 "};
for (String s : array) {
createTableCell(s, textFont, table, lineHeight, colSpan);
}
List<Map<String, Object>> dataResult = getResult();
for (Map<String, Object> map : dataResult) {
String dataTime = map.get("dateTime") != null?map.get("dateTime").toString():"0";
String status = "0";
if (null != map.get("status")) {
if ("0".equals(map.get("status"))) {
status = "待确认";
}
if ("1".equals(map.get("status"))) {
status = "待复核";
}
if ("2".equals(map.get("status"))) {
status = "已复核";
}
}
String creator = map.get("creator") != null?map.get("creator").toString():"0";
String createTime = map.get("createTime") != null?map.get("createTime").toString():"0";
String updator = map.get("updator") != null?map.get("updator").toString():"0";
String updateTime = map.get("updateTime") != null?map.get("updateTime").toString():"0";
String description = map.get("description") != null?map.get("description").toString():"0";
createTableCell(dataTime, textFont, table, lineHeight, colSpan);
createTableCell(status, textFont, table, lineHeight, colSpan);
createTableCell(creator, textFont, table, lineHeight, colSpan);
createTableCell(createTime, textFont, table, lineHeight, colSpan);
createTableCell(updator, textFont, table, lineHeight, colSpan);
createTableCell(updateTime, textFont, table, lineHeight, colSpan);
createTableCell(description, textFont, table, lineHeight, colSpan);
}
document.add(table);
document.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
File file = new File(fileName);
System.out.println(file);
}
private static void createTableCell(String text, Font font, PdfPTable table, float lineHeight, int colSapn) {
PdfPCell cell;
cell = new PdfPCell(new Paragraph(text,font));
cell.setColspan(colSapn);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight);
table.addCell(cell);
}
private static void createTableCellLeft(String text, Font font, PdfPTable table, float lineHeight ,int colSapn) {
PdfPCell cell;
cell = new PdfPCell(new Paragraph(text,font));
cell.setColspan(colSapn);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
cell.setFixedHeight(lineHeight);
cell.setBorderWidthRight(0);
cell.setBorderWidthBottom(0);
cell.setBorderWidthLeft(0);
cell.setBorderWidthTop(0);
table.addCell(cell);
}
private static void createTableCellCenter(String text, Font font, PdfPTable table, float lineHeight ,int colSapn) {
PdfPCell cell;
cell = new PdfPCell(new Paragraph(text,font));
cell.setColspan(colSapn);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight);
cell.setBorderWidthLeft(0);
cell.setBorderWidthRight(0);
cell.setBorderWidthBottom(0);
cell.setBorderWidthTop(0);
table.addCell(cell);
}
private static void createTableCellRight(String text, Font font, PdfPTable table, float lineHeight , int colSapn) {
PdfPCell cell;
cell = new PdfPCell(new Paragraph(text,font));
cell.setColspan(colSapn);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.setVerticalAlignment(Element.ALIGN_TOP);
cell.setFixedHeight(lineHeight);
cell.setBorderWidthLeft(0);
cell.setBorderWidthBottom(0);
cell.setBorderWidthRight(0);
cell.setBorderWidthTop(0);
table.addCell(cell);
}
private static List<Map<String, Object>> getResult() {
List<Map<String, Object>> result = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Map<String, Object> map = new HashMap<>();
map.put("dateTime", "2022/10/11");
map.put("creator", "行如火");
map.put("createTime", "2022/10/10");
map.put("updator", "疾如风");
map.put("updateTime", "2022/10/11");
if(i == 0) {
map.put("status", "0");
map.put("description", "测试导出PDF");
} else if (i < 4){
map.put("status", "1");
map.put("description", "测试导出PDF"+i);
} else {
map.put("status", "2");
map.put("description", "测试导出PDF"+i);
}
result.add(map);
}
System.out.println(result);
return result;
}
}
三、效果展示
- 对应目录下生成test.pdf 文件
- 生成效果如下所示: