因为工作中要用到,导出不同的PDF,所以学习了Itext

我两天创建的PDF如下所示,主要是简单的表格类的

1、引包

       <dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.11</version></dependency><dependency><groupId>com.itextpdf.tool</groupId><artifactId>xmlworker</artifactId><version>5.5.11</version></dependency><!-- 支持中文 --><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency><!-- 支持css样式渲染 --><dependency><groupId>org.xhtmlrenderer</groupId><artifactId>flying-saucer-pdf-itext5</artifactId><version>9.1.16</version></dependency><!-- 转换html为标准xhtml包 --><dependency><groupId>net.sf.jtidy</groupId><artifactId>jtidy</artifactId><version>r938</version></dependency>

2、工具类

package pdfdemo.pdfdemo.demoOne;import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;import java.io.InputStream;
import java.io.OutputStream;/*** @Description iTextPDFUtil* @author 小帅丶* @className iTextPDFUtil* @Date 2019/7/18-11:26**/
public class iTextPDFUtil {/** @Description 蓝色背景色标题内容行添加* @Author 小帅丶* @Date  2019/7/12 14:56* @param table 表格* @param cell  列* @param text  文本* @return void**/public static void addTableGroupTitle(PdfPTable table, PdfPCell cell, String text) {cell = new PdfPCell(new Phrase(text,getColorFont(BaseColor.WHITE)));table.addCell(addTitleCell(cell,25,new BaseColor(69,153,241),2,false));}/*** @Description 蓝色背景色标题内容行添加* @Author 小帅丶* @Date  2019/7/12 14:56* @param table 表格* @param cell  列* @param text  文本* @param colspan 需要合并的列* @return void**/public static void addTableGroupTitle(PdfPTable table, PdfPCell cell, String text,int colspan) {cell = new PdfPCell(new Phrase(text,getColorFont(BaseColor.WHITE)));table.addCell(addTitleCell(cell,25,new BaseColor(69,153,241),colspan,false));}/*** @Description 核查建议* @Author 小帅丶* @Date  2019/7/12 14:43* @param table 表格* @param cell 列* @param suggestText 核查建议内容* @param fontColor 核查建议内容文字颜色* @return com.itextpdf.text.Element**/public static void addSuggestLine(PdfPTable table,PdfPCell cell,String suggestText,BaseColor fontColor) throws Exception {addSuggestLine(table, cell, suggestText, fontColor, 0,10f,30f);}/*** @Description 核查建议* @Author 小帅丶* @Date  2019/7/12 14:43* @param table 表格* @param cell 列* @param suggestText 核查建议内容* @param fontColor 核查建议内容文字颜色* @param colspan 合并的列* @param widths 列所占宽* @return com.itextpdf.text.Element**/public static void addSuggestLine(PdfPTable table,PdfPCell cell,String suggestText,BaseColor fontColor,int colspan,float...widths) throws Exception {cell = new PdfPCell(new Phrase("核查建议:",getColorFont()));cell.setColspan(1);table.addCell(addBaseCell(cell,23,new BaseColor(238,238,238),false));cell = new PdfPCell(new Phrase(suggestText,getColorFont(fontColor)));if(colspan>0){cell.setColspan(colspan);}table.addCell(addBaseCell(cell,23,new BaseColor(238,238,238),false));table.setWidths(getColumnWiths(widths));}/*** @Description 信息分组table* @Author 小帅丶* @Date  2019/7/12 14:43* @param groupText 文本内容* @return com.itextpdf.text.Element**/public static Element addTableGroupLine(String groupText) {PdfPTable tableBaseInfoIndex = new PdfPTable(1);tableBaseInfoIndex.setWidthPercentage(20);PdfPCell cellBaseInfo = new PdfPCell(new Phrase(groupText,getColorFont()));cellBaseInfo.setHorizontalAlignment(Element.ALIGN_CENTER);tableBaseInfoIndex.addCell(addTitleCell(cellBaseInfo,28,new BaseColor(238,238,238),2,false));tableBaseInfoIndex.addCell(addBlankLine(10,1));return tableBaseInfoIndex;}public static Element addHeadLine(String groupText) {PdfPTable tableBaseInfoIndex = new PdfPTable(1);tableBaseInfoIndex.setWidthPercentage(100);PdfPCell cellBaseInfo = new PdfPCell(new Phrase(groupText,getHeadFont()));cellBaseInfo.setBorderWidth(0);cellBaseInfo.setMinimumHeight(80);cellBaseInfo.setHorizontalAlignment(Element.ALIGN_CENTER);cellBaseInfo.setVerticalAlignment(Element.ALIGN_MIDDLE);tableBaseInfoIndex.addCell(cellBaseInfo);return tableBaseInfoIndex;}public static PdfPCell addOneLineBox(String groupText,int col) {PdfPCell cell;cell = new PdfPCell(new Phrase(groupText, iTextPDFUtil.getColorFont()));cell.setColspan(col);cell.setMinimumHeight(35);cell.setRowspan(1);cell.setHorizontalAlignment(Element.ALIGN_CENTER);cell.setVerticalAlignment(Element.ALIGN_MIDDLE);return cell;}public static PdfPCell addTwoLineBox(String groupText,int col) {PdfPCell cell;cell = new PdfPCell(new Phrase(groupText, iTextPDFUtil.getColorFont()));cell.setColspan(col);cell.setMinimumHeight(45);cell.setRowspan(2);cell.setHorizontalAlignment(Element.ALIGN_CENTER);cell.setVerticalAlignment(Element.ALIGN_MIDDLE);return cell;}/*** @Description 指定颜色字体 默认处理中文显示* @Author 小帅丶* @Date  2019/7/12 14:05* @param color 字体颜色* @param fontSize 字体大小* @param fontFamily 字体* @return com.itextpdf.text.Font**/public static Font getColorFont(BaseColor color, int fontSize, String fontFamily) {Font font = new Font(getFont());font.setColor(color);if(fontSize>0&&(null!=fontFamily||!"".equals(fontFamily))){font.setSize(fontSize);font.setFamily(fontFamily);}return font;}/*** @Description 指定颜色字体 默认处理中文显示* @Author 小帅丶* @Date  2019/7/12 14:05* @param color 字体颜色* @return com.itextpdf.text.Font**/public static Font getColorFont(BaseColor color) {return getColorFont(color, 0, null);}/*** @Description  默认处理中文显示* @Author 小帅丶* @Date  2019/7/12 14:05* @return com.itextpdf.text.Font**/public static Font getColorFont() {Font font = new Font(getFont());return font;}public static Font getHeadFont() {Font font = new Font(getFont());font.setSize(25);return font;}/*** @Description 指定列宽度* @Author 小帅丶* @Date  2019/7/12 11:59* @param widths 一个或多个* @return float[]**/public static float[] getColumnWiths(float...widths){float[] columnWidths = new float[widths.length];for (int i = 0; i < widths.length; i++) {columnWidths[i]=widths[i];}return columnWidths;}/*** @Description 添加表头cell* @Author 小帅丶* @Date  2019/7/12 11:36* @param titleCell 要操作的cell* @param fixedHeight 行高度* @param baseColor 背景色* @param colspan  合并的列数* @param isBottomBorder 是否有下边框 true 有 fasle 没有* @return com.itextpdf.text.pdf.PdfPCell**/public static PdfPCell addTitleCell(PdfPCell titleCell,int fixedHeight,BaseColor baseColor,int colspan,boolean isBottomBorder){titleCell.setColspan(colspan);titleCell.setFixedHeight(fixedHeight);titleCell.setUseVariableBorders(true);titleCell.setUseAscender(true);titleCell.setUseDescender(true);titleCell.setBackgroundColor(baseColor);if(isBottomBorder){titleCell.setBorder(Rectangle.BOTTOM);titleCell.setBorderColorBottom(BaseColor.LIGHT_GRAY);}else{titleCell.setBorder(Rectangle.NO_BORDER);}titleCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);return titleCell;}/*** @Description 添加空行* @Author 小帅丶* @Date  2019/7/12 11:36* @param fixedHeight 空行高度* @param colspan  合并的列数* @return com.itextpdf.text.pdf.PdfPCell**/public static PdfPCell addBlankLine(int fixedHeight,int colspan){PdfPCell blankLine = new PdfPCell();blankLine.setFixedHeight(fixedHeight);blankLine.setBorder(Rectangle.NO_BORDER);blankLine.setColspan(colspan);return blankLine;}/*** @Description 添加默认cell* @Author 小帅丶* @param baseCell 要操作的cell* @Date  2019/7/12 11:36* @return com.itextpdf.text.pdf.PdfPCell**/public static PdfPCell addBaseCell(PdfPCell baseCell){baseCell.setFixedHeight(23);baseCell.setUseVariableBorders(true);baseCell.setUseAscender(true);baseCell.setUseDescender(true);baseCell.setBorder(Rectangle.BOTTOM);baseCell.setBorderColorBottom(BaseColor.LIGHT_GRAY);baseCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);return baseCell;}/*** @Description 添加cell* @Author 小帅丶* @param baseCell 要操作的cell* @param isBottomBorder 是否有下边框 true 有 fasle 没有* @Date  2019/7/12 11:36* @return com.itextpdf.text.pdf.PdfPCell**/public static PdfPCell addBaseCell(PdfPCell baseCell,boolean isBottomBorder){baseCell.setFixedHeight(23);baseCell.setUseVariableBorders(true);baseCell.setUseAscender(true);baseCell.setUseDescender(true);if(isBottomBorder){baseCell.setBorder(Rectangle.BOTTOM);baseCell.setBorderColorBottom(BaseColor.LIGHT_GRAY);}else{baseCell.setBorder(Rectangle.NO_BORDER);}baseCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);return baseCell;}/*** @Description 添加cell* @Author 小帅丶* @param baseCell 要操作的cell* @param fixedHeight 行高* @param color 背景色* @param isBottomBorder 是否有下边框 true 有 fasle 没有* @Date  2019/7/12 11:36* @return com.itextpdf.text.pdf.PdfPCell**/public static PdfPCell addBaseCell(PdfPCell baseCell,int fixedHeight,BaseColor color,boolean isBottomBorder){baseCell.setFixedHeight(fixedHeight);baseCell.setUseVariableBorders(true);baseCell.setUseAscender(true);baseCell.setUseDescender(true);if(null!=color){baseCell.setBackgroundColor(color);}if(isBottomBorder){baseCell.setBorder(Rectangle.BOTTOM);baseCell.setBorderColorBottom(BaseColor.LIGHT_GRAY);}else{baseCell.setBorder(Rectangle.NO_BORDER);}baseCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);return baseCell;}/*** @Description 设置中文支持* @Author 小帅丶* @Date  2019/7/11 10:33* @Param []* @return com.itextpdf.text.pdf.BaseFont**/public static BaseFont getFont() {BaseFont bf = null;try {
//bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);} catch (Exception e) {System.out.println("Exception = " + e.getMessage());}return bf;}/*** 斜角排列、全屏多个重复的花式文字水印** @param input             需要加水印的PDF读取输入流* @param output            输出生成PDF的输出流* @param waterMarkString   水印字符* @param xAmout            x轴重复数量* @param yAmout            y轴重复数量* @param opacity           水印透明度* @param rotation          水印文字旋转角度,一般为45度角* @param waterMarkFontSize 水印字体大小* @param color             水印字体颜色*/public static void stringWaterMark(InputStream input, OutputStream output, String waterMarkString, int xAmout, int yAmout, float opacity, float rotation, int waterMarkFontSize, BaseColor color) {try {PdfReader reader = new PdfReader(input);PdfStamper stamper = new PdfStamper(reader, output);// 添加中文字体BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);int total = reader.getNumberOfPages() + 1;PdfContentByte over;// 给每一页加水印for (int i = 1; i < total; i++) {Rectangle pageRect = stamper.getReader().getPageSizeWithRotation(i);// 计算水印每个单位步长X,Yfloat x = pageRect.getWidth() / xAmout;float y = pageRect.getHeight() / yAmout;over = stamper.getOverContent(i);PdfGState gs = new PdfGState();// 设置透明度为gs.setFillOpacity(opacity);over.setGState(gs);over.saveState();over.beginText();over.setColorFill(color);over.setFontAndSize(baseFont, waterMarkFontSize);for (int n = 0; n < xAmout + 1; n++) {for (int m = 0; m < yAmout + 1; m++) {over.showTextAligned(Element.ALIGN_CENTER, waterMarkString, x * n, y * m, rotation);}}over.endText();}stamper.close();} catch (Exception e) {new Exception("NetAnd PDF add Text Watermark error"+e.getMessage());}}/*** 图片水印,整张页面平铺* @param input     需要加水印的PDF读取输入流* @param output    输出生成PDF的输出流* @param imageFile 水印图片路径*/public static void imageWaterMark(InputStream input, OutputStream output, String imageFile, float opacity) {try {PdfReader reader = new PdfReader(input);PdfStamper stamper = new PdfStamper(reader, output);Rectangle pageRect = stamper.getReader().getPageSize(1);float w = pageRect.getWidth();float h = pageRect.getHeight();int total = reader.getNumberOfPages() + 1;Image image = Image.getInstance(imageFile);image.setAbsolutePosition(0, 0);// 坐标image.scaleAbsolute(w, h);PdfGState gs = new PdfGState();gs.setFillOpacity(opacity);// 设置透明度PdfContentByte over;// 给每一页加水印float x, y;Rectangle pagesize;for (int i = 1; i < total; i++) {pagesize = reader.getPageSizeWithRotation(i);x = (pagesize.getLeft() + pagesize.getRight()) / 2;y = (pagesize.getTop() + pagesize.getBottom()) / 2;over = stamper.getOverContent(i);over.setGState(gs);over.saveState();//没这个的话,图片透明度不起作用,必须在beginText之前,否则透明度不起作用,会被图片覆盖了内容而看不到文字了。over.beginText();// 添加水印图片over.addImage(image);}stamper.close();} catch (Exception e) {new Exception("NetAnd PDF add image Watermark error" + e.getMessage());}}/*** @description 顶部表格卡片形式显示格式数据组装* @Author 小帅丶* @Date  2019/7/16 15:31* @param tableMobileHeader 要操作的表格* @param cellMobileHeader 要操作的单元格* @param clospan 合并列 不需要合并填写0* @param fixedHeight 行高* @param padding 间距* @param border 边框* @param borderColor 边框颜色* @param backgroundColor 背景色* @param vertical 垂直对齐方式* @param horizontal  水平对齐方式* @return void**/public static void addTableHeaderData(PdfPTable tableMobileHeader, PdfPCell cellMobileHeader, int clospan, float fixedHeight, int padding, int border, BaseColor borderColor, BaseColor backgroundColor, int vertical, int horizontal) {cellMobileHeader.setUseBorderPadding(true);cellMobileHeader.setUseAscender(true);if(clospan>0){cellMobileHeader.setColspan(clospan);}cellMobileHeader.setUseDescender(true);cellMobileHeader.setFixedHeight(fixedHeight);cellMobileHeader.setPadding(padding);cellMobileHeader.setVerticalAlignment(vertical);cellMobileHeader.setHorizontalAlignment(horizontal);if(null!=backgroundColor){cellMobileHeader.setBackgroundColor(backgroundColor);}cellMobileHeader.setBorder(border);cellMobileHeader.setBorderColor(borderColor);tableMobileHeader.addCell(cellMobileHeader);}/*** @description 顶部表格卡片形式显示格式数据组装* @Author 小帅丶* @Date  2019/7/16 15:31* @param tableMobileHeader 要操作的表格* @param cellMobileHeader 要操作的单元格* @param clospan 合并列 不需要合并填写0* @param backgroundColor 背景色* @return void**/public static void addTableHeaderData(PdfPTable tableMobileHeader, PdfPCell cellMobileHeader, int clospan,BaseColor backgroundColor) {addTableHeaderData(tableMobileHeader, cellMobileHeader, clospan, 100, 10, 30, BaseColor.WHITE, backgroundColor, 0, 0);}
}

3、创建一个简单的PDF

public class CreatePDFMainTest {public static void main(String[] args) throws Exception {Document document = new Document(PageSize.A4);//第二步,创建Writer实例PdfWriter.getInstance(document, new FileOutputStream("hello.pdf"));//创建中文字体BaseFont bfchinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);Font fontChinese = new Font(bfchinese, 12, Font.NORMAL);//第三步,打开文档document.open();//第四步,写入内容Paragraph paragraph = new Paragraph("hello world", fontChinese);document.add(paragraph);//第五步,关闭文档document.close();}}

4、我建的表格简单,所以用到API也简单

1、创建一个表格,表格有6列,

并且铺满屏幕

        PdfPTable goodTable = new PdfPTable(6);goodTable.setWidthPercentage(100);

2、创建格子

        PdfPCell cell;cell = new PdfPCell(new Phrase(“格子内容”, iTextPDFUtil.getColorFont()));
//格子横跨2个格子cell.setColspan(2);
//格子高度35pxcell.setMinimumHeight(35);
//格子纵跨1个格子cell.setRowspan(1);
//格子内容左右居中cell.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

3、举例如下

package pdfdemo.pdfdemo.utils;import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import pdfdemo.pdfdemo.demoOne.iTextPDFUtil;import java.io.FileOutputStream;/*** @program: pdfdemo* @ClassName CreatePDFMainTest* @description:* @author:蒋皓洁* @create: 2022-04-02 11:29* @Version 1.0**/
public class CreatePDFMainTest3 {public static void main(String[] args) throws Exception {Document document = new Document(PageSize.A4);//第二步,创建Writer实例PdfWriter.getInstance(document, new FileOutputStream("hello.pdf"));//创建中文字体BaseFont bfchinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);Font fontChinese = new Font(bfchinese, 12, Font.NORMAL);//第三步,打开文档document.open();//第四步,写入内容
//创建一列的格子PdfPTable goodTable = new PdfPTable(6);goodTable.setWidthPercentage(100);PdfPCell cell;cell = new PdfPCell(new Phrase("格子内容", iTextPDFUtil.getColorFont()));
//格子横跨2个格子cell.setColspan(6);
//格子高度35pxcell.setMinimumHeight(35);
//格子纵跨1个格子cell.setRowspan(1);
//格子内容左右居中cell.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中cell.setVerticalAlignment(Element.ALIGN_MIDDLE);goodTable.addCell(cell);PdfPCell  cell2 = new PdfPCell(new Phrase("测试左边", iTextPDFUtil.getColorFont()));
//格子横跨2个格子cell2.setColspan(3);
//格子高度35pxcell2.setMinimumHeight(35);
//格子纵跨1个格子cell2.setRowspan(2);
//格子内容左右居中cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);goodTable.addCell(cell2);PdfPCell  cell3= new PdfPCell(new Phrase("测试右上", iTextPDFUtil.getColorFont()));
//格子横跨2个格子cell3.setColspan(3);
//格子高度35pxcell3.setMinimumHeight(35);
//格子纵跨1个格子cell3.setRowspan(1);
//格子内容左右居中cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);goodTable.addCell(cell3);PdfPCell cell4 = new PdfPCell(new Phrase("测试右下", iTextPDFUtil.getColorFont()));
//格子横跨2个格子cell4.setColspan(3);
//格子高度35pxcell4.setMinimumHeight(35);
//格子纵跨1个格子cell4.setRowspan(1);
//格子内容左右居中cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);goodTable.addCell(cell4);document.add(goodTable);//第五步,关闭文档document.close();}}

出来的如下

5、表格内嵌(一个table中内嵌宁外一个table)

 PdfPTable celltable =new PdfPTable(2);cell =new PdfPCell(celltable);cell.setColspan(2);//横着占用两格cell.setBorderWidth(1);//设置表格的边框宽度为1cell.setPadding(10);//设置表格与上一个表格的填充为10table.addCell(cell);

6、测试demo展示,有些代码可以封装,但是为了方便看且粘贴就没有封装

上面格子的代码展示如下

package pdfdemo.pdfdemo.utils;import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import pdfdemo.pdfdemo.demoOne.iTextPDFUtil;import java.io.FileOutputStream;/*** @program: pdfdemo* @ClassName CreatePDFMainTest* @description:* @author:蒋皓洁* @create: 2022-04-02 11:29* @Version 1.0**/
public class CreatePDFMainTest3 {public static void main(String[] args) throws Exception {Document document = new Document(PageSize.A4);//第二步,创建Writer实例PdfWriter.getInstance(document, new FileOutputStream("hello.pdf"));//创建中文字体BaseFont bfchinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);Font fontChinese = new Font(bfchinese, 12, Font.NORMAL);//第三步,打开文档document.open();//第四步,写入内容
//创建一列的格子PdfPTable goodTable = new PdfPTable(6);goodTable.setWidthPercentage(100);PdfPCell cell;cell = new PdfPCell(new Phrase("格子内容", iTextPDFUtil.getColorFont()));
//格子横跨2个格子cell.setColspan(6);
//格子高度35pxcell.setMinimumHeight(35);
//格子纵跨1个格子cell.setRowspan(1);
//格子内容左右居中cell.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中cell.setVerticalAlignment(Element.ALIGN_MIDDLE);goodTable.addCell(cell);PdfPCell cell2 = new PdfPCell(new Phrase("测试左边", iTextPDFUtil.getColorFont()));
//格子横跨2个格子cell2.setColspan(3);
//格子高度35pxcell2.setMinimumHeight(35);
//格子纵跨1个格子cell2.setRowspan(2);
//格子内容左右居中cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);goodTable.addCell(cell2);PdfPCell cell3 = new PdfPCell(new Phrase("测试右上", iTextPDFUtil.getColorFont()));
//格子横跨2个格子cell3.setColspan(3);
//格子高度35pxcell3.setMinimumHeight(35);
//格子纵跨1个格子cell3.setRowspan(1);
//格子内容左右居中cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);goodTable.addCell(cell3);PdfPCell cell4 = new PdfPCell(new Phrase("测试右下", iTextPDFUtil.getColorFont()));
//格子横跨2个格子cell4.setColspan(3);
//格子高度35pxcell4.setMinimumHeight(35);
//格子纵跨1个格子cell4.setRowspan(1);
//格子内容左右居中cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);goodTable.addCell(cell4);PdfPCell cell5 = new PdfPCell(new Phrase("内嵌表格展示", iTextPDFUtil.getColorFont()));
//格子横跨2个格子cell5.setColspan(2);
//格子高度35pxcell5.setMinimumHeight(35);
//格子纵跨1个格子cell5.setRowspan(1);
//格子内容左右居中cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);goodTable.addCell(cell5);PdfPTable goodTable2 = new PdfPTable(3);PdfPCell cell6 = new PdfPCell(new Phrase("内嵌表格抬头一", iTextPDFUtil.getColorFont()));
//格子横跨2个格子cell6.setColspan(1);
//格子高度35pxcell6.setMinimumHeight(35);
//格子纵跨1个格子cell6.setRowspan(1);
//格子内容左右居中cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中cell6.setVerticalAlignment(Element.ALIGN_MIDDLE);goodTable2.addCell(cell6);PdfPCell cell7 = new PdfPCell(new Phrase("内嵌表格抬头二", iTextPDFUtil.getColorFont()));
//格子横跨2个格子cell7.setColspan(2);
//格子高度35pxcell7.setMinimumHeight(35);
//格子纵跨1个格子cell7.setRowspan(1);
//格子内容左右居中cell7.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中cell7.setVerticalAlignment(Element.ALIGN_MIDDLE);goodTable2.addCell(cell7);PdfPCell cell8 = new PdfPCell(new Phrase("内嵌表格抬头三", iTextPDFUtil.getColorFont()));
//格子横跨2个格子cell8.setColspan(3);
//格子高度35pxcell8.setMinimumHeight(35);
//格子纵跨1个格子cell8.setRowspan(1);
//格子内容左右居中cell8.setHorizontalAlignment(Element.ALIGN_CENTER);
//格子内容上下居中cell8.setVerticalAlignment(Element.ALIGN_MIDDLE);goodTable2.addCell(cell8);PdfPCell cell33 = new PdfPCell(goodTable2);cell33.setColspan(4);cell33.setBorderWidth(1);//设置表格的边框宽度为1cell33.setPadding(7);//设置表格与上一个表格的填充为10goodTable.addCell(cell33);document.add(goodTable);//第五步,关闭文档document.close();}}

参考的几个博主内容

https://cloud.tencent.com/developer/article/1469129?from=15425https://cloud.tencent.com/developer/article/1469129?from=15425

http://www.javashuo.com/article/p-txowdndf-ny.htmlhttp://www.javashuo.com/article/p-txowdndf-ny.html

Java利用itext实现导出PDF文件相关推荐

  1. java使用itext生成表格pdf文件

    以下主要讲解的是java使用itext生成表格pdf文件,话不多说,直接上代码 一.首先引入itext所使用的包 <dependency><groupId>com.itextp ...

  2. unity利用ITextSharp实现导出pdf文件

    unity创建pdf文件首先需要搭建ITextSharp环境,先要导入所需要的dll文件.选用vs创建一个控制台项目,然后点击工具-包管理器-管理解决方案的Nuget程序包选项,如图所示: 打开包管理 ...

  3. java利用iText工具包生成PDF

    iText是一个非常著名的能够快速产生PDF文件的Java类库.支持文本,表格,图形的操作,可以方便的跟 Servlet 进行结合 iText的更新变化很大,早期版本在PDF样式上可能会有瑕疵,所有我 ...

  4. Java使用iText实现对PDF文件的操作

    iText是著名的开放项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转化为PDF文件. http://itextpdf.c ...

  5. java利用poi模板导出word文件

    注意:  doc文件的读取,需要导入poi-scratchpad包: docx文件读取,需要导入poi-ooxml包: 一.引入pom <dependency><groupId> ...

  6. java 使用itext导出PDF文件,中文不显示问题解决

    之前写的java 使用itext 导出pdf 发现有个问题,在今天使用的时候,发现一个问题,就是当单元格中写中文的时候,导出来的pdf中文不显示. java 使用itext导出PDF文件,图片文字左右 ...

  7. 使用java导出pdf文件

    使用java导出pdf文件 itext itext的使用 JasperReports JasperReports与itext的区别 Jaspersoft Studio 工具简介 Jaspersoft ...

  8. Itext导出pdf文件

    使用iText生成pdf文件并导出 前言: 项目中需要给用户提供一个可以导出pdf文件的功能,用来展示本月的数据.像excel.word这种直接就可以使用POI进行导出了,但是导出pdf格式的文件 我 ...

  9. Java中导出pdf文件,pdf工具类demo

    最近在做导出pdf文件的功能,参考了很多资料和demo,完成了转出pdf的功能,并适合项目的pdf工具类,现贴出具体的工具类和demo,如有不对的地方欢迎指正 1.pdf工具类PdfUtil.java ...

  10. 咖啡汪日志——JAVA导出pdf文件加水印 文字+图片、文字

    咖啡汪日志--JAVA导出pdf文件加水印 文字和图片.文字 hello,又大家见面了! 作为一只不是在戏精就是在戏精路上的哈士奇,今天要展示给大家的就是如何快捷地给pdf文件增加各种水印.嗷呜呜,前 ...

最新文章

  1. redisson get()数据报错,missing type id property ‘@class’
  2. ASP.NET 实践:写入 Cookie
  3. OpenCV calcHist()创建直方图的实例(附完整代码)
  4. OpenGL着色器创建一个星系由颗粒制成
  5. StackExchange.Redis 访问封装类
  6. 关于 Angular 项目类型为 library 的工程使用 tsconfig.json 的问题
  7. linux通过yum安装vim,linux/centos系统如何使用yum安装vi/vim?
  8. dateutil 日期计算_日期时间 - 日期时间工具-DateUtil - 《Hutool 参考文档》 - 书栈网 · BookStack...
  9. MySql 1067错误
  10. CSS命名与书写规范
  11. linux下输入法终极解决方案
  12. eos 源代码学习笔记二
  13. 庞果答题:亿阳信通:不可表示的数 的一个人见解(8-13第二次更新。)
  14. 《Pajek社会网络探索性分析》书籍简介
  15. Microsoft Excel 教程:如何在 Excel 中筛选唯一值或删除重复值?
  16. 面部皮肤200种问题_面部皮肤问题
  17. SpringBoot 集成 WebSocket 实现消息群发推送
  18. 图神经网络推荐方向--论文代码读记
  19. 计算机科学与技术要求具备的能力,计算机科学与技术专业毕业要求及指标点(2019版)...
  20. 集合--Set集合--HashSet类、LinkedHashSet类、TreeSet类及其自然排序

热门文章

  1. HTML如何实现简单登录页面
  2. natapp 使用教程
  3. 校招| C++ 后台开发学习路线
  4. ❤️制作人工智能QQ机器人,视频教程+源码❤️内容超级丰富,慢慢看!
  5. Topaz Gigapixel AI打开软件闪退及加载图片闪退 解决办法
  6. Python 数据挖掘(四) pandas模块 简单使用
  7. mysql的sqlhelper_SqlHelper
  8. 【黑苹果EFI下载】三星笔记本NP500R4K(5200U+HD5500)+Macos10.14版本
  9. GB2312、BIG5、GBK、GB18030简介
  10. 安卓一键清理内存_豆豆清理大师免费下载-豆豆清理大师老年版 v1.0.0手机版