Java实现Word转PDF文件
高效、稳定、不失真的文档转换解决方案
引言
在企业级Java应用开发中,经常需要将Word文档(.doc/.docx)转换为PDF格式,以确保文档的格式一致性、安全性和可打印性。本文将详细介绍几种主流的Java实现方案,分析其优缺点,并提供可运行的代码示例。
提示: 本文重点解决不失真问题,即转换后PDF能完美保留原Word文档的样式、图片、表格和字体。
方案一:使用Apache POI + iText(仅限.docx)
适用于现代Word文档(.docx),基于XML解析,无需外部依赖。
Maven依赖
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.4</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.1.16</version>
<type>pom</type>
</dependency>
</dependencies>
Java代码示例
import org.apache.poi.xwpf.usermodel.*;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
public class WordToPdfConverter {
public static void convert(String docxPath, String pdfPath) throws Exception {
XWPFDocument document = new XWPFDocument(new FileInputStream(docxPath));
PdfWriter writer = new PdfWriter(pdfPath);
PdfDocument pdfDoc = new PdfDocument(writer);
Document pdf = new Document(pdfDoc);
for (IBodyElement element : document.getBodyElements()) {
if (element instanceof XWPFParagraph) {
XWPFParagraph para = (XWPFParagraph) element;
pdf.add(new Paragraph(para.getText()));
}
}
pdf.close();
document.close();
}
}
优点: 纯Java,跨平台,轻量级。
缺点: 样式保留不完整,复杂文档易失真。
方案二:使用Jacob(推荐用于Windows环境)
Jacob(Java COM Bridge)调用Windows系统自带的Microsoft Word进行转换,转换质量最高。
实现步骤
- 下载Jacob库(jacob.jar 和 jacob-1.18-M2-x64.dll)
- 将DLL文件放入系统PATH或项目根目录
- 通过COM接口调用Word应用
Java代码示例
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
public class JacobConverter {
public static final int wdFormatPDF = 17;
public static void convert(String docPath, String pdfPath) {
ActiveXComponent word = new ActiveXComponent("Word.Application");
try {
word.setProperty("Visible", false);
Dispatch documents = word.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.call(documents, "Open", docPath).toDispatch();
Dispatch.call(doc, "SaveAs", pdfPath, wdFormatPDF);
Dispatch.call(doc, "Close");
} catch (Exception e) {
e.printStackTrace();
} finally {
word.invoke("Quit");
}
}
}
优点: 转换质量极高,100%保留原始样式,支持.doc和.docx。
缺点: 仅限Windows,需安装Microsoft Office,线程安全需注意。
方案三:使用LibreOffice + JODConverter
跨平台解决方案,利用LibreOffice的文档转换能力。
实现步骤
- 安装LibreOffice
- 启动LibreOffice服务:soffice --headless --accept="socket,port=8100;urp;"
- 添加JODConverter依赖
Maven依赖
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>4.4.3</version>
</dependency>
Java代码示例
import org.jodconverter.DocumentConverter;
import org.jodconverter.local.LocalConverter;
public class LibreOfficeConverter {
public static void convert(String inputPath, String outputPath) {
DocumentConverter converter = LocalConverter.builder()
.build();
converter.convert(new File(inputPath))
.to(new File(outputPath))
.execute();
}
}
优点: 跨平台(Windows/Linux/Mac),高质量转换,免费。
缺点: 需要安装LibreOffice,占用资源较多。
方案对比与选择建议
| 方案 |
平台 |
质量 |
性能 |
适用场景 |
| Apache POI + iText |
跨平台 |
中 |
高 |
简单文档,轻量级需求 |
| Jacob |
Windows |
高 |
中 |
企业内部系统,要求完美还原 |
| LibreOffice |
跨平台 |
高 |
中 |
Linux服务器部署,高质量需求 |
推荐: 若部署在Windows且有Office,首选Jacob;若在Linux服务器,首选LibreOffice + JODConverter。