【pdf转word】基于spire和poi免费实现pdf转word功能

文章介绍了如何在Java项目中利用Maven添加Spire.pdf.free和ApachePOI的依赖来实现PDF转Word功能,并提供了清除转换后文件中Spire警告文字的代码示例。还提及了版本升级的解决方案,针对高版本可能出现的每页警告,给出了遍历删除的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一、maven依赖

    <repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories>
    
    <dependencies>
      <!-- spire pdf转word工具 -->
        <dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.pdf.free</artifactId>
            <version>5.1.0</version>
        </dependency>

        <!-- poi工具 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
    </dependencies>

二、代码示例

废话不多说,直接上代码:

/**
 * pdf转word工具类
 */
public class Pdf2WordUtils {

    public static void main(String[] args) {
        pdf2Word("D:\\Cache\\DingDing\\508076963802517505.pdf","D:\\Cache\\DingDing\\ToDocx.docx");
    }

    /**
     * pdf转word
     * @param inPath pdf路径
     * @param outPath 生成word路径
     */
    public static void pdf2Word(String inPath, String outPath) {
        // pdf转word
        doPdf2Word(inPath, outPath);

        // 清除Spire转换后文件的备注
        clearSpireComment(outPath);
    }

    private static void doPdf2Word(String inPath, String outPath) {
        PdfDocument pdf = null;

        try {
            // 创建一个 PdfDocument 对象
            pdf = new PdfDocument();

            // 加载 PDF 文件
            pdf.loadFromFile(inPath);

            // 将PDF转换为Docx格式文件并保存
            pdf.saveToFile(outPath, FileFormat.DOCX);

        }catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("pdf转word异常!");
        }finally {
            if (pdf != null) {
                pdf.close();
            }
        }
    }

    /**
     * 清除Spire转换后文件的备注
     * Evaluation Warning : The document was created with Spire.PDF for Java.
     */
    private static void clearSpireComment(String outPath) {
        InputStream is = null;
        OutputStream out = null;
        XWPFDocument xwpfDocument = null;

        try {
            is = new FileInputStream(outPath);
            xwpfDocument = new XWPFDocument(is);
            // 删除第一个元素(Spire备注)
            xwpfDocument.removeBodyElement(0);
            out = new FileOutputStream(outPath);
            xwpfDocument.write(out);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("pdf转word异常!");
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (out != null) {
                    out.close();
                }
                if (xwpfDocument != null) {
                    xwpfDocument.close();
                }
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

三、注意事项

有个问题需要注意一下,spire 免费版生成的 word 文档,上面会多出一行警告的文字:

在这里插入图片描述

所以通过这行代码(基于 poi)把警告给删掉:

// 清除Spire转换后文件的备注
clearSpireComment(outPath);

四、版本升级

最近有同学反馈只能免费转10页,升级下 spire 版本就行,我这边用的是 9.6.2 版本。高版本的每页都会多一行警告,通过 poi 遍历删除就行。

 private void clearSpireComment(String outPath) {
        //要替换的map,key为占位符,value为要被替换的值
        Map<String, Object> map = new HashMap<>();
        map.put("Evaluation Warning : The document was created with Spire.PDF for java.", "");
        FileOutputStream fileOutputStream=null;
        XWPFDocument doc=null;
        try{
            //读取文件
            OPCPackage opcPackage = POIXMLDocument.openPackage(filePath);
            //加载文档
            doc = new XWPFDocument(opcPackage);
            doc.removeBodyElement(0);

            //获取所有段落
            List<XWPFParagraph> paragraphList = doc.getParagraphs();
            for (XWPFParagraph par : paragraphList) {
                //获取段落的文本对象
                List<XWPFRun> runs = par.getRuns();
                for (XWPFRun run : runs) {
                    //获取文本的值
                    String text = run.getText(0);
                    //遍历map
                    for (Map.Entry<String, Object> entry : map.entrySet()) {
                        //获取map的key
                        String key = entry.getKey();
                        //判断文本的值和map的key,文本中是否有和key一样的占位符
                        if (StringUtils.isNotEmpty(text) && text.indexOf(key) != -1) {
                            //获取对应key的value
                            Object value = entry.getValue();
                            //把文本的内容,key替换为value
                            text = text.replace(key, value.toString());
                            //把替换好的文本内容,保存到当前这个文本对象
                            run.setText(text, 0);
                        }
                    }
                }
            }
            File file = new File(fileOutPath);
            fileOutputStream = new FileOutputStream(file);
            doc.write(fileOutputStream);
        }catch (Exception e){
            log.error("docx文件文本替换失败",e);
        }finally {
            try {
                if(fileOutputStream!=null){
                    fileOutputStream.close();
                }
                doc.close();
            } catch (IOException e) {
                log.error("docx文件文本替换失败",e);
            }
        }

    }
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

砍光二叉树

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值