java8如何通過poi+text將word轉為pdf
更新時間:2025年04月23日 10:53:45 作者:C__jx
這篇文章主要介紹了java8如何通過poi+text將word轉為pdf問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
java8通過poi+text將word轉為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類
(部分文檔轉換后會有格式問題,暫未解決)
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轉成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、導出pdf樣式
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java中實現(xiàn)日期時間字符串轉換為Date對象的方法
在 Java 編程中,日期時間的處理是一項常見且重要的任務,無論是數據存儲、日志記錄還是業(yè)務邏輯處理,準確地表示和操作日期時間都是不可或缺的,本文給大家介紹了Java中實現(xiàn)日期時間字符串轉換為Date對象的方法,需要的朋友可以參考下2025-01-01SpringBoot從0到1整合銀聯(lián)無跳轉支付功能附源碼
這篇文章主要介紹了SpringBoot從0到1整合銀聯(lián)無跳轉功能支付附源碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11Spring的循環(huán)依賴、三級緩存解決方案源碼詳細解析
這篇文章主要介紹了Spring的循環(huán)依賴、三級緩存解決方案源碼詳細解析,在Spring中,由于IOC的控制反轉,創(chuàng)建對象不再是簡單的new出來,而是交給Spring去創(chuàng)建,會經歷一系列Bean的生命周期才創(chuàng)建出相應的對象,需要的朋友可以參考下2024-01-01