文章目录
- 前言
- 一、itextpdf是什么?
- 二、快速开始
- 1.废话不多说,效果先上一波
- 2.依赖引入
- 3.模版加载
- 3.1模版加载工具类PDF
- 4.PDF操作工具类方法
- 5.完整代码
- 5.1前端代码-Vue
- 5.2控制层代码
- 5.3业务层代码
- 5.4PDF工具类完整代码
- 总结
前言
在java开发的过程中会遇到太多太多文档pdf导出,excle导出等业务场景,时隔三个月或半年来一次每一次遇到这样的业务场景对我都是非常痛苦的过程,本文旨在记录工具类使用方法和技术分享。
一、itextpdf是什么?
itextpdf是一个开源的Java库,用于创建和操作PDF文档。使用itextpdf,您可以创建新的PDF文档或修改现有文档,添加文本和图像,创建表单等等。该库提供了广泛的功能,并经常用于企业级应用程序中根据用户输入动态生成复杂的PDF文档。它具有各种许可证选项,具体取决于您的用例,范围从AGPL到商业许可证。
itextpdf库提供了大量的方法和功能,以下是一些常用的方法:
- 创建PDF文档和页面:使用PdfWriter和Document对象可以创建一个新的PDF文档并添加页面。
- 添加内容到PDF文档:使用Paragraph、Phrase和Chunk对象可以向PDF文档中添加文本内容。同时,也可以添加图片、表格等其它类型的内容。
- 创建表格:使用PdfPTable对象可以创建表格,并向其中添加行和单元格。
- 设置页眉页脚:使用HeaderFooter对象可以设置PDF文档的页眉和页脚,以及页码等信息。
- 创建书签:使用PdfOutline对象可以创建PDF文档的书签,方便用户在浏览器中导航。
- 导出PDF文档:使用PdfWriter对象可以将PDF文档导出为文件或流。
二、快速开始
1.废话不多说,效果先上一波
下列数据均为测试虚拟数据,不具有任何真实性
2.依赖引入
com.itextpdf itextpdf 5.5.13 com.itextpdf itext-asian 5.2.0 com.itextpdf.tool xmlworker 5.5.11
3.模版加载
rescources-templates-创建一个pdf模版,如果没有定制内容,直接为空白即可
3.1模版加载工具类PDF
/** * 字体对象 */ public static BaseFont bfChinese; /** * 定义全局的字体静态变量 */ public static Font titlefont; public static Font titleSecondFont; public static Font headfont; public static Font secondHeadfont; public static Font textfont; public static Font secondTitleFont; public static Font itemFont; public static Font paragraphFont; public static Font tableCellFont; public static Font accessoryFont; /** * 间距 */ public static String shortSpacing = " "; public static String mediumSpacing = " "; public static String longSpacing = " "; public static String maxLongSpacing = " "; public static String shortSlash = " / "; public static String mediumSlash = " / "; public static String longSlash = " / "; public static String maxLongSlash = " / "; /** * 模版 */ public static String ht; public static String scan; public static String htScan; public static String over; public static String htSign; public static String signatureHt; public static File htFile; public static File overFile; public static File htSignFile; /** * 初始化字体及勾选、未勾选图标 */ static { try { // 不同字体(这里定义为同一种字体:包含不同字号、不同style) bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); titlefont = new Font(bfChinese, 16, Font.NORMAL); titleSecondFont = new Font(bfChinese, 14, Font.NORMAL); headfont = new Font(bfChinese, 14, Font.BOLD); secondHeadfont = new Font(bfChinese, 15, Font.BOLD); textfont = new Font(bfChinese, 16, Font.NORMAL); secondTitleFont = new Font(bfChinese,18,Font.BOLD); itemFont = new Font(bfChinese,16,Font.BOLD); paragraphFont = new Font(bfChinese,11,Font.NORMAL); accessoryFont = new Font(bfChinese,13,Font.NORMAL); tableCellFont = new Font(bfChinese,9,Font.NORMAL); /** * 判断当前环境 */ String osName = System.getProperty("os.name").toLowerCase(); if (osName.contains("win")||osName.contains("mac")) { ht = getResourceBasePath() + "\templates\ht.pdf"; htFile = new File(ht); } else { //todo: linux或unbunt ht = "/mnt/jar/spring-cloud/ht.pdf"; htFile = new File(ht); } } catch (Exception e) { e.printStackTrace(); } } /** * 获取项目根路径 * * @return */ public static String getResourceBasePath() { // 获取根目录 File path = null; try { path = new File(ResourceUtils.getURL("classpath:").getPath()); } catch (FileNotFoundException e) { } if (path == null || !path.exists()) { path = new File(""); } //todo: 就获取第三步中模版所在的位置,请看上列中 “3.模版加载里的内容” File file = new File(path.getAbsolutePath() + "\templates"); if (!file.exists()){ return path.getAbsolutePath().replace("xx","xx"); } return path.getAbsolutePath(); }
4.PDF操作工具类方法
public static PdfPTable createTable() throws DocumentException { PdfPTable table = new PdfPTable(2); // 设置总宽度 table.setTotalWidth(490); table.setLockedWidth(true); // 设置每一列宽度 table.setTotalWidth(new float[] {240,240}); table.setLockedWidth(true); return table; } public PdfPTable createTable(int numColumns) { PdfPTable table = new PdfPTable(numColumns); // 设置总宽度 table.setTotalWidth(490); table.setLockedWidth(true); return table; } public static PdfPTable createTitleTable(int numColumns,Font font, String ... titles) { PdfPTable table = new PdfPTable(numColumns); // 设置总宽度 table.setTotalWidth(490); table.setLockedWidth(true); for (String title : titles) { addTitleCell(table,title,1,font); } return table; } /** * 添加居中 title * @param document * @param text * @throws DocumentException * todo: 字体大小16px */ public static void addTitle(Document document, String text, int alignment) throws DocumentException { Paragraph paragraph = new Paragraph(text, new Font(bfChinese, 16, Font.NORMAL)); paragraph.setAlignment(alignment); //设置文字居中 0靠左 1,居中 2,靠右 paragraph.setIndentationLeft(0); //设置左缩进 paragraph.setIndentationRight(0); //设置右缩进 paragraph.setFirstLineIndent(0); //设置首行缩进 paragraph.setLeading(10f); //行间距 paragraph.setSpacingBefore(10f); //设置段落上空白 paragraph.setSpacingAfter(10f); //设置段落下空白 document.add(paragraph); } /** * 添加一级标题 * @param document * @param text * @throws DocumentException */ public static void addFirstTitle(Document document,String text,int alignment) throws DocumentException { Paragraph paragraph = new Paragraph(text, new Font(bfChinese, 14, Font.NORMAL)); paragraph.setAlignment(alignment); //设置文字居中 0靠左 1,居中 2,靠右 paragraph.setIndentationLeft(0); //设置左缩进 paragraph.setIndentationRight(0); //设置右缩进 paragraph.setFirstLineIndent(0); //设置首行缩进 paragraph.setLeading(7f); //行间距 paragraph.setSpacingBefore(20f); //设置段落上空白 paragraph.setSpacingAfter(7f); //设置段落下空白 document.add(paragraph); } /** * 添加二级标题 * @param document * @param text * @throws DocumentException */ public static void addSecondTitle(Document document,String text,Font font,int alignment) throws DocumentException { Paragraph paragraph = new Paragraph(text, new Font(bfChinese, 12, Font.NORMAL)); paragraph.setAlignment(alignment); //设置文字居中 0靠左 1,居中 2,靠右 paragraph.setIndentationLeft(9); //设置左缩进 paragraph.setIndentationRight(0); //设置右缩进 paragraph.setFirstLineIndent(9); //设置首行缩进 paragraph.setLeading(15f); //行间距 paragraph.setSpacingBefore(10f); //设置段落上空白 paragraph.setSpacingAfter(5f); //设置段落下空白 document.add(paragraph); } /** * 添加分割线 * @param document * @throws DocumentException */ public static void addLine(Document document) throws DocumentException { LineSeparator ls = new LineSeparator(); ls.setLineWidth(1); ls.setLineColor(new BaseColor(179,180,164)); Chunk chunk = new Chunk(ls); document.add(chunk); } public static void addNoBorderCell(PdfPTable table,String text) { PdfPCell cell = new PdfPCell(new Paragraph(text,paragraphFont)); cell.setBorderWidth(0); cell.setLeading(24,0); //行间距 table.addCell(cell); } public static void addTitleCell(PdfPTable table,String text,float borderWidth,Font font) { Paragraph paragraph = new Paragraph(text, font); paragraph.setAlignment(1); paragraph.setLeading(5f); PdfPCell cell = new PdfPCell(paragraph); cell.setBorderWidth(borderWidth); cell.setBackgroundColor(new BaseColor(245,247,251)); cell.setHorizontalAlignment(1); cell.setVerticalAlignment(1); table.addCell(cell); } public static void addCell(PdfPTable table,String text,float borderWidth,Font font) { if (StringUtils.isBlank(text)) { text = " "; } Paragraph paragraph = new Paragraph(text, font); paragraph.setAlignment(1); PdfPCell cell = new PdfPCell(paragraph); //水平居中和垂直居中 cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setUseAscender(true); table.addCell(cell); } public static void addCell(PdfPTable table, BigDecimal amount, float borderWidth, Font font) { Paragraph paragraph = null; if (Objects.isNull(amount)) { paragraph = new Paragraph(" ",font); } else { paragraph = new Paragraph(amount.toString(),font); } paragraph.setAlignment(1); PdfPCell cell = new PdfPCell(paragraph); cell.setBorderWidth(borderWidth); //水平居中和垂直居中 cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setUseAscender(true); table.addCell(cell); } /** * 添加二级标题 * @param document * @param text * @throws DocumentException */ public void addSecondTitle(Document document,String text,int alignment,float spacingBefore,float spacingAfter) throws DocumentException { Paragraph paragraph10 = new Paragraph(text, secondTitleFont); paragraph10.setAlignment(alignment); //设置文字居中 0靠左 1,居中 2,靠右 paragraph10.setIndentationLeft(12); //设置左缩进 paragraph10.setIndentationRight(12); //设置右缩进 paragraph10.setFirstLineIndent(32); //设置首行缩进 paragraph10.setLeading(30f); //行间距 paragraph10.setSpacingBefore(1f); //设置段落上空白 paragraph10.setSpacingAfter(15f); //设置段落下空白 document.add(paragraph10); } /** * 添加子项标题 * @param document * @param text * @throws DocumentException */ public void addItemTitle(Document document,String text) throws DocumentException { Paragraph paragraph = new Paragraph(text, itemFont); paragraph.setAlignment(0); //设置文字居中 0靠左 1,居中 2,靠右 paragraph.setIndentationLeft(12); //设置左缩进 paragraph.setIndentationRight(12); //设置右缩进 paragraph.setFirstLineIndent(32); //设置首行缩进 paragraph.setLeading(30f); //行间距 paragraph.setSpacingBefore(1f); //设置段落上空白 paragraph.setSpacingAfter(1f); //设置段落下空白 document.add(paragraph); } /** * 添加段落 自动换行 */ public void addTextParagraph(Document document,String text) throws DocumentException { Paragraph paragraph = new Paragraph(text,paragraphFont); paragraph.setAlignment(0); //设置文字居中 0靠左 1,居中 2,靠右 paragraph.setIndentationLeft(12); //设置左缩进 paragraph.setIndentationRight(12); //设置右缩进 paragraph.setFirstLineIndent(32); //设置首行缩进 paragraph.setLeading(30f); //行间距 document.add(paragraph); } /** * 添加段落 自动换行 */ public Paragraph createTextParagraph() throws DocumentException { Paragraph paragraph = new Paragraph("",paragraphFont); paragraph.setAlignment(0); //设置文字居中 0靠左 1,居中 2,靠右 paragraph.setIndentationLeft(12); //设置左缩进 paragraph.setIndentationRight(12); //设置右缩进 paragraph.setFirstLineIndent(32); //设置首行缩进 paragraph.setLeading(30f); //行间距 return paragraph; } /** * 添加段落 自动换行 */ public Paragraph createTextParagraph(String text) throws DocumentException { Paragraph paragraph = new Paragraph(text,paragraphFont); paragraph.setAlignment(0); //设置文字居中 0靠左 1,居中 2,靠右 paragraph.setIndentationLeft(12); //设置左缩进 paragraph.setIndentationRight(12); //设置右缩进 paragraph.setFirstLineIndent(32); //设置首行缩进 paragraph.setLeading(30f); //行间距 return paragraph; } /** * 创建段落 自动换行 */ public Paragraph createTextParagraph(String text,int alignment) { Paragraph paragraph = new Paragraph(text,paragraphFont); paragraph.setAlignment(alignment); //设置文字居中 0靠左 1,居中 2,靠右 paragraph.setIndentationLeft(12); //设置左缩进 paragraph.setIndentationRight(12); //设置右缩进 paragraph.setFirstLineIndent(32); //设置首行缩进 paragraph.setLeading(30f); //行间距 return paragraph; } public void appendParagraph(Document document,Paragraph paragraph,String text) throws DocumentException { paragraph.add(text); document.add(paragraph); } /** * 添加下划线 */ public Paragraph addUnderLine(Paragraph paragraph,String text){ if (StringUtils.isBlank(text)){ text = shortSpacing; } else { text = StringUtils.join(" ",text," "); } Chunk sigUnderline = new Chunk(text); sigUnderline.setUnderline(0.1f, -2f); paragraph.add(sigUnderline); return paragraph; } /** * 添加下划线 */ public void addUnderLine(Document document,Paragraph paragraph,String text) throws DocumentException { if (StringUtils.isBlank(text)){ text = shortSpacing; } Chunk sigUnderline = new Chunk(StringUtils.join(" ",text," ")); sigUnderline.setUnderline(0.1f, -2f); paragraph.add(sigUnderline); document.add(paragraph); } /**objStmMark = null * 追加签名 */ public File mergePdf(String [] files,String savePath){ PdfReader pdfReader = null; Document document = null; try { //创建一个与a.pdf相同纸张大小的document pdfReader = new PdfReader(files[0]); document = new Document(pdfReader.getPageSize(1)); PdfCopy copy = new PdfCopy(document, new FileOutputStream(savePath)); document.open(); for (int i = 0; i