word转pdf

发布时间 2023-03-24 22:00:17作者: 风子a_a

网上找了很久,虽说有解决办法,但是不是收费就是限制转换页数,或者有水印.功夫不负有心人,找到了,这里做下记录

学习版请24小时之内删除

使用前请先把license.xml放到配置文件

dll文件放在JDK lib文件夹

    /**
     * 获取aspose证书
     *
     * @return boolean
     * @auther: YSL
     * @date: 2022/8/2 12:10
     */
    private static boolean getLicense() {
        boolean result = false;
        InputStream is = null;
        try {
            Resource resource = new ClassPathResource("license.xml");
            is = resource.getInputStream();
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * word转pdf静态方法
     *
     * @param wrodPath   word文件全路径含文件名
     * @param pdfOutPath pdf输出全路径含文件名
     * @return boolean
     * @auther: YSL
     * @date: 2022/8/2 12:08
     */
    public static boolean word2pdf(String wrodPath, String pdfOutPath) {
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense()) {
            return false;
        }
        FileOutputStream os = null;
        try {
            long old = System.currentTimeMillis();
            // 新建一个空白pdf文档
            File file = new File(pdfOutPath);
            os = new FileOutputStream(file);
            // inPath是将要被转化的word文档
            Document doc = new Document(wrodPath);
            // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,EPUB, XPS, SWF 相互转换
            doc.save(os, SaveFormat.PDF);
            long now = System.currentTimeMillis();
            // 转化用时
            logger.info("word转换pdf成功,共耗时:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            logger.info("word转pdf失败", e);
            return false;
        } finally {
            if (os != null) {
                try {
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }

效果图

word

转换后

文件下载
https://wwo.lanzouy.com/iiEW20qzovuh
密码:2vem