SpringBoot集成iTextPDF的實(shí)例
SpringBoot集成iTextPDF
依賴(lài)
<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è)依賴(lài)無(wú)法設(shè)置中文
使用步驟
- 先創(chuàng)建
Document
對(duì)象,該對(duì)象是PDF文檔,可以進(jìn)行一些屬性設(shè)置 PdfPTable
是表格對(duì)象,用于表格對(duì)象的創(chuàng)建、管理及使用PdfPCell
是具體的表格子項(xiàng)了,里面就是我們要操作寫(xiě)入的對(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)填滿(mǎn) 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)名稱(chēng) 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ò)程
相關(guān)文章
教你怎么用idea創(chuàng)建web項(xiàng)目
好多朋友在使用IDEA創(chuàng)建項(xiàng)目時(shí),總會(huì)碰到一些小問(wèn)題.現(xiàn)在我們就演示一下使用IDEA創(chuàng)建web項(xiàng)目的完整步驟吧.文中有非常詳細(xì)的圖文示例哦,,需要的朋友可以參考下2021-05-05SpringBoot利用validation實(shí)現(xiàn)優(yōu)雅的校驗(yàn)參數(shù)
數(shù)據(jù)的校驗(yàn)是交互式網(wǎng)站一個(gè)不可或缺的功能,如果數(shù)據(jù)庫(kù)中出現(xiàn)一個(gè)非法的郵箱格式,會(huì)讓運(yùn)維人員頭疼不已。本文將介紹如何利用validation來(lái)對(duì)數(shù)據(jù)進(jìn)行校驗(yàn),感興趣的可以跟隨小編一起學(xué)習(xí)一下2022-06-06JVM(Java?Virtual?Machine,Java虛擬機(jī))的作用詳解
JVM是Java語(yǔ)言實(shí)現(xiàn)“一次編寫(xiě),到處運(yùn)行”特性的基石,也是Java平臺(tái)的核心組成部分,其主要作用包括平臺(tái)無(wú)關(guān)性、內(nèi)存管理、運(yùn)行Java程序、安全性以及性能優(yōu)化,通過(guò)這些功能,JVM確保了Java程序的可移植性、高效性和安全性2025-03-03Java開(kāi)發(fā)深入分析講解二叉樹(shù)的遞歸和非遞歸遍歷方法
樹(shù)是一種重要的非線性數(shù)據(jù)結(jié)構(gòu),直觀地看,它是數(shù)據(jù)元素(在樹(shù)中稱(chēng)為結(jié)點(diǎn))按分支關(guān)系組織起來(lái)的結(jié)構(gòu),很象自然界中的樹(shù)那樣。樹(shù)結(jié)構(gòu)在客觀世界中廣泛存在,如人類(lèi)社會(huì)的族譜和各種社會(huì)組織機(jī)構(gòu)都可用樹(shù)形象表示,本篇介紹二叉樹(shù)的遞歸與非遞歸遍歷的方法2022-05-05SpringBoot+Redis?BitMap實(shí)現(xiàn)簽到與統(tǒng)計(jì)的項(xiàng)目實(shí)踐
最近項(xiàng)目里需要集成簽到和統(tǒng)計(jì)功能,連續(xù)簽到后會(huì)給用戶(hù)發(fā)放一些優(yōu)惠券和獎(jiǎng)品,以此來(lái)吸引用戶(hù)持續(xù)在該品臺(tái)進(jìn)行活躍,本文就詳細(xì)的介紹一下如何實(shí)現(xiàn),感興趣的可以了解一下2023-09-09MyBatis通過(guò)JDBC數(shù)據(jù)驅(qū)動(dòng)生成的執(zhí)行語(yǔ)句問(wèn)題
這篇文章主要介紹了MyBatis通過(guò)JDBC數(shù)據(jù)驅(qū)動(dòng)生成的執(zhí)行語(yǔ)句問(wèn)題的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08SpringBoot在IDEA中實(shí)現(xiàn)熱部署(JRebel實(shí)用版)
這篇文章主要介紹了SpringBoot在IDEA中實(shí)現(xiàn)熱部署(JRebel實(shí)用版),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05