日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

SpringBoot使用itext導出pdf(含圖片和表格)

作者:Java--初學者 更新時間: 2024-07-15 編程語言

1.所需關鍵依賴

<!--pdf導出工具依賴-->
<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>itextpdf</artifactId>
	<version>5.5.13</version>
</dependency>

<!--解決中文字體不顯示問題-->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-asian</artifactId>
    <version>5.2.0</version>
</dependency>

2.方法參數如下

//file1和file2為照片
public void exportPdf(MultipartFile file1, MultipartFile file2, HttpServletResponse response)

3.開始畫文檔

step1.創建一個pdf文檔,關聯一個OutputStream,打開文檔
//新建一個A4大小的文檔
Document document = new Document(PageSize.A4);

//創建一個ByteArrayOutputStream綁定document,后續只需要將數據流寫入baos即可
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);

//打開文檔
document.open();
step2.解決中文字體不顯示問題
// 設置中文字體
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

//文檔標題字體樣式
Font titleFontChinese = new Font(bfChinese, 15, Font.BOLD);

//小標題字體樣式
Font smallTitleFontChinese = new Font(bfChinese, 8, Font.BOLD);

//表頭字體樣式
Font headFontChinese = new Font(bfChinese, 8, Font.BOLD);

//內容字體樣式
Font contentFontChinese = new Font(bfChinese, 9, Font.NORMAL);
step3.設置文檔標題
代碼:
//這一步將step2中的titleFontChinese設置進來即可解決中文字體不顯示問題
Paragraph title = new Paragraph("文檔標題", titleFontChinese);

//標題居中
title.setAlignment(Element.ALIGN_CENTER);

//將標題添加到文檔中
document.add(title);
效果:?

step4.插入第一張圖片
代碼:
//保持美觀,空一行
document.add(new Paragraph(" "));

//設置小標題
document.add(new Paragraph("圖片一", smallTitleFontChinese));

// 添加第一張圖片到PDF
Image img1 = Image.getInstance(file1.getBytes());

//重新設置圖片大小,適配PDF
img1.scaleToFit(525, img1.getHeight());

//將圖片添加進PDF
document.add(img1);
效果:?

step5.插入第二張圖片并在右側插入一個表格
代碼:
//這個地方的基礎思路還是利用表格實現

//添加一行間隔
document.add(new Paragraph(" "));

//創建一個包含兩列的表格,這個表格里面左列是圖片,右列是表格
PdfPTable table = new PdfPTable(2);

//設置表格寬度
table.setWidthPercentage(100);

//希望最終圖片的大小小于右邊的表格,需要劃分兩列比例
table.setWidths(new int[]{1, 2});


//第一列添加圖片
PdfPCell imageCell = new PdfPCell();
Image img2 = Image.getInstance(file2.getBytes());
//設置內部表格寬度為100%
img2.setWidthPercentage(100);
imageCell.addElement(img2);
//設置圖片水平居中
imageCell.setHorizontalAlignment(Element.ALIGN_CENTER); 
//設置圖片垂直居中
imageCell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
//設置單元格無邊框
imageCell.setBorder(Rectangle.NO_BORDER); 
//將帶圖片的單元格設置進入表格中
table.addCell(imageCell);


//第二列添加表格
//內部表格,包含四列
PdfPTable innerTable = new PdfPTable(4);
//設置內部表格寬度為100%
innerTable.setWidthPercentage(100);
//劃分表格每一列比例
innerTable.setWidths(new float[]{1f, 1.5f, 1.5f, 6f});

// 添加表頭
PdfPCell cell;
//表頭單元格背景色
BaseColor lightGray = new BaseColor(208, 223, 230);

//設置文本
cell = new PdfPCell(new Phrase("姓名", headFontChinese));
//設置背景色
cell.setBackgroundColor(lightGray);
//水平居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
//單元格最小高度
cell.setMinimumHeight(30);
//垂直居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
innerTable.addCell(cell);

cell = new PdfPCell(new Phrase("性別", headFontChinese));
cell.setBackgroundColor(lightGray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setMinimumHeight(30);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
innerTable.addCell(cell);

cell = new PdfPCell(new Phrase("年齡", headFontChinese));
cell.setBackgroundColor(lightGray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setMinimumHeight(30);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
innerTable.addCell(cell);

cell = new PdfPCell(new Phrase("學號", headFontChinese));
cell.setBackgroundColor(lightGray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setMinimumHeight(30);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
innerTable.addCell(cell);

// 添加數據就和添加表頭差不多,只不過headFontChinese改為contentFontChinese,并且不要設置背景色,可以繼續復用cell對象,并使用innerTable.addCell(cell);繼續往后面添加單元格即可


// 將內部表格添加到外部表格的單元格中
PdfPCell tableCell = new PdfPCell(innerTable);

// 設置單元格無邊框,否則內部表格會有兩個邊框
tableCell.setBorder(Rectangle.NO_BORDER); 

//將含有內部表格的單元格設置進入外部表格中
table.addCell(tableCell);

//將外部表格添加到文檔中
document.add(table);
效果:

step6.插入最后兩個表格
代碼:
//添加間隔
document.add(new Paragraph(" "));
//設置小標題
Paragraph paragraph1 = new Paragraph("表格", smallTitleFontChinese);
//防止小標題和表格挨得太近,在小標題之后設置一些間距
paragraph1.setSpacingAfter(8f);
document.add(paragraph1);

//創建一個包含五列的表格
PdfPTable newTable = new PdfPTable(5);
//設置表格寬度為100%
newTable.setWidthPercentage(100); 
//設置列寬比例
newTable.setWidths(new float[]{0.8f, 1f, 1f, 1f, 6.2f});

// 添加表頭和添加內容的基本代碼邏輯與innertable一致,不做贅述

// 將新表格添加到文檔中
document.add(newTable);

//第二張表格和第一張表格代碼一致,不做贅述
效果:

step7.將PDF數據流導出
//設置響應頭和內容格式
response.setContentType("application/pdf");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=export.pdf");

//關閉文檔
document.close();

//將PDF內容寫入響應輸出流并關閉
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(baos.toByteArray());
outputStream.flush();
outputStream.close();

?tips.如何設置表格中字體顏色
//首先預設置幾種顏色樣式,并保持除顏色外,其它樣式和其它內容一致
//紅色
Font redFont = new Font(bfChinese, contentFontChinese.getSize(), contentFontChinese.getStyle(), BaseColor.RED);
//綠色
Font greenFont = new Font(bfChinese, contentFontChinese.getSize(), contentFontChinese.getStyle(), BaseColor.GREEN);
//黃色
Font yellowFont = new Font(bfChinese, contentFontChinese.getSize(), contentFontChinese.getStyle(), new BaseColor(254,186,7));


//根據實際情況將cell對象重新賦值
if(Condition 1){
    cell = new PdfPCell(new Phrase("原文本", greenFont));
}

if(Condition 2){
    cell = new PdfPCell(new Phrase("原文本", redFont ));
}

if(Condition 3){
    cell = new PdfPCell(new Phrase("原文本", yellowFont));
}

//最后還是將cell設置進入table即可
table.addCell(cell);

4.最終效果?

原文鏈接:https://blog.csdn.net/weixin_56637697/article/details/140330831

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新