1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > java后端使用itextPDF生成PDF文件

java后端使用itextPDF生成PDF文件

时间:2022-10-09 16:40:46

相关推荐

java后端使用itextPDF生成PDF文件

项目中需要导出生成的报告为PDF,原来的版本是前端截图生成PDF,当时会产生内容因为分页被截断的问题,因此换成使用itext根据前端传回的数据来生成PDF文件。

<!-- /artifact/com.itextpdf/itextpdf --><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13</version></dependency><!-- /artifact/com.itextpdf/itext-asian --><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency>

所需的itextPDF依赖

/how-to/javadoc/itext-2.1.7/allclasses-noframe.html

对应的API文档

先创建相应的工具类

// 定义全局的字体静态变量private static Font titlefont;private static Font headfont;private static Font keyfont;private static Font textfont;// 最大宽度private static int maxWidth = 520;// 静态代码块static {try {// 不同字体(这里定义为同一种字体:包含不同字号、不同style)BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);titlefont = new Font(bfChinese, 16, Font.BOLD);headfont = new Font(bfChinese, 14, Font.BOLD);keyfont = new Font(bfChinese, 10, Font.BOLD);textfont = new Font(bfChinese, 10, Font.NORMAL);} catch (Exception e) {e.printStackTrace();}}/**------------------------创建表格单元格的方法start----------------------------*//*** 创建单元格(指定字体)* @param value* @param font* @return*/public static PdfPCell createCell(String value, Font font) {PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(Element.ALIGN_CENTER);cell.setPhrase(new Phrase(value, font));return cell;}/*** 创建单元格(指定字体、水平..)* @param value* @param font* @param align* @return*/public static PdfPCell createCell(String value, Font font, int align) {PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(align);cell.setPhrase(new Phrase(value, font));return cell;}/*** 创建单元格(指定字体、水平居..、单元格跨x列合并)* @param value* @param font* @param align* @param colspan* @return*/public static PdfPCell createCell(String value, Font font, int align, int colspan) {PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(align);cell.setColspan(colspan);cell.setPhrase(new Phrase(value, font));return cell;}/*** 创建单元格(指定字体、水平居..、单元格跨x列合并、设置单元格内边距)* @param value* @param font* @param align* @param colspan* @param boderFlag* @return*/public static PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag) {PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(align);cell.setColspan(colspan);cell.setPhrase(new Phrase(value, font));cell.setPadding(3.0f);if (!boderFlag) {cell.setBorder(0);cell.setPaddingTop(15.0f);cell.setPaddingBottom(8.0f);} else if (boderFlag) {cell.setBorder(0);cell.setPaddingTop(0.0f);cell.setPaddingBottom(15.0f);}return cell;}/*** 创建单元格(指定字体、水平..、边框宽度:0表示无边框、内边距)* @param value* @param font* @param align* @param borderWidth* @param paddingSize* @param flag* @return*/public static PdfPCell createCell(String value, Font font, int align, float[] borderWidth, float[] paddingSize, boolean flag) {PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(align);cell.setPhrase(new Phrase(value, font));cell.setBorderWidthLeft(borderWidth[0]);cell.setBorderWidthRight(borderWidth[1]);cell.setBorderWidthTop(borderWidth[2]);cell.setBorderWidthBottom(borderWidth[3]);cell.setPaddingTop(paddingSize[0]);cell.setPaddingBottom(paddingSize[1]);if (flag) {cell.setColspan(2);}return cell;}/**------------------------创建表格单元格的方法end----------------------------*//**--------------------------创建表格的方法start------------------- ---------*//*** 创建默认列宽,指定列数、水平(居中、右、左)的表格* @param colNumber* @param align* @return*/public static PdfPTable createTable(int colNumber, int align) {PdfPTable table = new PdfPTable(colNumber);try {table.setTotalWidth(maxWidth);table.setLockedWidth(true);table.setHorizontalAlignment(align);table.getDefaultCell().setBorder(1);} catch (Exception e) {e.printStackTrace();}return table;}/*** 创建指定列宽、列数的表格* @param widths* @return*/public static PdfPTable createTable(float[] widths) {PdfPTable table = new PdfPTable(widths);try {table.setTotalWidth(maxWidth);table.setLockedWidth(true);table.setHorizontalAlignment(Element.ALIGN_CENTER);table.getDefaultCell().setBorder(1);} catch (Exception e) {e.printStackTrace();}return table;}/*** 创建空白的表格* @return*/public static PdfPTable createBlankTable() {PdfPTable table = new PdfPTable(1);table.getDefaultCell().setBorder(0);table.addCell(createCell("", keyfont));table.setSpacingAfter(20.0f);table.setSpacingBefore(20.0f);return table;}/**--------------------------创建表格的方法end------------------- ---------*/

开始创建PDF文件

// 路径String PDFPath = "E:" + File.separator + "分析报告.pdf";Document document = new Document(PageSize.A4);// 建立一个Document对象

创建文件并打开,开始进行操作

file.createNewFile();PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));// 打开文档document.open();document.addTitle(store.getStoreName());// 设置文档标题

创建需要使用的文体

//设置字体 路径导向为本地的字体文件String fontPath = "C:/WINDOWS/Fonts/STSONG.TTF";String blackFontPath = "C:/WINDOWS/Fonts/simhei.ttf";String numberPath = "C:/WINDOWS/Fonts/calibri.ttf";BaseFont bfChinese = BaseFont.createFont( fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);BaseFont bfBlackChinese = BaseFont.createFont( blackFontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);BaseFont bfNumber = BaseFont.createFont( numberPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);Font titleFont = new Font(bfChinese);Font font = new Font(bfChinese);Font blackFont = new Font(bfBlackChinese);Font lowTitleFont = new Font(bfChinese);Font numberFont = new Font(bfNumber);

设置字体样式(详细见API)

blackFont.setSize(16);//设置字体大小blackFont.setStyle(Font.BOLD);//设置字体加粗

接下来开始正式拼接PDF文档

需要用到的几个重要的对象

Chunk:块(Chunk)是能被添加到文档的文本的最小单位。

Paragraph:段落是一系列块或短句。同短句一样,段落有确定的间距。用户还可以指定缩排;在边和(或)右边保留一定空白,段落可以左对齐、右对齐和居中对齐。添加到文档中的每一个段落将自动另起一行。

Paragraph paragraph = new Paragraph();//创建段落paragraph.setKeepTogether(true);//使段落内容尽可能在同一页中(可能失效)paragraph.setAlignment(Element.ALIGN_CENTER);//设置段落中文字对齐格式Chunk titleChunk = new Chunk("项目基本信息", blackFont);//创建新的块并设置文字信息与字体paragraph.add(chunk);//把块加入到段落中paragraph.add(Chunk.NEWLINE);//加入新的一行,相当于换行paragraph.add(new Chunk(new LineSeparator()));//加入一条实线document.add(paragraph);//把段落加入到文档中

Image图片对象

Image image = new Imageimage = Image.getInstance(mapImgPath);//参数为图片的路径//设置居中image.setAlignment(Image.ALIGN_CENTER);//设置宽高image.scaleAbsolute(385, 247);//去除边框image.disableBorderSide(15);//插入地图图片document.add(image);

Table创建

//使用工具类编辑表格PdfPTable table = CreatePDFUtil.createTable(new float[]{120, 160, 120, 80});//设置表头table.addCell(CreatePDFUtil.createCell("所属部门", titleFont)).setFixedHeight(50);//设置单元格高度table.addCell(CreatePDFUtil.createCell("类型", titleFont));table.addCell(CreatePDFUtil.createCell("面积", titleFont));table.addCell(CreatePDFUtil.createCell("是否通过", titleFont));//设置表格内容table.addCell(CreatePDFUtil.createCell("A", font)).setFixedHeight(50);table.addCell(CreatePDFUtil.createCell("B", font)).setFixedHeight(50);table.addCell(CreatePDFUtil.createCell("C", font)).setFixedHeight(50);table.addCell(CreatePDFUtil.createCell("true", font)).setFixedHeight(50);document.add(table);//添加到文档中

操作完成进行关闭操作

PdfContentByte cb = writer.getDirectContent();cb.fill();cb.sanityCheck();document.close();

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。