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