在對接某第三方XX平臺時需要對錶格傳入PDF文件類型。於是自造了一個。
Maven使用版本如下:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.1</version>
</dependency>
支持中文的一個jar包
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
最終效果:
以下是全部的代碼實現以及說明:
package com.qingzu.applet.util;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.stereotype.Component;
import java.io.FileOutputStream;
public class CreatePDFUtils1 {
/**
*新建以下兩個方法,創建表格內的字體和樣式的方法
* @param str 內容
* @param font 字體對象
* @param high 表格高度
* @Param alignCenter 是否水平居中
* @Param alignMidde 是否垂直居中
* @return
*/
private static PdfPCell mircoSoftFont(String str,Font font,int high,boolean alignCenter,boolean alignMidde){
PdfPCell pdfPCell = new PdfPCell(new Phrase(str,font));
pdfPCell.setMinimumHeight(high);
pdfPCell.setUseAscender(true); // 設置可以居中
if (alignCenter){
pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); // 設置水平居中
}
if (alignMidde){
pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 設置垂直居中
}
return pdfPCell;
}
/**
*
* @param str 字符串
* @param font 字體
* @param high 表格高度
* @Param alignCenter 是否水平居中
* @Param alignMidde 是否垂直居中
* @Param haveColor 是否有背景色(灰色)
* @return
*/
private static PdfPCell mircoSoftFont(String str,Font font,int high,boolean alignCenter,boolean alignMidde,boolean haveColor){
PdfPCell pdfPCell = new PdfPCell(new Phrase(str,font));
pdfPCell.setMinimumHeight(high);
pdfPCell.setUseAscender(true); // 設置可以居中
if (alignCenter){
pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); // 設置水平居中
}
if (alignMidde){
pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 設置垂直居中
}
if (haveColor){
//顏色代碼 RGB
pdfPCell.setBackgroundColor(new BaseColor(217,217,217));
}
return pdfPCell;
}
public static void createHardwarePDF( String outputPath)throws Exception{
//新建文檔對象,頁大小爲A4紙,然後設置4個邊距
Document document = new Document(PageSize.A4,20,20,30,30);
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(outputPath));
document.open();
//創建字體
BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
//字體對象
Font size14font = new Font(baseFont,14,Font.NORMAL); //大小爲14的正常字體
Font size10font = new Font(baseFont,10,Font.BOLD); //大小爲10的粗體
//添加標題
PdfPTable tableName = new PdfPTable(1);
tableName.setWidthPercentage(90); //設置標題長度佔紙張比例
tableName.addCell(mircoSoftFont("個人信息",size14font,50,true,true));
document.add(tableName);
//添加第二行的數據
PdfPTable secondRowTable = new PdfPTable(3); //三列的意思
secondRowTable.setWidthPercentage(90);
//這裏的數組長度是上面創建的列數,數組的總和爲1,就是按比例劃分的意思
secondRowTable.setTotalWidth(new float[]{0.18f,0.32f,0.5f});
secondRowTable.addCell(mircoSoftFont(" 姓名: ",size10font,50,false,true));
secondRowTable.addCell(mircoSoftFont("李曉明",size10font,50,false,true));
secondRowTable.addCell(mircoSoftFont(" 出生日期: 1994年3月14日",size10font,50,false,true));
document.add(secondRowTable);
//第三行數據
PdfPTable thirdRowTable = new PdfPTable(3);
thirdRowTable.setWidthPercentage(90);
thirdRowTable.setTotalWidth(new float[]{0.18f,0.32f,0.5f});
thirdRowTable.addCell(mircoSoftFont(" 名族:",size10font,50,false,true));
thirdRowTable.addCell(mircoSoftFont("漢族",size10font,50,false,true));
thirdRowTable.addCell(mircoSoftFont(" 聯繫電話: 13888880000",size10font,50,false,true));
document.add(thirdRowTable);
//第四行數據
PdfPTable fourthRowTable = new PdfPTable(2);
fourthRowTable.setWidthPercentage(90);
fourthRowTable.setTotalWidth(new float[]{0.66f,0.34f});
fourthRowTable.addCell(mircoSoftFont(" 個人描述 :",size10font,175,false,false));
fourthRowTable.addCell(mircoSoftFont("個人特長 :",size10font,175,false,false));
document.add(fourthRowTable);
//第五行
PdfPTable fifthDetailName = new PdfPTable(1);
fifthDetailName.setWidthPercentage(90);
fifthDetailName.addCell(mircoSoftFont("獲獎記錄 :",size14font,50,true,true));
document.add(fifthDetailName);
//第六行
PdfPTable sisthRowTalbe= new PdfPTable(1);
sisthRowTalbe.setWidthPercentage(90);
sisthRowTalbe.addCell(mircoSoftFont(" 聯繫地址: "+"廣東省廣州市天河區XXXXXXXXXXXXXXXXXX",size10font,50,false,true));
document.add(sisthRowTalbe);
PdfPTable seventhRowTalbe = new PdfPTable(1);
seventhRowTalbe.setWidthPercentage(90);
seventhRowTalbe.addCell(mircoSoftFont(" 畢業院校 ",size14font,60,true,true));document.add(seventhRowTalbe);
//第八行
PdfPTable eiththRowTalbe = new PdfPTable(3);
eiththRowTalbe.setWidthPercentage(90);
eiththRowTalbe.setTotalWidth(new float[]{0.3f,0.5f,0.2f});
eiththRowTalbe.addCell(mircoSoftFont(" 畢業學校",size10font,50,true,true,true));
eiththRowTalbe.addCell(mircoSoftFont(" 就讀日期",size10font,50,true,true,true));
eiththRowTalbe.addCell(mircoSoftFont(" 聯繫人",size10font,50,true,true,true));
document.add(eiththRowTalbe);
//接下來加List
String school = "XXX學校";
String time = "201909 - 2022-06";
String name = "陳某";
for (int i = 0 ;i<4 ;i++){
PdfPTable tempTable = new PdfPTable(3);
tempTable.setWidthPercentage(90);
tempTable.setTotalWidth(new float[]{0.3f,0.5f,0.2f});
tempTable.addCell(mircoSoftFont(school,size10font,50,true,true));
tempTable.addCell(mircoSoftFont(time,size10font,50,true,true));
tempTable.addCell(mircoSoftFont(name,size10font,50,true,true));
document.add(tempTable);
}
document.close();
writer.close();
}
//使用例子
public static void main(String[] args)throws Exception{
createHardwarePDF("test.pdf");
}
}
完成。