SpringBoot集成iTextPDF的實(shí)例
SpringBoot集成iTextPDF
依賴
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>注意:不加下面這個(gè)依賴無(wú)法設(shè)置中文
使用步驟
- 先創(chuàng)建
Document對(duì)象,該對(duì)象是PDF文檔,可以進(jìn)行一些屬性設(shè)置 PdfPTable是表格對(duì)象,用于表格對(duì)象的創(chuàng)建、管理及使用PdfPCell是具體的表格子項(xiàng)了,里面就是我們要操作寫入的對(duì)象- 將
PdfPCell對(duì)象加入到PdfPTable對(duì)象中,PdfPTable對(duì)象再加入到Document對(duì)象中,便完成了文檔的創(chuàng)建
基本屬性
文檔大?。?/strong>
由Document對(duì)象的多個(gè)重載構(gòu)造器決定。
Document(); // 默認(rèn)頁(yè)面大小是A4 Document(PageSize.A4); // 指定頁(yè)面大小為A4 Document(PageSize.A4,50,50,30,20); // 指定頁(yè)面大小為A4,且自定義頁(yè)邊距(marginLeft、marginRight、marginTop、marginBottom)
段落的設(shè)置:
由Paragraph的對(duì)象屬性決定。
Paragraph paragraph = new Paragraph(name,headfont);//設(shè)置字體樣式 paragraph.setAlignment(1);//設(shè)置文字居中 0靠左 1,居中 2,靠右 paragraph.setIndentationLeft(12);// 左縮進(jìn) paragraph.setIndentationRight(12);// 右縮進(jìn) paragraph.setFirstLineIndent(24);// 首行縮進(jìn) paragraph.setLeading(20f); //行間距 paragraph.setSpacingBefore(5f); //設(shè)置段落上空白 paragraph.setSpacingAfter(10f); //設(shè)置段落下空白
表格:
由Table的對(duì)象屬性決定。
table.setAlignment(Element.ALIGN_CENTER);//居中 table.setAutoFillEmptyCells(true);//自動(dòng)填滿 table.setBorderWidth((float)0.1);//表格邊框線條寬度 table.setPadding(1);//邊距:單元格的邊線與單元格內(nèi)容的邊距 table.setSpacing(0);//間距:?jiǎn)卧衽c單元格之間的距離
cell:
由Cell的對(duì)象屬性決定。
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
代碼
@RequestMapping(value = "ef/pdf")
public void pdfController() throws IOException, DocumentException {
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
response.setHeader("content-Type", "application/pdf");
// 下載文件的默認(rèn)名稱
response.setHeader("Content-Disposition", "attachment;filename=test.pdf");
Document document = new Document();
try {
PdfWriter.getInstance(document, response.getOutputStream());
} catch (DocumentException e) {
e.printStackTrace();
}
document.open();
document.setPageCount(2);
document.addTitle("Personal Grade Browser");// 標(biāo)題
document.addAuthor("Jack.Edward");// 作者
document.addSubject("Personal Grade of Jack.Edward");// 主題
document.addKeywords("Simulator");// 關(guān)鍵字
document.addCreator("Kicinio");// 創(chuàng)建者
Image image =Image.getInstance("/xp.png");
List<String> titleList = new ArrayList<>();
titleList.add("Literature");
titleList.add("Math");
titleList.add("English");
for(int i = 0; i < 10; i++){
PdfPTable tableContent = new PdfPTable(titleList.size());
if(i == 0){
PdfPTable tableTitle = new PdfPTable(titleList.size());
PdfPCell cellOne = new PdfPCell();
cellOne.setPhrase(new Paragraph(titleList.get(0)));
cellOne.setBackgroundColor(BaseColor.LIGHT_GRAY);
cellOne.setHorizontalAlignment(Element.ALIGN_CENTER);
tableTitle.addCell(cellOne);
PdfPCell cellTwo = new PdfPCell();
cellTwo.setPhrase(new Paragraph(titleList.get(1)));
cellTwo.setBackgroundColor(BaseColor.LIGHT_GRAY);
cellTwo.setHorizontalAlignment(Element.ALIGN_CENTER);
tableTitle.addCell(cellTwo);
PdfPCell cellThree = new PdfPCell();
cellThree.setPhrase(new Paragraph(titleList.get(2)));
cellThree.setBackgroundColor(BaseColor.LIGHT_GRAY);
cellTwo.setHorizontalAlignment(Element.ALIGN_CENTER);
tableTitle.addCell(cellThree);
document.add(tableTitle);
}
Random randomGrade = new Random();
PdfPCell cell = new PdfPCell();
cell = new PdfPCell();
cell.setPhrase(new Paragraph(String.valueOf(randomGrade.nextInt(100))));
tableContent.addCell(cell);
document.add(tableContent);
cell = new PdfPCell();
cell.setPhrase(new Paragraph(String.valueOf(randomGrade.nextInt(100))));
tableContent.addCell(cell);
cell.setBorderWidth(20);
document.add(tableContent);
cell = new PdfPCell();
cell.setPhrase(new Paragraph(String.valueOf(randomGrade.nextInt(100))));
// cell.setImage(image);
tableContent.addCell(cell);
document.add(tableContent);
}
document.close();
}效果

- 加上圖片之后:

有興趣的讀者可自行美化
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Springboot整合itext實(shí)現(xiàn)PDF文件合并
- SpringBoot3集成iText實(shí)現(xiàn)PDF導(dǎo)出功能
- SpringBoot整合iText7導(dǎo)出PDF及性能優(yōu)化方式
- SpringBoot使用itext填充pdf表單及導(dǎo)出pdf的流程
- SpringBoot集成itext實(shí)現(xiàn)html轉(zhuǎn)PDF
- SpringBoot集成itextpdf實(shí)現(xiàn)根據(jù)模板動(dòng)態(tài)生成PDF
- SpringBoot使用iText7實(shí)現(xiàn)將HTML轉(zhuǎn)成PDF并添加頁(yè)眉頁(yè)腳水印
- SpringBoot集成itext導(dǎo)出PDF的過程
相關(guān)文章
Skywalking改成適配阿里云等帶Http?Basic的Elasticsearch服務(wù)
這篇文章主要介紹了改造Skywalking支持阿里云等帶Http?Basic的Elasticsearch服務(wù)2022-02-02
RabbitMq消息防丟失功能實(shí)現(xiàn)方式講解
這篇文章主要介紹了RabbitMq消息防丟失功能實(shí)現(xiàn),RabbitMQ中,消息丟失可以簡(jiǎn)單的分為兩種:客戶端丟失和服務(wù)端丟失。針對(duì)這兩種消息丟失,RabbitMQ都給出了相應(yīng)的解決方案2023-01-01
IntelliJ IDEA打開多個(gè)Maven的module且相互調(diào)用代碼的方法
這篇文章主要介紹了IntelliJ IDEA打開多個(gè)Maven的module且相互調(diào)用代碼的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
javaweb項(xiàng)目入門實(shí)戰(zhàn)詳細(xì)指南
Java?Web開發(fā)是指使用Java技術(shù)來創(chuàng)建動(dòng)態(tài)網(wǎng)站或Web應(yīng)用程序,Java?Web開發(fā)主要使用Servlet、JSP(JavaServer?Pages)、JavaBeans等技術(shù)來實(shí)現(xiàn)動(dòng)態(tài)頁(yè)面和處理業(yè)務(wù)邏輯,這篇文章主要介紹了javaweb項(xiàng)目入門的相關(guān)資料,需要的朋友可以參考下2025-12-12
淺談Java數(shù)組的一些使用方法及堆棧存儲(chǔ)
下面小編就為大家?guī)硪黄獪\談Java數(shù)組的一些使用方法及堆棧存儲(chǔ)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07

