前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【iText5 生成PDF】纯Java代码实现生成PDF(自定义表格、文本水印、单元格样式)

【iText5 生成PDF】纯Java代码实现生成PDF(自定义表格、文本水印、单元格样式)

作者头像
小帅丶
发布2019-07-22 17:20:28
8.1K0
发布2019-07-22 17:20:28
举报
文章被收录于专栏:XAIXAIXAI

工作中遇到需要生成PDF。最终选择了iText。其他也有通过html再生成。感觉不太适合就用了代码实现。 使用iText 5.5.13.1版本。纯Java代码实现 1.自定义表格合并指定行列完成数据填充 2.自定义单元格显示 3.文本内容水平垂直居中显示 4.中文显示 5.图片增加(三角雷达图,基于JFreeChart 可参考另一篇博文)

先看个效果图

Maven项目引入iText

<!-- itextpdf -->
<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>itextpdf</artifactId>
	<version>5.5.13.1</version>
</dependency>
<!-- itext-asian -->
<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>itext-asian</artifactId>
	<version>5.2.0</version>
</dependency>
<!-- itextpdf-tool-xmlworker -->
<dependency>
	<groupId>com.itextpdf.tool</groupId>
	<artifactId>xmlworker</artifactId>
	<version>5.5.13.1</version>
</dependency>

基于看到文档后,封装的工具类

package cn.netand.test;

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;
    }

    /**
     * @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;
    }
    /**
     * @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,Y
                float 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);
    }
}

生成PDF测试代码

package cn.netand.test;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @Description iTextGeneratePDFSample
 * @author 小帅丶
 * @className iTextGeneratePDFSample
 * @Date 2019/7/18-11:27
 **/
public class iTextGeneratePDFSample {
    //水印PDF
    public static final String DEST = "C:\\Users\\Administrator\\Desktop\\demo_watermark.pdf";
    public static void main(String[] args) throws Exception {
        PdfPCellEvent roundRectangle = new Samplepdf01.RoundRectangle();
        //测试pdf保存路径
        String filePath = "C:\\Users\\Administrator\\Desktop\\demoxs.pdf";
//        Image image = Image.getInstance(IMG);
        File file = new File(filePath);

        //生成PDF文档
        Document document = new Document(PageSize.A4);
        PdfWriter.getInstance(document,new FileOutputStream(file));
        document.open();
        document.addAuthor("小帅丶");//作者
        document.addCreationDate();//创建时间
        document.addCreator("https://www.ydxiaoshuai.cn");//创建者
        document.addTitle("报告");//标题
        document.addSubject("报告");//主题
        document.addKeywords("iText 生成PDF 纯代码实现 表格 等效果 ");//关键字
        //顶部说明
        document.add(new Paragraph("报告内容涉及个人隐私,查询者应依法使用、妥善保管。", iTextPDFUtil.getColorFont(BaseColor.RED,19,"宋体")));
        document.add( Chunk.NEWLINE );
        //报告基础信息说明
        document.add(new Paragraph("报告方:"+"有点小帅",iTextPDFUtil.getColorFont(BaseColor.BLACK,19,"宋体")));
        document.add(new Paragraph("报告编号:"+"YDXS201907180001",iTextPDFUtil.getColorFont(BaseColor.BLACK,19,"宋体")));
        document.add(new Paragraph("查询时间:"+"2019-07-18 13:31:51",iTextPDFUtil.getColorFont(BaseColor.BLACK,19,"宋体")));
        document.add( Chunk.NEWLINE );
        //高危预警
        PdfPTable table = new PdfPTable(2);
        table.setWidthPercentage(100);
        table.setHorizontalAlignment(Element.ALIGN_LEFT);
        PdfPCell cell = new PdfPCell(new Phrase("高危预警",iTextPDFUtil.getColorFont(BaseColor.WHITE)));
        table.addCell(iTextPDFUtil.addTitleCell(cell, 25, BaseColor.RED, 2, false));
        cell = new PdfPCell(new Phrase("无",iTextPDFUtil.getColorFont()));
        table.addCell(iTextPDFUtil.addBaseCell(cell));
        cell = new PdfPCell(new Phrase("未命中任何高危预警项目,请查看具体报告内容",iTextPDFUtil.getColorFont()));
        table.addCell(iTextPDFUtil.addBaseCell(cell));
        table.setWidths(iTextPDFUtil.getColumnWiths(10f,30f));
        //添加空行
        table.addCell(iTextPDFUtil.addBlankLine(10,2));
        document.add(table);


        PdfPTable tableMobileHeader = new PdfPTable(6);
        tableMobileHeader.setWidthPercentage(100);
        PdfPCell cellMobileHeader = new PdfPCell();

        Image radarImage = Image.getInstance("E:/JfreeChart/MySpiderWebPlot.png");
        cellMobileHeader = new PdfPCell(radarImage,false);
        cellMobileHeader.setRowspan(2);
        cellMobileHeader.setColspan(2);
        cellMobileHeader.setBorder(Rectangle.NO_BORDER);
        cellMobileHeader.setFixedHeight(100);
        tableMobileHeader.addCell(cellMobileHeader);
//        tableMobileHeader.addCell(iTextPDFUtil.addTitleCell(cellMobileHeader, 50, BaseColor.WHITE, 2, true));

        cellMobileHeader = new PdfPCell(new Phrase("预警总数",iTextPDFUtil.getColorFont()));
        cellMobileHeader.setUseBorderPadding(true);
        iTextPDFUtil.addTableHeaderData(tableMobileHeader,cellMobileHeader,2,null);

        cellMobileHeader = new PdfPCell(new Phrase("失信被执行人信息",iTextPDFUtil.getColorFont(BaseColor.WHITE,20,"黑体")));
        iTextPDFUtil.addTableHeaderData(tableMobileHeader,cellMobileHeader,0,new BaseColor(145,108,240));

        cellMobileHeader = new PdfPCell(new Phrase("存在失信记录",iTextPDFUtil.getColorFont(BaseColor.WHITE,20,"黑体")));
        iTextPDFUtil.addTableHeaderData(tableMobileHeader,cellMobileHeader,0,new BaseColor(240,113,62));

        cellMobileHeader = new PdfPCell(new Phrase("特殊名单核查",iTextPDFUtil.getColorFont(BaseColor.WHITE,20,"黑体")));
        iTextPDFUtil.addTableHeaderData(tableMobileHeader,cellMobileHeader,0,new BaseColor(44,154,241));

        cellMobileHeader = new PdfPCell(new Phrase("风险关注名单",iTextPDFUtil.getColorFont(BaseColor.WHITE,20,"黑体")));
        iTextPDFUtil.addTableHeaderData(tableMobileHeader,cellMobileHeader,0,new BaseColor(235,87,75));

        cellMobileHeader = new PdfPCell(new Phrase("逾期名单",iTextPDFUtil.getColorFont(BaseColor.WHITE,20,"黑体")));
        iTextPDFUtil.addTableHeaderData(tableMobileHeader,cellMobileHeader,0,new BaseColor(40,193,111));

        cellMobileHeader = new PdfPCell(new Phrase("过往存在逾期记录",iTextPDFUtil.getColorFont(BaseColor.WHITE,20,"黑体")));
        iTextPDFUtil.addTableHeaderData(tableMobileHeader,cellMobileHeader,0,new BaseColor(44,154,241));

        tableMobileHeader.addCell(iTextPDFUtil.addBlankLine(20,6));
        document.add(tableMobileHeader);


        //信息核验
        document.add(iTextPDFUtil.addTableGroupLine("信息核验"));
        PdfPTable tableBaseInfo = new PdfPTable(2);
        PdfPCell cellBaseInfo = new PdfPCell();
        tableBaseInfo.setWidthPercentage(100);
        tableBaseInfo.setHorizontalAlignment(Element.ALIGN_LEFT);
        /********身份信息核验************/
        iTextPDFUtil.addTableGroupTitle(tableBaseInfo,cellBaseInfo,"身份信息核验");
        //姓名
        cellBaseInfo = new PdfPCell(new Phrase("姓名",iTextPDFUtil.getColorFont()));
        tableBaseInfo.addCell(iTextPDFUtil.addBaseCell(cellBaseInfo));
        cellBaseInfo = new PdfPCell(new Phrase("有点小帅",iTextPDFUtil.getColorFont()));
        tableBaseInfo.addCell(iTextPDFUtil.addBaseCell(cellBaseInfo));
        tableBaseInfo.setWidths(iTextPDFUtil.getColumnWiths(10f,30f));
        //身份证号
        cellBaseInfo = new PdfPCell(new Phrase("身份证号",iTextPDFUtil.getColorFont()));
        tableBaseInfo.addCell(iTextPDFUtil.addBaseCell(cellBaseInfo));
        cellBaseInfo = new PdfPCell(new Phrase("110000******000010",iTextPDFUtil.getColorFont()));
        tableBaseInfo.addCell(iTextPDFUtil.addBaseCell(cellBaseInfo));
        float[] cloumnWithsBaseInfoIdCard =new float[]{10f,30f};
        tableBaseInfo.setWidths(cloumnWithsBaseInfoIdCard);
        //身份二要素验证
        cellBaseInfo = new PdfPCell(new Phrase("手机号",iTextPDFUtil.getColorFont()));
        tableBaseInfo.addCell(iTextPDFUtil.addBaseCell(cellBaseInfo,false));
        cellBaseInfo = new PdfPCell(new Phrase("188****8888",iTextPDFUtil.getColorFont()));
        tableBaseInfo.addCell(iTextPDFUtil.addBaseCell(cellBaseInfo,false));
        tableBaseInfo.setWidths(iTextPDFUtil.getColumnWiths(10f,30f));
        //核查建议
        iTextPDFUtil.addSuggestLine(tableBaseInfo, cellBaseInfo, "该客户身份信息核验正常",new BaseColor(79,170,246));
        //添加空行
        tableBaseInfo.addCell(iTextPDFUtil.addBlankLine(20,2));
        document.add(tableBaseInfo);

        PdfPTable tableMobileInfo = new PdfPTable(2);
        PdfPCell cellMobileInfo = new PdfPCell();
        tableMobileInfo.setWidthPercentage(100);
        tableMobileInfo.setHorizontalAlignment(Element.ALIGN_LEFT);
        /********手机信息核验************/
        iTextPDFUtil.addTableGroupTitle(tableMobileInfo,cellMobileInfo,"手机信息核验");
        //手机三要素验证
        cellMobileInfo = new PdfPCell(new Phrase("手机三要素验证",new Font(iTextPDFUtil.getFont())));
        tableMobileInfo.addCell(iTextPDFUtil.addBaseCell(cellMobileInfo));
        cellMobileInfo = new PdfPCell(new Phrase("一致",new Font(iTextPDFUtil.getFont())));
        tableMobileInfo.addCell(iTextPDFUtil.addBaseCell(cellMobileInfo));
        tableMobileInfo.setWidths(iTextPDFUtil.getColumnWiths(10f,30f));
        //手机在网状态
        cellMobileInfo = new PdfPCell(new Phrase("手机在网状态",new Font(iTextPDFUtil.getFont())));
        tableMobileInfo.addCell(iTextPDFUtil.addBaseCell(cellMobileInfo));
        cellMobileInfo = new PdfPCell(new Phrase("正常",new Font(iTextPDFUtil.getFont())));
        tableMobileInfo.addCell(iTextPDFUtil.addBaseCell(cellMobileInfo));
        tableMobileInfo.setWidths(iTextPDFUtil.getColumnWiths(10f,30f));
        //手机在网时长
        cellMobileInfo = new PdfPCell(new Phrase("手机在网时长",new Font(iTextPDFUtil.getFont())));
        tableMobileInfo.addCell(iTextPDFUtil.addBaseCell(cellMobileInfo));
        cellMobileInfo = new PdfPCell(new Phrase("24个月以上",new Font(iTextPDFUtil.getFont())));
        tableMobileInfo.addCell(iTextPDFUtil.addBaseCell(cellMobileInfo));
        tableMobileInfo.setWidths(iTextPDFUtil.getColumnWiths(10f,30f));
        //手机消费档次
        cellMobileInfo = new PdfPCell(new Phrase("手机消费档次",new Font(iTextPDFUtil.getFont())));
        tableMobileInfo.addCell(iTextPDFUtil.addBaseCell(cellMobileInfo,false));
        cellMobileInfo = new PdfPCell(new Phrase("移动,近三月平均金额50-100元",new Font(iTextPDFUtil.getFont())));
        tableMobileInfo.addCell(iTextPDFUtil.addBaseCell(cellMobileInfo,false));
        tableMobileInfo.setWidths(iTextPDFUtil.getColumnWiths(10f,30f));
        //核查建议
        iTextPDFUtil.addSuggestLine(tableMobileInfo, cellMobileInfo, "手机三要素验证一致",new BaseColor(245,163,112));
        //添加空行
        tableMobileInfo.addCell(iTextPDFUtil.addBlankLine(20,2));
        document.add(tableMobileInfo);

        PdfPTable tableBankInfo = new PdfPTable(2);
        PdfPCell cellBankInfo = new PdfPCell();
        tableBankInfo.setWidthPercentage(100);
        tableBankInfo.setHorizontalAlignment(Element.ALIGN_LEFT);
        /********银行卡信息核验(选填项)************/
        iTextPDFUtil.addTableGroupTitle(tableBankInfo,cellBankInfo,"银行卡信息核验(选填项)");
        //银行卡四要素核验
        cellBankInfo = new PdfPCell(new Phrase("银行卡四要素核验",iTextPDFUtil.getColorFont()));
        tableBankInfo.addCell(iTextPDFUtil.addBaseCell(cellBankInfo));
        cellBankInfo = new PdfPCell(new Phrase("一致",iTextPDFUtil.getColorFont()));
        tableBankInfo.addCell(iTextPDFUtil.addBaseCell(cellBankInfo));
        tableBankInfo.setWidths(iTextPDFUtil.getColumnWiths(10f,30f));
        //添加空行
        tableBankInfo.addCell(iTextPDFUtil.addBlankLine(20,2));
        document.add(tableBankInfo);
        //公检法信息
        document.add(iTextPDFUtil.addTableGroupLine("公检法信息"));
        PdfPTable tablePCInfo = new PdfPTable(2);
        PdfPCell cellPCInfo = new PdfPCell();
        tablePCInfo.setWidthPercentage(100);
        tablePCInfo.setHorizontalAlignment(Element.ALIGN_LEFT);
        /********公安负面信息************/
        iTextPDFUtil.addTableGroupTitle(tablePCInfo,cellPCInfo,"公安负面信息");
        //违法行为
        cellPCInfo = new PdfPCell(new Phrase("违法行为",iTextPDFUtil.getColorFont()));
        tablePCInfo.addCell(iTextPDFUtil.addBaseCell(cellPCInfo,false));
        cellPCInfo = new PdfPCell(new Phrase("金融诈骗案",iTextPDFUtil.getColorFont()));
        tablePCInfo.addCell(iTextPDFUtil.addBaseCell(cellPCInfo,false));
        tablePCInfo.setWidths(iTextPDFUtil.getColumnWiths(10f,30f));
        //核查建议
        iTextPDFUtil.addSuggestLine(tablePCInfo, cellPCInfo, "该客户存在公安负面信息,违约风险较大,不建议授信",new BaseColor(240,61,61));
        //添加空行
        tablePCInfo.addCell(iTextPDFUtil.addBlankLine(20,2));
        document.add(tablePCInfo);
        //失信被执行人信息
        PdfPTable breakFithTable = new PdfPTable(5);
        PdfPCell breakFithCell = new PdfPCell();
        breakFithTable.setWidthPercentage(100);
        breakFithTable.setHorizontalAlignment(Element.ALIGN_LEFT);
        iTextPDFUtil.addTableGroupTitle(breakFithTable,breakFithCell,"失信被执行人信息",5);
        //实际渲染数据 循环即可
        //表头1
        breakFithCell = new PdfPCell(new Phrase("序号",iTextPDFUtil.getColorFont(new BaseColor(204,204,204))));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("姓名",iTextPDFUtil.getColorFont(new BaseColor(204,204,204))));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("执行案号",iTextPDFUtil.getColorFont(new BaseColor(204,204,204))));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("执行法院",iTextPDFUtil.getColorFont(new BaseColor(204,204,204))));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("案件执行状态",iTextPDFUtil.getColorFont(new BaseColor(204,204,204))));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        //数据行1
        breakFithCell = new PdfPCell(new Phrase("1",iTextPDFUtil.getColorFont()));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("艾腾祥",iTextPDFUtil.getColorFont()));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("(2018) 沪0115执15507号",iTextPDFUtil.getColorFont()));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("上海市浦东新区人民法院",iTextPDFUtil.getColorFont()));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("-",iTextPDFUtil.getColorFont()));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        //表头2
        breakFithCell = new PdfPCell(new Phrase("   ",iTextPDFUtil.getColorFont(new BaseColor(204,204,204))));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("执行依据文号",iTextPDFUtil.getColorFont(new BaseColor(204,204,204))));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("执行依据单位",iTextPDFUtil.getColorFont(new BaseColor(204,204,204))));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("被执行人履行情况",iTextPDFUtil.getColorFont(new BaseColor(204,204,204))));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("发布日期",iTextPDFUtil.getColorFont(new BaseColor(204,204,204))));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        //数据行2
        breakFithCell = new PdfPCell(new Phrase("   ",iTextPDFUtil.getColorFont(new BaseColor(204,204,204))));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("(2018) 沪0115执15507号",iTextPDFUtil.getColorFont()));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("上海市浦东新区人民法院",iTextPDFUtil.getColorFont()));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("全部未履行",iTextPDFUtil.getColorFont()));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("2018-08-03",iTextPDFUtil.getColorFont()));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        //数据行3
        breakFithCell = new PdfPCell(new Phrase("失信公告详情",iTextPDFUtil.getColorFont(new BaseColor(204,204,204))));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("有履行能力而拒不履行生效法律文...",iTextPDFUtil.getColorFont()));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("   ",iTextPDFUtil.getColorFont()));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("   ",iTextPDFUtil.getColorFont()));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("-",iTextPDFUtil.getColorFont()));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        //数据行4
        breakFithCell = new PdfPCell(new Phrase("失信人被执行人行为具体情形",iTextPDFUtil.getColorFont(new BaseColor(204,204,204))));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("有履行能力而拒不履行生效法律文书确定义务",iTextPDFUtil.getColorFont()));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("   ",iTextPDFUtil.getColorFont()));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("   ",iTextPDFUtil.getColorFont()));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        breakFithCell = new PdfPCell(new Phrase("   ",iTextPDFUtil.getColorFont()));
        breakFithTable.addCell(iTextPDFUtil.addBaseCell(breakFithCell));
        document.add(breakFithTable);
        //添加空行
        breakFithTable.addCell(iTextPDFUtil.addBlankLine(20,5));
        //额外处理的建议表格
        PdfPTable breakFootTable = new PdfPTable(2);
        breakFootTable.setWidthPercentage(100);
        breakFootTable.setHorizontalAlignment(Element.ALIGN_LEFT);
        PdfPCell breakFootCell = new PdfPCell();
        iTextPDFUtil.addSuggestLine(breakFootTable, breakFootCell, "存在失信记录,违约风险较大,不建议授信", new BaseColor(240,61,61),0,20f,50f);
        document.add(breakFootTable);

        document.add(Chunk.NEWLINE);
        //添加图片
//        document.add(image);
        document.add(Chunk.NEWLINE);
        document.add(new Paragraph("请妥善保管报告",new Font(iTextPDFUtil.getFont())));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        document.close();
        //添加文本水印
        FileInputStream input1 = new FileInputStream(file);
        FileOutputStream output2 = new FileOutputStream(new File(DEST));
        iTextPDFUtil.stringWaterMark(input1, output2, "有点小帅", 4, 5, 0.2f,45, 26,BaseColor.BLACK);//文字水印
//        iTextPDFUtil.imageWaterMark(input1, output2, IMG, 0.2f);
    }

}

PDF示例文件查看地址

https://gitee.com/xshuai/ai/attach_files

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 先看个效果图
  • Maven项目引入iText
  • 基于看到文档后,封装的工具类
  • 生成PDF测试代码
  • PDF示例文件查看地址
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档