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文件格式互转内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 关于Java类的构造方法详解

    关于Java类的构造方法详解

    这篇文章主要介绍了关于Java类的构造方法详解的相关资料,需要的朋友可以参考下
    2023-01-01
  • 解决swagger2.9.2接口文档显示的问题

    解决swagger2.9.2接口文档显示的问题

    这篇文章主要介绍了解决swagger2.9.2接口文档显示的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • mac配置Java web开发环境的全过程

    mac配置Java web开发环境的全过程

    对于开发人员来说,电脑的性能很重要,所以换了Mac后需要重新配置开发环境,这篇文章主要介绍了mac配置Java web开发环境的相关资料,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2025-09-09
  • Java定时器Timer使用方法详解

    Java定时器Timer使用方法详解

    这篇文章主要为大家详细介绍了Java定时器Timer的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • Spring StopWatch使用实例详解

    Spring StopWatch使用实例详解

    这篇文章主要介绍了Spring StopWatch使用实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01
  • Java并发编程中的Exchanger解析

    Java并发编程中的Exchanger解析

    这篇文章主要介绍了Java并发编程中的Exchanger解析,Exchanger用于线程间数据的交换,它提供一个同步点,在这个同步点,两个线程可以交换彼此的数据,这两个线程通过exchange方法交换数据,如果第一个线程先执行exchange()方法,需要的朋友可以参考下
    2023-11-11
  • String类下compareTo()与compare()方法比较

    String类下compareTo()与compare()方法比较

    这篇文章主要介绍了String类下compareTo()与compare()方法比较的相关资料,需要的朋友可以参考下
    2017-05-05
  • Java实战权限管理系统的实现流程

    Java实战权限管理系统的实现流程

    读万卷书不如行万里路,只学书上的理论是远远不够的,只有在实战中才能获得能力的提升,本篇文章手把手带你用java+SpringBoot+MyBatis+AOP+LayUI+Mysql实现一个权限管理系统,大家可以在过程中查缺补漏,提升水平
    2022-01-01
  • Java中类的加载器及其加载过程

    Java中类的加载器及其加载过程

    字节码文件在类加载器子系统中要进行加载、链接、初始化等处理,我们这里详细来谈其中过程,本文主要介绍了Java中类的加载器及其加载过程,感兴趣的同学可以参考一下
    2023-04-04
  • Java的ThreadContext类加载器的实现

    Java的ThreadContext类加载器的实现

    这篇文章主要介绍了Java的ThreadContext类加载器的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06

最新评论