java8如何通過poi+text將word轉(zhuǎn)為pdf
更新時間:2025年04月23日 10:53:45 作者:C__jx
這篇文章主要介紹了java8如何通過poi+text將word轉(zhuǎn)為pdf問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
java8通過poi+text將word轉(zhuǎn)為pdf
1、jar包
<dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId> <version>1.0.6</version> </dependency>
2、代碼util類
(部分文檔轉(zhuǎn)換后會有格式問題,暫未解決)
package com.zjjw.jxtest.util.util; import com.lowagie.text.Font; import com.lowagie.text.pdf.BaseFont; import fr.opensagres.xdocreport.itext.extension.font.IFontProvider; import org.apache.poi.xwpf.converter.pdf.PdfConverter; import org.apache.poi.xwpf.converter.pdf.PdfOptions; import org.apache.poi.xwpf.usermodel.XWPFDocument; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Paths; /** * @author: chenjiaxiang * @create: 2022/11/22 14:40 **/ public class WordToPdfUtils { private static final String FONTS1 = "/Users/chenjx/Library/Fonts/SIMSUN.TTC,1"; private static final String FONTS2 = "/Users/chenjx/Library/Fonts/SIMFANG.TTF"; private static final String FONTS_NAME = "仿宋"; public static void main(String[] args) throws Exception { String filePath = "/Users/chenjx/Downloads/zipceshi/createYuWord.docx"; String outPath = "/Users/chenjx/Downloads/zipceshi/pdf/a.pdf"; WordToPdfUtils wordPdfUtils = new WordToPdfUtils(); wordPdfUtils.wordToPdf(filePath, outPath); } public void wordToPdf(String wordPath, String pdfPath) { InputStream in = null; OutputStream outPDF = null; XWPFDocument document; try { in = Files.newInputStream(Paths.get(wordPath)); document = new XWPFDocument(in); // 將word轉(zhuǎn)成pdf PdfOptions options = PdfOptions.create(); outPDF = Files.newOutputStream(Paths.get(pdfPath)); options.fontProvider(new IFontProvider() { @Override public Font getFont(String familyName, String encoding, float size, int style, java.awt.Color color) { try { String prefixFont; String os = System.getProperties().getProperty("os.name"); if (os.startsWith("win") || os.startsWith("Win")) { /*windows字體*/ prefixFont = "C:\\Windows\\Fonts\\simsun.ttc,0"; } else { /*linux字體*/ prefixFont = FONTS1; } BaseFont stChinese = BaseFont.createFont(prefixFont, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); BaseFont fsChinese = BaseFont.createFont(FONTS2, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font stFontChinese = new Font(stChinese, size, style, color); Font fsFontChinese = new Font(fsChinese, size, style, color); if (familyName != null) { if (FONTS_NAME.equals(familyName)) { fsFontChinese.setFamily(familyName); return fsFontChinese; } else { stFontChinese.setFamily(familyName); } } return stFontChinese; } catch (Exception e) { e.printStackTrace(); return null; } } }); PdfConverter.getInstance().convert(document, outPDF, options); } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } if (outPDF != null) { outPDF.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
3、word格式
4、導(dǎo)出pdf樣式
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中實現(xiàn)日期時間字符串轉(zhuǎn)換為Date對象的方法
在 Java 編程中,日期時間的處理是一項常見且重要的任務(wù),無論是數(shù)據(jù)存儲、日志記錄還是業(yè)務(wù)邏輯處理,準確地表示和操作日期時間都是不可或缺的,本文給大家介紹了Java中實現(xiàn)日期時間字符串轉(zhuǎn)換為Date對象的方法,需要的朋友可以參考下2025-01-01Spring Security基本架構(gòu)與初始化操作流程詳解
這篇文章主要介紹了Spring Security基本架構(gòu)與初始化操作流程,Spring Security是一個能夠為基于Spring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架2023-03-03學(xué)習(xí)JVM之java內(nèi)存區(qū)域與異常
關(guān)于JVM內(nèi)存區(qū)域的知識對于初學(xué)者來說其實是很重要的,了解Java內(nèi)存分配的原理,這對于以后JAVA的學(xué)習(xí)會有更深刻的理解。下面來看看詳細介紹。2016-07-07java實現(xiàn)浮點數(shù)轉(zhuǎn)人民幣的小例子
java實現(xiàn)浮點數(shù)轉(zhuǎn)人民幣的小例子,需要的朋友可以參考一下2013-03-03SpringBoot從0到1整合銀聯(lián)無跳轉(zhuǎn)支付功能附源碼
這篇文章主要介紹了SpringBoot從0到1整合銀聯(lián)無跳轉(zhuǎn)功能支付附源碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11Java事務(wù)管理學(xué)習(xí)之Spring和Hibernate詳解
這篇文章主要給大家介紹了Java事務(wù)管理學(xué)習(xí)之Spring和Hibernate的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友們可以參考借鑒,下面來一起看看吧。2017-03-03Spring的循環(huán)依賴、三級緩存解決方案源碼詳細解析
這篇文章主要介紹了Spring的循環(huán)依賴、三級緩存解決方案源碼詳細解析,在Spring中,由于IOC的控制反轉(zhuǎn),創(chuàng)建對象不再是簡單的new出來,而是交給Spring去創(chuàng)建,會經(jīng)歷一系列Bean的生命周期才創(chuàng)建出相應(yīng)的對象,需要的朋友可以參考下2024-01-01