前言
使用iText的JAR包如下
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.1</version>
</dependency>
<!-- 输出中文所需-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
实现代码
public static void main(String[] args) {
String[] headers = {"列一", "列二", "列三", "列四", "列五", "列六"};
String list = "[{\"balance\":\"42402\",\"changeDate\":\"20211026111641\",\"dgst\":\"SSTR\",\"outMoney\":\"0\",\"bankSeqNo\":\"00000000000641199893\",\"inMoney\":\"11539\"},{\"balance\":\"30863\",\"changeDate\":\"20211005133736\",\"dgst\":\"SALE\",\"outMoney\":\"7500\",\"bankSeqNo\":\"00000000000635138192\",\"inMoney\":\"0\"},{\"balance\":\"38363\",\"changeDate\":\"20210926113940\",\"dgst\":\"SSTR\",\"outMoney\":\"0\",\"bankSeqNo\":\"00000000000632420138\",\"inMoney\":\"11539\"},{\"balance\":\"26824\",\"changeDate\":\"20210925111144\",\"dgst\":\"SALE\",\"outMoney\":\"11150\",\"bankSeqNo\":\"00000000000632752221\",\"inMoney\":\"0\"},{\"balance\":\"37974\",\"changeDate\":\"20210922083109\",\"dgst\":\"SALE\",\"outMoney\":\"4860\",\"bankSeqNo\":\"00000000000631851353\",\"inMoney\":\"0\"},{\"balance\":\"42834\",\"changeDate\":\"20210919153251\",\"dgst\":\"SALE\",\"outMoney\":\"7910\",\"bankSeqNo\":\"00000000000631503750\",\"inMoney\":\"0\"},{\"balance\":\"50744\",\"changeDate\":\"20210917080014\",\"dgst\":\"SALE\",\"outMoney\":\"5870\",\"bankSeqNo\":\"00000000000630377865\",\"inMoney\":\"0\"},{\"balance\":\"56614\",\"changeDate\":\"20210915153430\",\"dgst\":\"SALE\",\"outMoney\":\"8560\",\"bankSeqNo\":\"00000000000630120441\",\"inMoney\":\"0\"},{\"balance\":\"65174\",\"changeDate\":\"20210830103432\",\"dgst\":\"SALE\",\"outMoney\":\"6200\",\"bankSeqNo\":\"00000000000625635279\",\"inMoney\":\"0\"},{\"balance\":\"71374\",\"changeDate\":\"20210825124135\",\"dgst\":\"SSTR\",\"outMoney\":\"0\",\"bankSeqNo\":\"00000000000624479550\",\"inMoney\":\"11539\"}]";
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
BASE64Encoder base64 = new BASE64Encoder();
Document document = null;
PdfWriter writer = null;
try {
List<Map<String, Object>> mapList = (List) JSON.parseArray(StringEscapeUtils.unescapeHtml4(list));
//创建纵向文件
document = PdfUtils.createPortraitDocument();
//建立一个书写器(内存流方式)
writer = PdfWriter.getInstance(document, byteArrayOutputStream);
//打开文件
document.open();
//添加字体样式
Font bigFont = PdfUtils.createFont(14, Font.NORMAL, BaseColor.BLACK);
Font blueFont = PdfUtils.createFont(10, Font.NORMAL, BaseColor.BLACK);
//段落内容
Paragraph title = PdfUtils.createParagraph("测试pdf", bigFont);
//文字居中显示
title.setAlignment(Element.ALIGN_CENTER);
//添加段落内容
document.add(title);
//创建表格列数
PdfPTable table = PdfUtils.createPdfPTable(headers.length);
//创建表头
PdfUtils.createHeader(document, table, headers, bigFont);
//填充数据
PdfUtils.dataProcessing(document, table, mapList, blueFont);
} catch (DocumentException e) {
} finally {
if (document != null) {
//关闭文档
document.close();
}
if (writer != null) {
//关闭书写器
writer.close();
}
}
System.out.println(base64.encode(byteArrayOutputStream.toByteArray()));
}
实现效果
工具类
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* @version V1.0
* @Description:生成PDF文件
* @Author: Mr.LJie
* @Date: 2021/11/30 14:17
*/
public class PdfUtils {
/**
* 固定高
*/
public static float fixedHeight = 27f;
/**
* 跨行
*/
public static int rowSpan = 0;
/**
* 跨列
*/
public static int colSpan = 0;
/**
* 左边距
*/
public static float marginLeft = 20;
/**
* 右边距
*/
public static float marginRight = 20;
/**
* 上边距
*/
public static float marginTop = 20;
/**
* 下边距
*/
public static float marginBottom = 20;
/***
* @Description :创建A4纵向文件
* @param
* @return com.itextpdf.text.Document
* @Author Mr.Jie
* @Date 2021/11/30 14:25
*/
public static Document createPortraitDocument() {
//生成pdf
Document document = new Document();
// 页面大小
Rectangle rectangle = new Rectangle(PageSize.A4);
// 页面背景颜色
rectangle.setBackgroundColor(BaseColor.WHITE);
document.setPageSize(rectangle);
// 页边距 左,右,上,下
document.setMargins(marginLeft, marginRight, marginTop, marginBottom);
return document;
}
/***
* @Description :创建A4横向文件
* @param
* @return com.itextpdf.text.Document
* @Author Mr.Jie
* @Date 2021/11/30 15:42
*/
public static Document createTransverseDocument() {
//生成pdf
Document document = new Document();
// 页面大小
Rectangle rectangle = new Rectangle(new RectangleReadOnly(842F, 595F));
// 页面背景颜色
rectangle.setBackgroundColor(BaseColor.WHITE);
document.setPageSize(rectangle);
// 页边距 左,右,上,下
document.setMargins(marginLeft, marginRight, marginTop, marginBottom);
return document;
}
/**
* @param text 段落内容
* @param font 字体设置
* @return com.itextpdf.text.Paragraph
* @Description :创建段落内容
* @Author Mr.Jie
* @Date 2021/11/30 14:59
*/
public static Paragraph createParagraph(String text, Font font) {
Paragraph elements = new Paragraph(text, font);
elements.setSpacingBefore(5);
elements.setSpacingAfter(5);
return elements;
}
/***
* @Description :创建字体样式
* @param fontNumber 字号
* @param fontSize 字体大小
* @param fontColor 字体颜色
* @return com.itextpdf.text.Font
* @Author Mr.Jie
* @Date 2021/11/30 14:57
*/
public static Font createFont(int fontNumber, int fontSize, BaseColor fontColor) {
//中文字体 ----不然中文会乱码
BaseFont bf = null;
try {
bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
return new Font(bf, fontNumber, fontSize, fontColor);
} catch (Exception e) {
e.printStackTrace();
}
return new Font(bf, Font.DEFAULTSIZE, Font.NORMAL, BaseColor.BLACK);
}
/***
* @Description :隐藏表格边框线
* @param cell 单元格
* @return void
* @Author Mr.Jie
* @Date 2021/11/30 14:57
*/
public static void disableBorderSide(PdfPCell cell) {
if (cell != null) {
cell.disableBorderSide(1);
cell.disableBorderSide(2);
cell.disableBorderSide(4);
cell.disableBorderSide(8);
}
}
/***
* @Description :创建居中得单元格
* @param
* @return com.itextpdf.text.pdf.PdfPCell
* @Author Mr.Jie
* @Date 2021/11/30 14:57
*/
public static PdfPCell createCenterPdfPCell() {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setFixedHeight(fixedHeight);
return cell;
}
/***
* @Description :创建指定文字得单元格
* @param text 文字内容
* @param rowSpan 跨行合并单元格
* @param colSpan 跨列合并单元格
* @param font 字体
* @return com.itextpdf.text.pdf.PdfPCell
* @Author Mr.Jie
* @Date 2021/11/30 14:50
*/
public static PdfPCell createCenterPdfPCell(String text, int rowSpan, int colSpan, Font font) {
PdfPCell cell = new PdfPCell(new Paragraph(text, font));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setFixedHeight(fixedHeight);
cell.setRowspan(rowSpan);
cell.setColspan(colSpan);
return cell;
}
/***
* @Description :表格列数
* @param len
* @return com.itextpdf.text.pdf.PdfPTable
* @Author Mr.Jie
* @Date 2021/11/30 14:52
*/
public static PdfPTable createPdfPTable(int len) {
PdfPTable pdfPTable = new PdfPTable(len);
//宽度100%填充
pdfPTable.setWidthPercentage(100);
return pdfPTable;
}
/***
* @Description :创建表头
* @param document
* @param headers
* @param font
* @return void
* @Author Mr.Jie
* @Date 2021/11/30 15:27
*/
public static void createHeader(Document document, PdfPTable table, String[] headers, Font font) throws DocumentException {
for (int i = 0; i < headers.length; i++) {
//默认不跨行、跨列
PdfPCell cell = PdfUtils.createCenterPdfPCell(headers[i], rowSpan, rowSpan, font);
//居中显示
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
}
document.add(table);
}
/***
* @Description :数据处理
* @param document
* @param table
* @param list
* @param font
* @return void
* @Author Mr.Jie
* @Date 2021/11/30 15:32
*/
public static void dataProcessing(Document document, PdfPTable table, List<Map<String, Object>> list, Font font) throws DocumentException {
Iterator<Map<String, Object>> iterator = list.iterator();
while (iterator.hasNext()) {
//获取对象
Map<String, Object> map = iterator.next();
//Map 获取key & value
for (Map.Entry<String, Object> entry : map.entrySet()) {
Object value = entry.getValue();
//默认不跨行、跨列
PdfPCell cell = PdfUtils.createCenterPdfPCell(value.toString(), rowSpan, rowSpan, font);
table.addCell(cell);
}
document.add(table);
}
}
}
版权声明:本文为LiuQjie原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。