SpringBoot集成iTextPDF的實例
更新時間:2024年09月20日 10:43:07 作者:kicinio
SpringBoot集成iTextPDF時,創(chuàng)建PDF文檔涉及Document、PdfPTable和PdfPCell對象,設(shè)置文檔大小和頁邊距,使用Paragraph設(shè)置段落樣式,并通過Table和Cell控制表格樣式和對齊,還可加入圖片美化文檔,這些步驟對于生成具有中文內(nèi)容的PDF文件至關(guān)重要
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>注意:不加下面這個依賴無法設(shè)置中文
使用步驟
- 先創(chuàng)建
Document對象,該對象是PDF文檔,可以進行一些屬性設(shè)置 PdfPTable是表格對象,用于表格對象的創(chuàng)建、管理及使用PdfPCell是具體的表格子項了,里面就是我們要操作寫入的對象- 將
PdfPCell對象加入到PdfPTable對象中,PdfPTable對象再加入到Document對象中,便完成了文檔的創(chuàng)建
基本屬性
文檔大?。?/strong>
由Document對象的多個重載構(gòu)造器決定。
Document(); // 默認頁面大小是A4 Document(PageSize.A4); // 指定頁面大小為A4 Document(PageSize.A4,50,50,30,20); // 指定頁面大小為A4,且自定義頁邊距(marginLeft、marginRight、marginTop、marginBottom)
段落的設(shè)置:
由Paragraph的對象屬性決定。
Paragraph paragraph = new Paragraph(name,headfont);//設(shè)置字體樣式 paragraph.setAlignment(1);//設(shè)置文字居中 0靠左 1,居中 2,靠右 paragraph.setIndentationLeft(12);// 左縮進 paragraph.setIndentationRight(12);// 右縮進 paragraph.setFirstLineIndent(24);// 首行縮進 paragraph.setLeading(20f); //行間距 paragraph.setSpacingBefore(5f); //設(shè)置段落上空白 paragraph.setSpacingAfter(10f); //設(shè)置段落下空白
表格:
由Table的對象屬性決定。
table.setAlignment(Element.ALIGN_CENTER);//居中 table.setAutoFillEmptyCells(true);//自動填滿 table.setBorderWidth((float)0.1);//表格邊框線條寬度 table.setPadding(1);//邊距:單元格的邊線與單元格內(nèi)容的邊距 table.setSpacing(0);//間距:單元格與單元格之間的距離
cell:
由Cell的對象屬性決定。
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");
// 下載文件的默認名稱
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");// 標題
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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
Skywalking改成適配阿里云等帶Http?Basic的Elasticsearch服務(wù)
這篇文章主要介紹了改造Skywalking支持阿里云等帶Http?Basic的Elasticsearch服務(wù)2022-02-02
IntelliJ IDEA打開多個Maven的module且相互調(diào)用代碼的方法
這篇文章主要介紹了IntelliJ IDEA打開多個Maven的module且相互調(diào)用代碼的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02

