Itex 实现java 后台的pdf导出 Maven spring boot 项目

时间:2025-04-09 11:29:12
package webmaster.webmaster.pdf; import java.awt.Color; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import com.itextpdf.text.BaseColor; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPRow; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.StringUtils; import io.netty.util.internal.StringUtil; /** * 定义中文可以在pdf 内显示出来 * * @author Administrator * */ public class PDFParagraph extends Paragraph { private static final long serialVersionUID = 1L; /** * 规范的黑色字体 * * @param content */ public PDFParagraph(String content) { super(content, getChineseFont()); } /** * 自己定义 字体的规范 * * @param content * @param fontSize 字体大小 * @param style 规范 * @param color */ public PDFParagraph(String content, Integer fontSize, Integer style, BaseColor color) { super(content, getChineseFont(fontSize, style, color)); } /** * 自己定义12px 的规范字体并且自己定义颜色 * * @param content * @param color */ public PDFParagraph(String content, BaseColor color) { super(content, getChineseFont(12, Font.NORMAL, color)); } private static final Font getChineseFont() { Font FontChinese = null; try { BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); FontChinese = new Font(bfChinese, 12, Font.NORMAL); } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } return FontChinese; } private static final Font getChineseFont(Integer fontSize, Integer style, BaseColor color) { Font FontChinese = null; try { BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); FontChinese = new Font(bfChinese, fontSize, style, color); } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } return FontChinese; } } /** * 快速定义字体 * * @author Administrator * */ class MyFont { /** * 默认的字体 size 12 常规字体,黑色 * * @param content * @return */ public static PDFParagraph speedFont(String content) { return new PDFParagraph(content); } /** * 自己定义一段文字 * * @param content 文字内容 * @param fontSize 文字大小 * @param style 文字的央视 常规 * @param color 文字的颜色 * @return */ public static PDFParagraph speedFont(String content, Integer fontSize, Integer style, BaseColor color) { return new PDFParagraph(content, fontSize, style, color); } /** * 设置一段常规 12 大小的字体,可以另外设置颜色 * * @param content * @param color * @return */ public static PDFParagraph speedFont(String content, BaseColor color) { return new PDFParagraph(content, color); } /** * 自定义一个title 类似于标题 * * @param content * @param fontSize * @param style * @param color * @param element Element.ALIGN_CENTER 水平居中的意思 * @return */ public static PDFParagraph centerTitle(String content, Integer fontSize, Integer style, BaseColor color, Integer element) { PDFParagraph font = new PDFParagraph(content, fontSize, style, color); font.setAlignment(element); return font; } } /** * 快速定义一个table * * @author Administrator * */ class MyTable { /** * 解析list map 数据导出 * * @param columnWidth 列宽度,可设置多个 * @param list 要解析的数据 * @return * @throws DocumentException */ public static PdfPTable initTable(float[] columnWidth, List<Map<String, Object>> list) throws DocumentException { PdfPTable table = new PdfPTable(columnWidth.length); // 设置列宽 table.setWidths(columnWidth); // 第一行是列名 table.setHeaderRows(1); // 宽度填充 table.getDefaultCell().setHorizontalAlignment(1); // 设置表哥的浮动宽度, 让文字保持居中 //(100.0F); // 储存列标题 Map<String, Integer> title = new HashMap<String, Integer>(); // 当前创建的表哥的row 获取出来 List<PdfPRow> rows = table.getRows(); // 设置单元格背景颜色为灰色 // cells0[0].setBackgroundColor(BaseColor.LIGHT_GRAY); for (int i = 0; i < list.size(); i++) { Map<String, Object> map = list.get(i); if (i == 0) { int j = 0; // 当前是第一列,解析出来所有的列 PdfPCell[] cell = new PdfPCell[list.get(i).size()]; for (String key : map.keySet()) { // 把所有的列都存起来 title.put(key, j); cell[j] = new PdfPCell(MyFont.centerTitle(key, 13, Font.BOLD, BaseColor.BLACK, Element.ALIGN_CENTER)); cell[j].setBackgroundColor(BaseColor.LIGHT_GRAY); cell[j].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中 cell[j].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中 cell[j].setFixedHeight(25f); j++; } rows.add(new PdfPRow(cell)); } // 开始添加 PdfPCell[] cell = new PdfPCell[list.get(i).size()]; for (String key : map.keySet()) { cell[title.get(key)] = new PdfPCell(MyFont.speedFont(map.get(key) == null ? "" : map.get(key).toString())); cell[title.get(key)].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中 cell[title.get(key)].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中 cell[title.get(key)].setFixedHeight(25f); } rows.add(new PdfPRow(cell)); } System.out.println("table 生成完成"); return table; } public static PdfPTable initTable(String topTitle, float[] columnWidth, List<Map<String, Object>> list) throws DocumentException { // 新建一个表格 列数为列宽比例的个数 PdfPTable table = new PdfPTable(columnWidth.length); // 宽度填充 table.getDefaultCell().setHorizontalAlignment(1); // 设置表哥的浮动宽度, 让文字保持居中 //(100.0F); // 获取到表格的行准备操作 List<PdfPRow> rows = table.getRows(); // 设置表格的列宽 table.setWidths(columnWidth); // 定义一个列 然后跨列 添加到table 的表头 PdfPCell[] celltop = new PdfPCell[2]; celltop[0] = new PdfPCell(MyFont.centerTitle(topTitle, 13, Font.BOLD, BaseColor.BLACK, Element.ALIGN_CENTER)); celltop[0].setBackgroundColor(BaseColor.LIGHT_GRAY); celltop[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中 celltop[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中 celltop[0].setFixedHeight(25f); // 设置跨列 celltop[0].setColspan(2); rows.add(new PdfPRow(celltop)); for(Map<String,Object> map : list) { for(String key : map.keySet()) { PdfPCell[] cells = new PdfPCell[2]; cells[0] = new PdfPCell(MyFont.centerTitle(key, 13, Font.BOLD, BaseColor.BLACK, Element.ALIGN_CENTER)); // 设置当前单元格的背景为灰色 cells[0].setBackgroundColor(BaseColor.LIGHT_GRAY); cells[1] = new PdfPCell(MyFont.speedFont(map.get(key)==null?"":map.get(key).toString())); cells[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中 cells[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中 cells[0].setFixedHeight(25f); cells[1].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中 cells[1].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中 cells[1].setFixedHeight(25f); rows.add(new PdfPRow(cells)); } } return table; } }