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樣式

總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring-data-redis操作redis cluster的示例代碼
這篇文章主要介紹了Spring-data-redis操作redis cluster的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10
Java利用HttpClient模擬POST表單操作應用及注意事項
本文主要介紹JAVA中利用HttpClient模擬POST表單操作,希望對大家有所幫助。2016-04-04
java打包文件成zip、壓縮文件及file.mkdir和mkdirs的區(qū)別詳解
這篇文章主要給大家介紹了關于java打包文件成zip、壓縮文件及file.mkdir和mkdirs區(qū)別詳解的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-12-12

