工作中遇到需要生成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);
}
}