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)換后會(huì)有格式問題,暫未解決)
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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中實(shí)現(xiàn)日期時(shí)間字符串轉(zhuǎn)換為Date對(duì)象的方法
在 Java 編程中,日期時(shí)間的處理是一項(xiàng)常見且重要的任務(wù),無論是數(shù)據(jù)存儲(chǔ)、日志記錄還是業(yè)務(wù)邏輯處理,準(zhǔn)確地表示和操作日期時(shí)間都是不可或缺的,本文給大家介紹了Java中實(shí)現(xiàn)日期時(shí)間字符串轉(zhuǎn)換為Date對(duì)象的方法,需要的朋友可以參考下2025-01-01
Spring Security基本架構(gòu)與初始化操作流程詳解
這篇文章主要介紹了Spring Security基本架構(gòu)與初始化操作流程,Spring Security是一個(gè)能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架2023-03-03
學(xué)習(xí)JVM之java內(nèi)存區(qū)域與異常
關(guān)于JVM內(nèi)存區(qū)域的知識(shí)對(duì)于初學(xué)者來說其實(shí)是很重要的,了解Java內(nèi)存分配的原理,這對(duì)于以后JAVA的學(xué)習(xí)會(huì)有更深刻的理解。下面來看看詳細(xì)介紹。2016-07-07
java實(shí)現(xiàn)浮點(diǎn)數(shù)轉(zhuǎn)人民幣的小例子
java實(shí)現(xiàn)浮點(diǎn)數(shù)轉(zhuǎn)人民幣的小例子,需要的朋友可以參考一下2013-03-03
Java實(shí)現(xiàn)的斷點(diǎn)續(xù)傳功能的示例代碼
本篇文章主要介紹了Java實(shí)現(xiàn)的斷點(diǎn)續(xù)傳功能的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
SpringBoot從0到1整合銀聯(lián)無跳轉(zhuǎn)支付功能附源碼
這篇文章主要介紹了SpringBoot從0到1整合銀聯(lián)無跳轉(zhuǎn)功能支付附源碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Java事務(wù)管理學(xué)習(xí)之Spring和Hibernate詳解
這篇文章主要給大家介紹了Java事務(wù)管理學(xué)習(xí)之Spring和Hibernate的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們可以參考借鑒,下面來一起看看吧。2017-03-03
Spring的循環(huán)依賴、三級(jí)緩存解決方案源碼詳細(xì)解析
這篇文章主要介紹了Spring的循環(huán)依賴、三級(jí)緩存解決方案源碼詳細(xì)解析,在Spring中,由于IOC的控制反轉(zhuǎn),創(chuàng)建對(duì)象不再是簡單的new出來,而是交給Spring去創(chuàng)建,會(huì)經(jīng)歷一系列Bean的生命周期才創(chuàng)建出相應(yīng)的對(duì)象,需要的朋友可以參考下2024-01-01

