Java使用iTextPDF生成PDF文件的實(shí)現(xiàn)方法
iText介紹和說明
因?yàn)轫?xiàng)目需要生成PDF文件,所以去找了一下能夠生成PDF的Java工具,看到了iText可以說好評(píng)如潮。
如果你想通過java操作PDF文件,那么 iText 絕對(duì)是你的首選。
引入依賴
這里使用的是iText5
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.10</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency>
使用步驟簡(jiǎn)單介紹
快速入手iText攏共需要5步:
- 創(chuàng)建文檔實(shí)例 Document
- 獲取PdfWriter實(shí)例 (需要指定Document實(shí)例 和pdf 生成的磁盤路徑)
- 打開文檔
- 添加段落內(nèi)容
- 關(guān)閉操作文檔實(shí)例 (操作完成后必須執(zhí)行文檔關(guān)閉操作)
創(chuàng)建工具類
public class PdfUtil { // 標(biāo)準(zhǔn)字體 public static Font NORMALFONT; // 加粗字體 public static Font BOLDFONT; //固定高 public static float fixedHeight = 27f; //間距 public static int spacing = 5; static { try { BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); NORMALFONT = new Font(bfChinese, 10, Font.NORMAL); BOLDFONT = new Font(bfChinese, 14, Font.BOLD); } catch (Exception e) { e.printStackTrace(); } } public static Document createDocument() { //生成pdf Document document = new Document(); // 頁面大小 Rectangle rectangle = new Rectangle(PageSize.A4); // 頁面背景顏色 rectangle.setBackgroundColor(BaseColor.WHITE); document.setPageSize(rectangle); // 頁邊距 左,右,上,下 document.setMargins(20, 20, 20, 20); return document; } /** * @param text 段落內(nèi)容 * @return */ public static Paragraph createParagraph(String text, Font font) { Paragraph elements = new Paragraph(text, font); elements.setSpacingBefore(5); elements.setSpacingAfter(5); elements.setSpacingAfter(spacing); return elements; } public static Font createFont(int fontNumber, int fontSize, BaseColor fontColor) { //中文字體 ----不然中文會(huì)亂碼 BaseFont bf = null; try { bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); return new Font(bf, fontNumber, fontSize, fontColor); } catch (Exception e) { e.printStackTrace(); } return new Font(bf, Font.DEFAULTSIZE, Font.NORMAL, BaseColor.BLACK); } /** * 隱藏表格邊框線 * * @param cell 單元格 */ public static void disableBorderSide(PdfPCell cell) { if (cell != null) { cell.disableBorderSide(1); cell.disableBorderSide(2); cell.disableBorderSide(4); cell.disableBorderSide(8); } } /** * 創(chuàng)建居中得單元格 * * @return */ public static PdfPCell createCenterPdfPCell() { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setFixedHeight(fixedHeight); return cell; } /** * 創(chuàng)建指定文字得單元格 * * @param text * @return */ public static PdfPCell createCenterPdfPCell(String text, int rowSpan, int colSpan, Font font) { PdfPCell cell = new PdfPCell(new Paragraph(text, font)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setFixedHeight(fixedHeight); cell.setRowspan(rowSpan); cell.setColspan(colSpan); return cell; } /** * @param len 表格列數(shù) * @return */ public static PdfPTable createPdfPTable(int len) { PdfPTable pdfPTable = new PdfPTable(len); pdfPTable.setSpacingBefore(5); pdfPTable.setHorizontalAlignment(Element.ALIGN_CENTER); return pdfPTable; } }
創(chuàng)建controller進(jìn)行測(cè)試
/** * @author Wang Guolong * @version 1.0 * @date 2020/6/28 3:17 下午 */ @RestController @RequestMapping("/pdf") public class PdfController { @RequestMapping("/generate") public Response generatePDF(HttpServletResponse response) throws Exception { String filename = "測(cè)試pdf"; // 設(shè)置下載格式為pdf response.setContentType("application/x-download"); response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".pdf"); OutputStream os = new BufferedOutputStream(response.getOutputStream()); // 1. Document document = new Document(); Document document = PdfUtil.createDocument(); // 2. 獲取writer PdfWriter.getInstance(document, os); // 3. open() document.open(); //設(shè)置字體 Font blackFont = PdfUtil.createFont(10, Font.NORMAL, BaseColor.BLACK); Font blueFont = PdfUtil.createFont(10, Font.NORMAL, BaseColor.BLUE); Font bigFont = PdfUtil.createFont(14, Font.NORMAL, BaseColor.BLACK); Font littleFont = PdfUtil.createFont(10, Font.NORMAL, BaseColor.BLACK); Paragraph title = PdfUtil.createParagraph("測(cè)試pdf", bigFont); title.setAlignment(Element.ALIGN_CENTER); // 4. 添加段落內(nèi)容 document.add(title); // 5. close() document.close(); os.close(); return new Response().setContent("success"); } }
運(yùn)行結(jié)果
下載頁面:
下載的文件效果:
到此這篇關(guān)于Java使用iTextPDF生成PDF文件的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)Java使用iTextPDF生成PDF內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring4.3 實(shí)現(xiàn)跨域CORS的方法
下面小編就為大家分享一篇spring4.3 實(shí)現(xiàn)跨域CORS的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01Java中Dijkstra算法求解最短路徑的實(shí)現(xiàn)
Dijkstra算法是一種解決最短路徑問題的常用算法,本文主要介紹了Java中Dijkstra算法求解最短路徑的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09Java的Hibernate框架中復(fù)合主鍵映射的創(chuàng)建和使用教程
復(fù)合主鍵映射用起來比普通的增加主鍵字段要復(fù)雜,這里我們就來共同學(xué)習(xí)Java的Hibernate框架中復(fù)合主鍵映射的創(chuàng)建和使用教程,需要的朋友可以參考下2016-07-07springboot如何通過不同的策略動(dòng)態(tài)調(diào)用不同的實(shí)現(xiàn)類
這篇文章主要介紹了springboot如何通過不同的策略動(dòng)態(tài)調(diào)用不同的實(shí)現(xiàn)類,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Java的Hibernate框架中一對(duì)多的單向和雙向關(guān)聯(lián)映射
建立對(duì)SQL語句的映射是Hibernate框架操作數(shù)據(jù)庫的主要手段,這里我們列舉實(shí)例來為大家講解Java的Hibernate框架中一對(duì)多的單向和雙向關(guān)聯(lián)映射2016-06-06Spring?框架中的?Bean?作用域(Scope)使用詳解
Spring框架中的Bean作用域(Scope)決定了在應(yīng)用程序中創(chuàng)建和管理的Bean對(duì)象的生命周期和可見性。本文將詳細(xì)介紹Spring框架中的Bean作用域的不同類型,包括Singleton、Prototype、Request、Session和Application,并解釋它們的特點(diǎn)和適用場(chǎng)景。2023-09-09