You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

com.itextpdf.pdfoffice.OfficeConverter无法使用,求Java实现PDF转Word方法

Java实现PDF转Word(.doc/.docx)方案

首先明确:你当前的代码是将Word转PDF,和需求的PDF转Word方向完全相反,且com.itextpdf.pdfoffice.OfficeConverter属于iText 7的商业模块,iText 5版本并不包含该功能,这是你代码无法运行的核心原因之一。以下是几种可行的PDF转Word实现方案:

方案一:iText 7 PdfOffice(商业付费,格式保留完整)

iText 7的PdfOffice模块支持PDF转Word,需使用正确依赖并获取合法授权:

Maven依赖

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>pdfoffice</artifactId>
    <version>4.0.3</version> <!-- 建议使用最新兼容版本 -->
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>kernel</artifactId>
    <version>7.2.5</version>
</dependency>

代码示例

import com.itextpdf.pdfoffice.PdfOfficeConverter;
import java.io.File;

public class PdfToWordConverter {
    public static void main(String[] args) throws Exception {
        // 加载授权文件(必须,无授权无法使用商业功能)
        // LicenseKey.loadLicenseFile(new File("path/to/license.json"));
        
        File pdfSource = new File("input.pdf");
        File wordTarget = new File("output.docx");
        
        // 执行PDF转Word
        PdfOfficeConverter.convertPdfToOfficeDocument(pdfSource, wordTarget);
    }
}

方案二:Apache PDFBox + POI(免费开源,适合纯文本场景)

该方案通过PDFBox提取PDF文本,再用POI写入Word文档,仅支持纯文本转换,无法保留格式、图片、表格等复杂元素:

Maven依赖

<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.32</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.3</version>
</dependency>

代码示例

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class PdfToWordWithOpenSource {
    public static void main(String[] args) {
        try (PDDocument pdfDoc = PDDocument.load(new FileInputStream("input.pdf"));
             XWPFDocument wordDoc = new XWPFDocument();
             FileOutputStream outStream = new FileOutputStream("output.docx")) {

            PDFTextStripper textStripper = new PDFTextStripper();
            String pdfContent = textStripper.getText(pdfDoc);
            
            // 创建Word段落并写入文本
            XWPFParagraph paragraph = wordDoc.createParagraph();
            paragraph.createRun().setText(pdfContent);
            
            wordDoc.write(outStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

方案三:Aspose.PDF for Java(商业付费,高质量转换)

Aspose.PDF支持保留PDF中的格式、图片、表格等元素,转换质量较高,但需付费授权:

代码示例

import com.aspose.pdf.Document;
import com.aspose.pdf.SaveFormat;

public class PdfToWordWithAspose {
    public static void main(String[] args) {
        // 加载授权(无授权输出文档会带水印)
        // com.aspose.pdf.License license = new com.aspose.pdf.License();
        // license.setLicense("Aspose.PDF.lic");
        
        Document pdfDocument = new Document("input.pdf");
        pdfDocument.save("output.docx", SaveFormat.DocX);
    }
}

内容的提问来源于stack exchange,提问作者user3053933

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 09:20:36