admin管理员组

文章数量:1638920

目录

  • 一、maven依赖
  • 二、代码示例
  • 三、注意事项
  • 四、版本升级


一、maven依赖

    <repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>https://repo.e-iceblue/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);
            }
        }

    }

本文标签: 功能wordPDFpoispire