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中lambda表达式的基本运用

    Java中lambda表达式的基本运用

    大家好,本篇文章主要讲的是Java中lambda表达式的基本运用,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-01-01
  • 浅谈@FeignClient中name和value属性的区别

    浅谈@FeignClient中name和value属性的区别

    这篇文章主要介绍了@FeignClient中name和value属性的区别,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • SpringCloud feign微服务调用之间的异常处理方式

    SpringCloud feign微服务调用之间的异常处理方式

    这篇文章主要介绍了SpringCloud feign微服务调用之间的异常处理方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • Java使用jacob将微软office中word、excel、ppt转成pdf

    Java使用jacob将微软office中word、excel、ppt转成pdf

    这篇文章主要为大家详细介绍了Java使用jacob将微软office中word、excel、ppt转成pdf,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • Java多线程异步调用性能调优方法详解

    Java多线程异步调用性能调优方法详解

    这篇文章主要为大家详细介绍了Java多线程异步调用性能调优,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • 解决@Autowired报错Could not autowire. No beans of ‘XXX‘ type found问题

    解决@Autowired报错Could not autowire. No bea

    介绍了在IDEA中使用@Autowired报错Couldnot autowire. No beans of 'XXX' type found的解决方法,原因是@Autowired在注入service时,由于service接口没有实现类,而mybatis仅需提供Dao接口,导致@Autowired无法识别
    2024-12-12
  • java反射简单实例

    java反射简单实例

    这篇文章主要介绍了java反射机制,以一个简单实例形式分析了Java反射的原理与实现技巧,需要的朋友可以参考下
    2015-02-02
  • java并发编程专题(一)----线程基础知识

    java并发编程专题(一)----线程基础知识

    这篇文章主要介绍了java并发编程线程的基础知识,文中讲解非常详细,帮助大家更好的学习JAVA并发编程,感兴趣想学习JAVA的可以了解下
    2020-06-06
  • Intellij IDEA导入eclipse web项目的操作步骤详解

    Intellij IDEA导入eclipse web项目的操作步骤详解

    Eclipse当中的web项目都会有这两个文件,但是idea当中应该是没有的,所以导入会出现兼容问题,但是本篇文章会教大家如何导入,并且导入过后还能使用tomcat运行,需要的朋友可以参考下
    2023-08-08
  • Java之NoClassDefFoundError的原因及分析

    Java之NoClassDefFoundError的原因及分析

    在Java开发中,经常会遇到ClassNotFoundException和NoClassDefFoundError异常,ClassNotFoundException发生在编译时JVM无法找到类,而NoClassDefFoundError则发生在运行时JVM无法加载类,这两个异常虽然原因相似,但有本质区别
    2024-09-09

最新评论