1、公司要求生成pdf文件,内容中间需要一个表格,表格要根据数据列表的长度动态变长
直接上代码,需要的复制+百度
引入包
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
package com.tansun.tb.portal.utils;
import javax.servlet.ServletResponseWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import org.apache.logging.log4j.core.jmx.Server;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.TabStop.Alignment;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.AcroFields.FieldPosition;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
/*import com.lowagie.text.*;
import com.lowagie.text.pdf.*; */
import java.io.*;
import java.util.*;
import java.awt.Color;
public class signPdfExport {
/**
* 生成最终版本的pdf
* @param newPath 已写入数据的pdf模板路径
* @param finalPath 最终版本的pdf生成路径
* @param listNum
* @param baselineData2
* @throws Exception
*/
public static byte[] generateFinalPdf(List<String[]> list, String[] titleArr,
float[] widths, List<String[]> listNum,Map<String, Object> params) throws Exception {
/** 实例化文档对象 */
Document document = new Document();
/** 创建 PdfWriter 对象 */
//创建临时文件名
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
//临时路径
String path = uuid+".pdf";
FileOutputStream outputStream = new FileOutputStream(path);
PdfWriter writer = PdfWriter.getInstance(document, outputStream);// 文件的输出路径+文件的实际名称
document.open();
// document.newPage();//新创建一页来存放后面生成的表格
BaseFont bfCN = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
// 表格的字体
Font headFont = new Font(bfCN, 11f, Font.BOLD, BaseColor.BLACK);
Font textFont = new Font(bfCN, 9f, Font.NORMAL, BaseColor.BLACK);
//正文的字体
Font headTxtFont = new Font(bfCN, 16f, Font.BOLD, BaseColor.BLACK);//标题
Font TxtFont = new Font(bfCN, 11f, Font.NORMAL, BaseColor.BLACK);//正文
Font Fwhite = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Fwhite.setColor(BaseColor.WHITE);
/** 向文档中添加内容,创建段落对象 */
Paragraph titleParagraph = new Paragraph();
Chunk chunk = new Chunk("应收账款转让通知书",headTxtFont);
titleParagraph.setAlignment(Element.ALIGN_CENTER);
titleParagraph.add(chunk);
document.add(titleParagraph);
//备注
Paragraph remarkParagraph = new Paragraph();
Chunk chunk1 = new Chunk("(适用于供应商通知,每笔应收账款债权出具本通知书一式叁份)",textFont);
remarkParagraph.setAlignment(Element.ALIGN_CENTER);
remarkParagraph.add(chunk1);
document.add(remarkParagraph);
//加入空行
Paragraph blankRow31 = new Paragraph(18f, " ", headTxtFont);
document.add(blankRow31);
//编号 *****此处需要填充编号
Paragraph noParagraph = new Paragraph();
Chunk chunk2 = new Chunk("编号:"+params.get("receNo"),TxtFont);
noParagraph.setIndentationLeft(280);
noParagraph.add(chunk2);
document.add(noParagraph);
document.add(blankRow31);
Paragraph bodyParagraph = new Paragraph();
Chunk chunk3 = new Chunk("致:中国建筑第七工程局有限公司(以下简称“贵公司”)",TxtFont);
bodyParagraph.setSpacingAfter(5);
bodyParagraph.setIndentationLeft(50);
bodyParagraph.add(chunk3);
document.add(bodyParagraph);
//此处有回执单编号需要填 ************
Paragraph bodyParagraph1 = new Paragraph();
/*Chunk chunk4 = new Chunk("鉴于我司(供应商/债权人)已与深圳市万融保理有限公司(以下简称“保理商”)签署了编号为【"+params.get("factoAgreementNumber") +"】"
+ "的《国内保理综合服务协议》,并就下表所列应收账款债权通过保理商IT系统达成应收账款债权转让事宜。根据前述《国内保理综合服务协议》,"
+ "下列表格所述的应收款债权及因该应收账款债权享有的全部权利已由我司转让给保理商:",TxtFont);*/
Chunk chunk4 = new Chunk("鉴于我司(供应商/债权人)已与深圳市万融保理有限公司(以下简称“保理商”)签署了编号为【"+params.get("factoAgreementNumber") +"】的《国内保理综合服务协议》,"
+ "并就下表所列应收账款债权通过保理商IT系统达成应收账款债权转让事宜。根据前述《国内保理综合服务协议》,"
+ "下列表格所述的应收款债权及因该应收账款债权享有的全部权利已由我司转让给保理商:",TxtFont);
bodyParagraph1.setIndentationLeft(50);
bodyParagraph1.setIndentationRight(50);
bodyParagraph1.setSpacingAfter(10);
bodyParagraph1.setFirstLineIndent(20);
bodyParagraph1.add(chunk4);
document.add(bodyParagraph1);
document.add(blankRow31);
// 建立一个titleArr长度列的表格
PdfPTable table = new PdfPTable(titleArr.length);
// 计算'合并列'的合并相关集合
List<Integer> indexs;
// 每个'合并列'的初始下标集合
List<Integer> spanStartIndex;
// 每个'合并列'的合并数量集合
List<Integer> spanNumList;
// 取出'合并列'的所有数据计算
List<String> ListIn = new ArrayList<>();
for (String[] aList : list) {
ListIn.add(aList[1]);
}
spanNumList = getSpanNumList(ListIn);
spanStartIndex = getStartIndexList(ListIn, spanNumList);
indexs = getIndexs(list, spanNumList, spanStartIndex);
addTitle(table, titleArr, headFont);
// 加表格内容
addContent(table, list, textFont, spanNumList, spanStartIndex, indexs, document);
PdfContentByte canvas = writer.getDirectContent();
table.setTotalWidth(1000f);
table.setWidths(widths);
/**
* rowStart 0 起始行 rowEnd -1 表示全部行 xPos 表格横座标- 从左向右开始计算 yPos 表格纵座标- 从下向上开始计算
* canvas 画布
*/
document.add(table);
//添加总计
PdfPTable tableNum = new PdfPTable(2);
addNum(tableNum, listNum, textFont);
tableNum.setTotalWidth(1000f);
tableNum.setWidths(new float[] {200,400});
document.add(tableNum);
document.add(blankRow31);
//添加表格之后内容
Paragraph bodyParagraph2 = new Paragraph();
Chunk chunk5 = new Chunk("通过上述转让,保理商已成为贵公司的债权人,享有本公司原作为该应收账款债权人的所有权利和权益。现将有关具体事宜知会贵公司,敬请支持。",TxtFont);
bodyParagraph2.setIndentationLeft(50);
bodyParagraph2.setIndentationRight(50);
bodyParagraph2.setSpacingAfter(5);
bodyParagraph2.setFirstLineIndent(20);
bodyParagraph2.add(chunk5);
document.add(bodyParagraph2);
Paragraph bodyParagraph3 = new Paragraph();
Chunk chunk6 = new Chunk("本公司在此确认并同意:",TxtFont);
bodyParagraph3.setIndentationLeft(50);
bodyParagraph3.setIndentationRight(50);
bodyParagraph3.setSpacingAfter(5);
bodyParagraph3.setFirstLineIndent(20);
bodyParagraph3.add(chunk6);
document.add(bodyParagraph3);
Paragraph bodyParagraph3_1 = new Paragraph();
Chunk chunk6_1 = new Chunk("本公司仅转让基础交易合同项下本公司享有的应收账款债权及因该等应收账款债权享有的全部权利而不转让相关的义务及/或责任,"
+ "基础交易合同项下对贵公司的所有承诺、保证、义务和责任仍由本公司向贵公司履行,并且贵公司将继续收到由本公司直接开出的发票;",TxtFont);
bodyParagraph3.setIndentationLeft(50);
bodyParagraph3.setIndentationRight(50);
bodyParagraph3.setSpacingAfter(5);
bodyParagraph3.setFirstLineIndent(20);
bodyParagraph3.add(chunk6);
document.add(bodyParagraph3_1);
Paragraph bodyParagraph4 = new Paragraph();
Chunk chunk7 = new Chunk("除非取得保理商事先书面同意外,否则本公司不得从贵公司收取上述表格所提述的应收账款债权下的任何款项或就此获得清偿;",TxtFont);
bodyParagraph4.setIndentationLeft(50);
bodyParagraph4.setIndentationRight(50);
bodyParagraph4.setSpacingAfter(5);
bodyParagraph4.setFirstLineIndent(20);
bodyParagraph4.add(chunk7);
document.add(bodyParagraph4);
Paragraph bodyParagraph5 = new Paragraph();
Chunk chunk8 = new Chunk("请贵公司按与本公司签订的基础交易合同约定并经上述表格确定的付款期限和付款金额向保理商履行上述表格内相应应收账款债权项下的付款义务,"
+ "并将相应应付账款(即上表所列“转让应收账款金额合计”)全部以现金方式支付至保理商另行指定(书面或其他方式)的收款账户从而履行贵公司相应的债务。"
+ "保理商指定的收款账户为贵公司付款的唯一目标账户,未经保理商通知(书面或其他方式),账户不得增加或变更。",TxtFont);
bodyParagraph5.setIndentationLeft(50);
bodyParagraph5.setIndentationRight(50);
bodyParagraph5.setSpacingAfter(5);
bodyParagraph5.setFirstLineIndent(20);
bodyParagraph5.add(chunk8);
document.add(bodyParagraph5);
Paragraph bodyParagraph6 = new Paragraph();
Chunk chunk9 = new Chunk("本通知书可以书面纸质文件为载体并进行线下签署,或采用数据电文为载体并进行线上签署,根据上述方式签署本通知书均对本公司具有法律约束力。"
+ "若以线上形式签署,本通知书自本公司签署的电子签名完成认证之日起生效;若以线下形式签署,本通知书自本公司加盖单位公章之日起生效。本通知书不可撤销。"
+ "基础交易合同与本通知书有不一致的,包括但不限于应收账款到期日等,以本通知书为准。本通知书一式叁份,本公司、贵公司及保理商各执壹份。\r\n" +
" 特此通知。\r\n" +
"",TxtFont);
bodyParagraph6.setIndentationLeft(50);
bodyParagraph6.setIndentationRight(50);
bodyParagraph6.setSpacingAfter(5);
bodyParagraph6.setFirstLineIndent(20);
bodyParagraph6.add(chunk9);
document.add(bodyParagraph6);
document.add(blankRow31);
document.add(blankRow31);
//此处需盖章 所以需要个白色的字体 作为盖章目标
Paragraph supPerParagraph = new Paragraph();
Chunk chunk10 = new Chunk("供应商/债权人:",TxtFont);
Chunk chunkW = new Chunk("signLocation_1",TxtFont);
Chunk chunkW_1 = new Chunk(" ",TxtFont);
Chunk chunkW_2 = new Chunk("signLocation_2",TxtFont);
supPerParagraph.setIndentationLeft(280);
supPerParagraph.add(chunk10);
supPerParagraph.add(chunkW);
supPerParagraph.add(chunkW_1);
supPerParagraph.add(chunkW_2);
document.add(supPerParagraph);
document.add(blankRow31);
document.add(blankRow31);
//盖章之后的盖章时间 这里是供应商盖章**************
Paragraph supPerParagraph1 = new Paragraph();
Chunk chunk11 = new Chunk(String.valueOf(params.get("supsignDate")),TxtFont);
supPerParagraph1.setIndentationLeft(360);
supPerParagraph1.add(chunk11);
document.add(supPerParagraph1);
//添加空行
document.add(blankRow31);
document.add(blankRow31);
document.add(blankRow31);
document.add(blankRow31);
document.add(blankRow31);
document.add(blankRow31);
//下面是回执
Paragraph conPerParagraph = new Paragraph();
Chunk Hchunk = new Chunk("回 执",TxtFont);
conPerParagraph.setAlignment(Element.ALIGN_CENTER);
conPerParagraph.add(Hchunk);
document.add(conPerParagraph);
document.add(blankRow31);
//下面是回执编号 此处需要填充回执单编号 *****************
Paragraph conPerParagraphNo = new Paragraph();
Chunk Hchunk1 = new Chunk("编号:【"+params.get("receiptNumber")+"】",TxtFont);
conPerParagraphNo.setIndentationLeft(280);
conPerParagraphNo.add(Hchunk1);
document.add(conPerParagraphNo);
//这里是供应商名称 ********************
Paragraph conPerParagraph2 = new Paragraph();
Chunk Hchunk2 = new Chunk("致:"+params.get("supName")+"[供应商名称]" ,TxtFont);
conPerParagraph2.setIndentationLeft(50);
conPerParagraph2.setIndentationRight(50);
conPerParagraph2.setSpacingAfter(5);
conPerParagraph2.add(Hchunk2);
document.add(conPerParagraph2);
//此处需要填写编号 *********************
Paragraph conPerParagraph3 = new Paragraph();
Chunk Hchunk3 = new Chunk("鉴于本公司已收到编号为【"+params.get("receNo")+"】的《应收账款转让通知书》,知晓其中所列应收账款债权已转让予深圳市万融保理有限公司(简称“保理商”)的事实。" ,TxtFont);
conPerParagraph3.setIndentationLeft(50);
conPerParagraph3.setIndentationRight(50);
conPerParagraph3.setSpacingAfter(5);
conPerParagraph3.setFirstLineIndent(20);
conPerParagraph3.add(Hchunk3);
document.add(conPerParagraph3);
Paragraph conPerParagraph4 = new Paragraph();
Chunk Hchunk4 = new Chunk("本公司在此不可撤销地承诺和确认,应收账款债权真实、合法、有效,且具有公允的基础交易关系,且原始债权人已完全适当履行了应收账款债权对应的基础交易合同项下的义务,本公司对该等应付账款负有无条件付款义务,"
+ "本公司对应收账款债权承担的到期付款义务不以任何理由(包括但不限于商业纠纷)抗辩,不享有任何扣减、减免或抵销全部或部分应付账款的权利(为避免疑义,前述扣减、"
+ "减免或抵销应付款的权利包括但不限于本公司因原始债权人提供的产品、服务或工程不符合法定或约定的产品、服务或工程质量要求而对应付款主张扣减、减免或抵销的权利),"
+ "直至应收账款债权获得全部清偿。" ,TxtFont);
conPerParagraph4.setIndentationLeft(50);
conPerParagraph4.setIndentationRight(50);
conPerParagraph4.setSpacingAfter(5);
conPerParagraph4.setFirstLineIndent(20);
conPerParagraph4.add(Hchunk4);
document.add(conPerParagraph4);
Paragraph conPerParagraph4_1 = new Paragraph();
Chunk Hchunk4_1 = new Chunk("本公司将于应收账款到期日前10天履行应收账款债权(即上表所列“转让应收账款金额合计”)对应金额的划付义务,直至应收账款债权获得全部清偿。" ,TxtFont);
conPerParagraph4_1.setIndentationLeft(50);
conPerParagraph4_1.setIndentationRight(50);
conPerParagraph4_1.setSpacingAfter(5);
conPerParagraph4_1.setFirstLineIndent(20);
conPerParagraph4_1.add(Hchunk4_1);
document.add(conPerParagraph4_1);
Paragraph conPerParagraph5 = new Paragraph();
Chunk Hchunk5 = new Chunk("本公司对上述应收账款的支付应当以现金方式。" ,TxtFont);
conPerParagraph5.setIndentationLeft(50);
conPerParagraph5.setIndentationRight(50);
conPerParagraph5.setSpacingAfter(5);
conPerParagraph5.setFirstLineIndent(20);
conPerParagraph5.add(Hchunk5);
document.add(conPerParagraph5);
document.add(blankRow31);
Paragraph conPerParagraph5_1 = new Paragraph();
Chunk Hchunk5_1 = new Chunk("本公司确认,对基础交易合同中约定限制应收账款转让的条款(如有),本公司确认允许应收账款转让。" ,TxtFont);
conPerParagraph5_1.setIndentationLeft(50);
conPerParagraph5_1.setIndentationRight(50);
conPerParagraph5_1.setSpacingAfter(5);
conPerParagraph5_1.setFirstLineIndent(20);
conPerParagraph5_1.add(Hchunk5_1);
document.add(conPerParagraph5_1);
Paragraph conPerParagraph6 = new Paragraph();
Chunk Hchunk6 = new Chunk("本《回执》可以书面纸质文件为载体并进行线下签署,或采用数据电文为载体并进行线上签署,根据上述方式签署本《回执》均对本公司具有法律约束力。"
+ "若以线上形式签署,本《回执》自本公司签署的电子签名完成认证之日起生效;若以线下形式签署,本《回执》自本公司加盖单位公章之日起生效。"
+ "基础交易合同与本《回执》有不一致的,包括但不限于应收账款到期日等,以本《回执》为准。" ,TxtFont);
conPerParagraph6.setIndentationLeft(50);
conPerParagraph6.setIndentationRight(50);
conPerParagraph6.setSpacingAfter(5);
conPerParagraph6.setFirstLineIndent(20);
conPerParagraph6.add(Hchunk6);
document.add(conPerParagraph6);
Paragraph conPerParagraph7 = new Paragraph();
Chunk Hchunk7 = new Chunk("本《回执》一式叁份,本公司、贵公司、保理商各执壹份。\r\n" +
"特此。\r\n" +
"" ,TxtFont);
conPerParagraph7.setIndentationLeft(70);
conPerParagraph7.setIndentationRight(70);
conPerParagraph7.setSpacingAfter(5);
conPerParagraph7.add(Hchunk7);
document.add(conPerParagraph7);
document.add(blankRow31);
document.add(blankRow31);
Paragraph conPerParagraph8 = new Paragraph();
Chunk Hchunk8 = new Chunk("应收账款债务人:" ,TxtFont);
Chunk HchunkW = new Chunk("signLocation_3" ,TxtFont);
Chunk HchunkW_1 = new Chunk("(公章)" ,TxtFont);
conPerParagraph8.setIndentationLeft(280);
conPerParagraph8.add(Hchunk8);
conPerParagraph8.add(HchunkW);
conPerParagraph8.add(HchunkW_1);
document.add(conPerParagraph8);
//***************
Paragraph conPerParagraph9 = new Paragraph();
Chunk Hchunk9 = new Chunk("conssignDate" ,TxtFont);
conPerParagraph9.setIndentationLeft(320);
conPerParagraph9.add(Hchunk9);
document.add(conPerParagraph9);
System.out.println("===============PDF导出成功=============");
document.close();
//临时文件转byte数组
byte[] file2Bytes = File2Byte.file2Bytes(path);
//删除临时文件
boolean deleteFile = File2Byte.deleteFile(path);
return file2Bytes;
}
private static void addContent(PdfPTable table, List<String[]> list, Font textFont, List<Integer> spanNumList,
List<Integer> spanStartIndex, List<Integer> indexs, Document document) throws DocumentException {
// 表格数据内容
for (int i = 0; i < list.size(); i++) {
String[] str = list.get(i);
// 第1列
Paragraph paragraph01 = new Paragraph(str[0], textFont);
paragraph01.setAlignment(1);
PdfPCell cell = new PdfPCell();
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);// 然并卵
cell.setPaddingTop(-2f);// 把字垂直居中
cell.setPaddingBottom(8f);// 把字垂直居中
cell.addElement(paragraph01);
table.addCell(cell);
// 第2列
Paragraph paragraph02 = new Paragraph(str[1], textFont);
paragraph02.setAlignment(1);
PdfPCell cell1 = new PdfPCell();
cell1.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);// 然并卵
cell1.setPaddingTop(-2f);// 把字垂直居中
cell1.setPaddingBottom(8f);// 把字垂直居中
cell1.addElement(paragraph02);
setRowsSpan(indexs, spanStartIndex, spanNumList, table, cell1, i);
// 第3列
Paragraph paragraph03 = new Paragraph(str[2], textFont);
paragraph03.setAlignment(1);
PdfPCell cell2 = new PdfPCell();
cell2.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell2.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);// 然并卵
cell2.setPaddingTop(-2f);// 把字垂直居中
cell2.setPaddingBottom(8f);// 把字垂直居中
cell2.addElement(paragraph03);
setRowsSpan(indexs, spanStartIndex, spanNumList, table, cell2, i);
// 第4列
Paragraph paragraph04 = new Paragraph(str[3], textFont);
paragraph04.setAlignment(1);
PdfPCell cell3 = new PdfPCell();
cell3.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell3.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);// 然并卵
cell3.setPaddingTop(-2f);// 把字垂直居中
cell3.setPaddingBottom(8f);// 把字垂直居中
cell3.addElement(paragraph04);
setRowsSpan(indexs, spanStartIndex, spanNumList, table, cell3, i);
// 第5列
Paragraph paragraph05 = new Paragraph(str[4], textFont);
paragraph05.setAlignment(1);
PdfPCell cell4 = new PdfPCell();
cell4.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell4.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);// 然并卵
cell4.setPaddingTop(-2f);// 把字垂直居中
cell4.setPaddingBottom(8f);// 把字垂直居中
cell4.addElement(paragraph05);
setRowsSpan(indexs, spanStartIndex, spanNumList, table, cell4, i);
// 第6列
Paragraph paragraph06 = new Paragraph(str[5], textFont);
paragraph06.setAlignment(1);
PdfPCell cell5 = new PdfPCell();
cell5.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell5.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);// 然并卵
cell5.setPaddingTop(-2f);// 把字垂直居中
cell5.setPaddingBottom(8f);// 把字垂直居中
cell5.addElement(paragraph06);
table.addCell(cell5);
// 第7列
Paragraph paragraph07 = new Paragraph(str[6], textFont);
paragraph07.setAlignment(1);
PdfPCell cell6 = new PdfPCell();
cell6.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell6.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);// 然并卵
cell6.setPaddingTop(-2f);// 把字垂直居中
cell6.setPaddingBottom(8f);// 把字垂直居中
cell6.addElement(paragraph07);
table.addCell(cell6);
// 第8列
Paragraph paragraph08 = new Paragraph(str[7], textFont);
paragraph08.setAlignment(1);
PdfPCell cell7 = new PdfPCell();
cell7.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell7.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);// 然并卵
cell7.setPaddingTop(-2f);// 把字垂直居中
cell7.setPaddingBottom(8f);// 把字垂直居中
cell7.addElement(paragraph08);
table.addCell(cell7);
// 第9列
Paragraph paragraph09 = new Paragraph(str[8], textFont);
paragraph09.setAlignment(1);
PdfPCell cell8 = new PdfPCell();
cell8.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell8.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);// 然并卵
cell8.setPaddingTop(-2f);// 把字垂直居中
cell8.setPaddingBottom(8f);// 把字垂直居中
cell8.addElement(paragraph09);
table.addCell(cell8);
// 第10列
Paragraph paragraph10 = new Paragraph(str[9], textFont);
paragraph10.setAlignment(1);
PdfPCell cell9 = new PdfPCell();
cell9.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell9.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);// 然并卵
cell9.setPaddingTop(-2f);// 把字垂直居中
cell9.setPaddingBottom(8f);// 把字垂直居中
cell9.addElement(paragraph10);
table.addCell(cell9);
}
}
/**
* 设置恰当位置的setRowSpan属性
*
* @param indexs 不加单元格的索引集合
* @param spanStartIndex 要加的单元格开始索引集合
* @param spanNumList 要加的单元格合并的值集合
* @param table table
* @param cell cell
* @param i 索引
*/
private static void setRowsSpan(List<Integer> indexs, List<Integer> spanStartIndex, List<Integer> spanNumList,
PdfPTable table, PdfPCell cell, int i) {
if (indexs != null) {
Boolean isAllNotEqual = true;
for (Integer index : indexs) {
if (i == index) {
isAllNotEqual = false;
}
}
// 没有不加的下标
if (isAllNotEqual) {
// 判断在哪里设置span值
if (spanStartIndex != null) {
Boolean isSpan = false;
int copyJ = 0;
for (int j = 0; j < spanStartIndex.size(); j++) {
if (i == spanStartIndex.get(j)) {
isSpan = true;
copyJ = j;
break;
}
}
if (isSpan) {
int spanNum = spanNumList.get(copyJ);
cell.setRowspan(spanNum);
}
}
table.addCell(cell);
}
} else {
table.addCell(cell);
}
}
private static void addTitle(PdfPTable table, String[] titleArr, Font headFont) {
for (String aTitleArr : titleArr) {
Paragraph p = new Paragraph(aTitleArr, headFont);
PdfPCell cell = new PdfPCell();
p.setAlignment(1);
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);// 然并卵
cell.setPaddingTop(-2f);// 把字垂直居中
cell.setPaddingBottom(8f);// 把字垂直居中
cell.addElement(p);
table.addCell(cell);
}
}
private static void addNum(PdfPTable table,List<String[]> list, Font textFont) {
for (int i = 0; i < list.size(); i++) {
String[] str = list.get(i);
if(i == 1) {
// 第1列
Paragraph paragraph03 = new Paragraph(str[0], textFont);
paragraph03.setAlignment(1);
PdfPCell cell2 = new PdfPCell();
cell2.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell2.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);// 然并卵
cell2.setPaddingTop(-2f);// 把字垂直居中
cell2.setPaddingBottom(5f);// 把字垂直居中
cell2.setFixedHeight(30);
cell2.addElement(paragraph03);
table.addCell(cell2);
// 第2列
Paragraph paragraph04 = new Paragraph(str[1], textFont);
paragraph04.setAlignment(1);
PdfPCell cell3 = new PdfPCell();
cell3.setPaddingTop(-2f);// 把字垂直居中
cell3.setPaddingBottom(5f);// 把字垂直居中
cell2.setFixedHeight(30);
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell3.addElement(paragraph04);
table.addCell(cell3);
}else {
// 第1列
Paragraph paragraph01 = new Paragraph(str[0], textFont);
paragraph01.setAlignment(1);
PdfPCell cell = new PdfPCell();
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);// 然并卵
cell.setPaddingTop(-2f);// 把字垂直居中
cell.setPaddingBottom(5f);// 把字垂直居中
cell.addElement(paragraph01);
table.addCell(cell);
// 第2列
Paragraph paragraph02 = new Paragraph(str[1], textFont);
paragraph02.setAlignment(1);
PdfPCell cell1 = new PdfPCell();
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell1.setPaddingTop(-2f);// 把字垂直居中
cell1.setPaddingBottom(5f);// 把字垂直居中
cell1.addElement(paragraph02);
table.addCell(cell1);
}
}
}
/**
* 获取某列要合并的单元格数量
*
* @param list
* @return
*/
private static List<Integer> getSpanNumList(List<String> list) {
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
if (list.size() > 0) {
for (int i = 0; i < list.size(); i = i + 1) {
map.put(list.get(i), i);
}
}
// 修改对应key值的value
Set<String> s = map.keySet();// 获取KEY集合
List<String> strings = new ArrayList<>(s);
int spanNum = 0;
for (String string : strings) {
int tmpSpanNum = map.get(string) - spanNum + 1;
map.put(string, tmpSpanNum);
spanNum += tmpSpanNum;
}
List<Integer> res = new ArrayList<>();
for (String string : strings) {
res.add(map.get(string));
}
return res;
}
/**
* 获取某列要合并的单元格初始下标
*
* @param list
* @param resSpanNumList
* @return
*/
private static List<Integer> getStartIndexList(List<String> list, List<Integer> resSpanNumList) {
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
if (list.size() > 0) {
for (int i = 0; i < list.size(); i = i + 1) {
map.put(list.get(i), i);
}
}
Set<String> keys = map.keySet();// 获取KEY集合
List<String> stringKeys = new ArrayList<>(keys);
List<Integer> res = new ArrayList<>();
if (stringKeys.size() == resSpanNumList.size()) {
for (int i = 0; i < stringKeys.size(); i++) {
res.add(map.get(stringKeys.get(i)) - resSpanNumList.get(i) + 1);
}
}
return res;
}
/**
* 不加某字段的索引集合
*
* @param list
* @param resSpanNumList
* @param resStartIndexList
* @return
*/
private static List<Integer> getIndexs(List<String[]> list, List<Integer> resSpanNumList,
List<Integer> resStartIndexList) {
List<Integer> indexs = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < resStartIndexList.size(); j++) {
if (i == resStartIndexList.get(j)) {
for (int k = i + 1; k < i + resSpanNumList.get(j); k++) {
indexs.add(k);
}
}
}
}
return indexs;
}
}