使用itextpdf操作pdf的實(shí)例講解
使用maven引入jar
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency>
創(chuàng)建exportToPDF工具類(lèi)
package com.os.core.util.file.exportPdf; import com.itextpdf.text.*; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.FontSelector; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; /** * Created by PengSongHe on 2016/10/20 0020. */ public class exportToPDF { public static void main(String[] args) { try { Document document = new Document();//默認(rèn)A4 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("d:/HelloWorld.pdf")); writer.setPdfVersion(PdfWriter.PDF_VERSION_1_7); document.addCreationDate(); document.addCreator("飛翔家族"); document.addTitle("export pdf"); document.addKeywords("export"); document.addSubject("飛翔家族 export pdf"); document.open(); //處理中文,需要itextasian.jar支持 FontSelector selector = new FontSelector(); selector.addFont(FontFactory.getFont(FontFactory.TIMES_ROMAN, 12)); selector.addFont(FontFactory.getFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED)); Phrase phrase = selector.process("HelloWord!你好。\u4fdd\u5b58"); document.add(new Paragraph(phrase)); //添加新頁(yè) //document.newPage(); //writer.setPageEmpty(false); //document.add(new Paragraph("New page")); document.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } } }
以上這篇使用itextpdf操作pdf的實(shí)例講解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MybatisPlus更新為null的字段及自定義sql注入
mybatis-plus在執(zhí)行更新操作,當(dāng)更新字段為空字符串或者null的則不會(huì)執(zhí)行更新,本文主要介紹了MybatisPlus更新為null的字段及自定義sql注入,感興趣的可以了解一下2024-05-05Activiti進(jìn)階之組任務(wù)實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Activiti進(jìn)階之組任務(wù)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08詳解Spring簡(jiǎn)單容器中的Bean基本加載過(guò)程
本篇將對(duì)定義在 XMl 文件中的 bean,從靜態(tài)的的定義到變成可以使用的對(duì)象的過(guò)程,即 bean 的加載和獲取的過(guò)程進(jìn)行一個(gè)整體的了解2017-05-05基于Process#waitFor()阻塞問(wèn)題的解決
這篇文章主要介紹了Process#waitFor()阻塞問(wèn)題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12SpringBoot開(kāi)發(fā)技巧啟動(dòng)時(shí)配置校驗(yàn)實(shí)現(xiàn)示例
這篇文章主要為大家介紹了SpringBoot開(kāi)發(fā)在啟動(dòng)時(shí)自動(dòng)配置校驗(yàn)的實(shí)現(xiàn)示例及原理解析,有需要的朋友可以借鑒參考下希望能夠有所幫助2021-10-10IntelliJ IDEA2019實(shí)現(xiàn)Web項(xiàng)目創(chuàng)建示例
這篇文章主要介紹了IntelliJ IDEA2019實(shí)現(xiàn)Web項(xiàng)目創(chuàng)建示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04關(guān)于MyBatis plus條件構(gòu)造器的逐條詳解
什么是條件構(gòu)造器呢?簡(jiǎn)單來(lái)說(shuō),條件構(gòu)造器就是用來(lái)生成我們查數(shù)據(jù)庫(kù)的sql。它可以簡(jiǎn)化sql代碼的編寫(xiě),靈活、方便且易于維護(hù)2021-09-09Mybatis(ParameterType)傳遞多個(gè)不同類(lèi)型的參數(shù)方式
這篇文章主要介紹了Mybatis(ParameterType)傳遞多個(gè)不同類(lèi)型的參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04