Java实现Word、Excel、PDF文件格式互转的几种实现方式
更新时间:2025年12月10日 09:42:25 作者:Full Stack Developme
在Java中实现文档格式转换通常需要使用专门的库,下面我将介绍如何使用Apache POI、iText和Aspose等库来实现这些转换,有需要的小伙伴可以了解下
方案一:使用开源库(免费)
1. 添加Maven依赖
<dependencies>
<!-- Apache POI for Word and Excel -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<!-- iText for PDF -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.2.5</version>
<type>pom</type>
</dependency>
<!-- Apache PDFBox for PDF handling -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.27</version>
</dependency>
</dependencies>2. 实现代码示例
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import java.io.*;
public class DocumentConverter {
// Word转PDF
public static void convertWordToPdf(String inputPath, String outputPath) throws IOException {
try (XWPFDocument document = new XWPFDocument(new FileInputStream(inputPath));
PdfWriter writer = new PdfWriter(outputPath);
PdfDocument pdf = new PdfDocument(writer);
Document pdfDoc = new Document(pdf)) {
// 简单实现:提取文本内容并写入PDF
document.getParagraphs().forEach(paragraph -> {
pdfDoc.add(new Paragraph(paragraph.getText()));
});
}
}
// Excel转PDF
public static void convertExcelToPdf(String inputPath, String outputPath) throws IOException {
try (Workbook workbook = new XSSFWorkbook(new FileInputStream(inputPath));
PdfWriter writer = new PdfWriter(outputPath);
PdfDocument pdf = new PdfDocument(writer);
Document pdfDoc = new Document(pdf)) {
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
StringBuilder rowText = new StringBuilder();
for (Cell cell : row) {
switch (cell.getCellType()) {
case STRING:
rowText.append(cell.getStringCellValue()).append("\t");
break;
case NUMERIC:
rowText.append(cell.getNumericCellValue()).append("\t");
break;
default:
rowText.append("\t");
}
}
pdfDoc.add(new Paragraph(rowText.toString()));
}
}
}
// PDF转Word (简单文本提取)
public static void convertPdfToWord(String inputPath, String outputPath) throws IOException {
try (PDDocument document = PDDocument.load(new File(inputPath));
XWPFWordDocument wordDocument = new XWPFDocument();
FileOutputStream out = new FileOutputStream(outputPath)) {
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
// 创建段落并添加文本
XWPFParagraph paragraph = wordDocument.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText(text);
wordDocument.write(out);
}
}
// PDF转Excel (简单实现)
public static void convertPdfToExcel(String inputPath, String outputPath) throws IOException {
try (PDDocument document = PDDocument.load(new File(inputPath));
Workbook workbook = new XSSFWorkbook();
FileOutputStream out = new FileOutputStream(outputPath)) {
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
Sheet sheet = workbook.createSheet("PDF Content");
String[] lines = text.split("\n");
for (int i = 0; i < lines.length; i++) {
Row row = sheet.createRow(i);
Cell cell = row.createCell(0);
cell.setCellValue(lines[i]);
}
workbook.write(out);
}
}
}方案二:使用商业库Aspose(功能更强大)
Aspose提供了更完整和专业的文档转换解决方案,但需要购买许可证。
1. 添加Maven依赖
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>22.11</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>22.11</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>22.11</version>
</dependency>
</dependencies>2. 实现代码示例
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import com.aspose.cells.Workbook;
import com.aspose.pdf.*;
public class AsposeDocumentConverter {
// Word转PDF
public static void convertWordToPdf(String inputPath, String outputPath) throws Exception {
Document doc = new Document(inputPath);
doc.save(outputPath, SaveFormat.PDF);
}
// Excel转PDF
public static void convertExcelToPdf(String inputPath, String outputPath) throws Exception {
Workbook workbook = new Workbook(inputPath);
workbook.save(outputPath, com.aspose.cells.SaveFormat.PDF);
}
// PDF转Word
public static void convertPdfToWord(String inputPath, String outputPath) throws Exception {
Document doc = new Document(inputPath);
doc.save(outputPath, SaveFormat.DOCX);
}
// PDF转Excel
public static void convertPdfToExcel(String inputPath, String outputPath) throws Exception {
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(inputPath);
ExcelSaveOptions options = new ExcelSaveOptions();
pdfDocument.save(outputPath, options);
}
// Word转Excel
public static void convertWordToExcel(String inputPath, String outputPath) throws Exception {
// 先将Word转为PDF,再转为Excel
String tempPdf = "temp.pdf";
convertWordToPdf(inputPath, tempPdf);
convertPdfToExcel(tempPdf, outputPath);
new java.io.File(tempPdf).delete();
}
// Excel转Word
public static void convertExcelToWord(String inputPath, String outputPath) throws Exception {
// 先将Excel转为PDF,再转为Word
String tempPdf = "temp.pdf";
convertExcelToPdf(inputPath, tempPdf);
convertPdfToWord(tempPdf, outputPath);
new java.io.File(tempPdf).delete();
}
}使用示例
public class Main {
public static void main(String[] args) {
try {
// 使用开源方案
DocumentConverter.convertWordToPdf("input.docx", "output.pdf");
DocumentConverter.convertExcelToPdf("input.xlsx", "output.pdf");
// 使用Aspose方案(需要许可证)
// AsposeDocumentConverter.setLicense(); // 设置许可证
AsposeDocumentConverter.convertWordToPdf("input.docx", "output.pdf");
AsposeDocumentConverter.convertExcelToPdf("input.xlsx", "output.pdf");
} catch (Exception e) {
e.printStackTrace();
}
}
}注意事项
- 开源方案限制:使用Apache POI和iText的开源方案可能无法完美处理复杂的文档格式和布局
- 商业方案:Aspose等商业库提供更完整的解决方案,但需要购买许可证
- 字体处理:确保系统中安装了文档中使用的字体,以避免转换时的字体问题
- 复杂格式:表格、图片、特殊格式等在转换时可能需要特殊处理
根据您的具体需求和预算,可以选择适合的方案。对于简单的文档转换,开源方案可能足够;对于企业级应用,商业库可能是更好的选择。
到此这篇关于Java实现Word、Excel、PDF文件格式互转的几种实现方式的文章就介绍到这了,更多相关Java文件格式互转内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
String类下compareTo()与compare()方法比较
这篇文章主要介绍了String类下compareTo()与compare()方法比较的相关资料,需要的朋友可以参考下2017-05-05


最新评论