使用aspose.words 18.6实现pdf文档转换

使用aspose试问pdf文档转换

jar包引入

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words</artifactId>
    <version>18.6</version>
</dependency>

水印处理

因为aspose是收费的,如果大家经济实力允许的话建议在官网购买授权的版本。
水印破解jar包下载链接

代码demo


/**
 * doc文档转换问pdf
 */
public class Word2PdfUtil {
    public static void main(String[] args) {
        doc2pdf("d:\\mnt\\uploads\\test.docx","d:\\mnt\\uploads\\test.pdf");
//        docx2doc("d:\\mnt\\uploads\\test.docx","d:\\mnt\\uploads\\test.doc");
//        System.out.println(getWordPages("d:\\mnt\\uploads\\test.docx"));
    }

    /**
     * check license
     *
     * @return
     */
    public static boolean getLicense() {
        boolean result = false;
        try {
            /**
             * license.xml应放在..\WebRoot\WEB-INF\classes路径下
             */
            InputStream is = Test.class.getClassLoader().getResourceAsStream("com.aspose.words.lic_2999.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * doc to pdf
     *
     * @param inPath
     * @param outPath
     */
    public static void doc2pdf(String inPath, String outPath) {
        Logs.info("开始进行doc转pdf文件操作");
        Logs.info("inPath >> " + inPath);
        Logs.info("outPath >> " + outPath);
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!Word2PdfUtil.getLicense()) {
            return;
        }
        try {
            long old = System.currentTimeMillis();
            // 新建一个空白pdf文档
            File file = new File(outPath);
            if (!file.canRead()) {
                file.setReadable(true);
            }
            if (!file.canWrite()) {
                file.setWritable(true);
            }
            FileOutputStream os = new FileOutputStream(file);
            // Address是将要被转化的word文档
            Document doc = new Document(inPath);
            // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
            doc.save(os, SaveFormat.PDF);
            // EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            // 转化用时
            Logs.info("共耗时:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            Logs.error("文件转换异常:" + e.getMessage(), e);
        }
    }

    public static void docx2doc(String docxPath, String docPath) {
        Logs.info("开始进行docx转doc文件操作");
        Logs.info("docxPath >> " + docxPath);
        Logs.info("docPath >> " + docPath);
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!Word2PdfUtil.getLicense()) {
            return;
        }
        try {
            long old = System.currentTimeMillis();
            // 新建一个空白pdf文档
            File file = new File(docPath);
            if (!file.canRead()) {
                file.setReadable(true);
            }
            if (!file.canWrite()) {
                file.setWritable(true);
            }
            FileOutputStream os = new FileOutputStream(file);
            // Address是将要被转化的word文档
            Document doc = new Document(docxPath);
            // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
            doc.save(os, SaveFormat.DOC);
            // EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            // 转化用时
            Logs.info("共耗时:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            Logs.error("文件转换异常:" + e.getMessage(), e);
        }
    }

    /**
     * 获取word文件页码数
     *
     * @param filePath 文件路径
     * @return
     */
    public static Integer getWordPages(String filePath) {
        try {
            //需要将文档转成pdf再读取。当我们的文档较大时使用这种方式会效率会比较慢,需要优化处理
            //但目前我们平台的文章不会太大,所以暂时不考虑
            String pdfPath = filePath.replace(".doc", ".pdf").replace(".docx", ".pdf");
            Word2PdfUtil.doc2pdf(filePath, pdfPath);
            int pageSize = 0;
            Document dc = new Document(pdfPath);
            Logs.info(">>>>" + dc.getPageCount());
//            new File(pdfPath).delete();//读取完成后将文件删除
            return pageSize;
        } catch (Exception e) {
            Logs.error("获取文件页数异常:" + e.getMessage(), e);
        }
        return 0;
    }

    /**
     * 获取分页数
     *
     * @param file
     * @return
     */
    public static Integer getWordPages(File file) {
        return Word2PdfUtil.getWordPages(file.getPath());
    }

    /**
     * 将文件转换成pdf,在原路径保留一份pdf文件
     *
     * @param file
     */
    public static void doc2pdf(File file) {
        if (file == null) {
            return;
        }
        String docPath = file.getPath();
        String pdfPath = docPath.replace(".doc", ".pdf").replace(".docx", ".pdf");
        Word2PdfUtil.doc2pdf(docPath, pdfPath);
    }

    /**
     * doc 转 pdf
     *
     * @param docFile
     * @param pdfFile
     */
    public static void doc2pdf(File docFile, File pdfFile) {
        String docPath = docFile.getPath();
        String pdfPath = pdfFile.getPath();
        Word2PdfUtil.doc2pdf(docPath, pdfPath);
    }

    /**
     * doc 转 pdf
     *
     * @param docxFile
     * @param docFile
     */
    public static void docx2doc(File docxFile, File docFile) {
        String docxPath = docxFile.getPath();
        String pdfPath = docFile.getPath();
        Word2PdfUtil.docx2doc(docxPath, pdfPath);
    }

    /**
     * doc 转 pdf
     *
     * @param docxFile
     */
    public static void docx2doc(File docxFile) {
        String docxPath = docxFile.getPath();
        String pdfPath = docxPath.replace(".docx", ".doc");
        Word2PdfUtil.docx2doc(docxPath, pdfPath);
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值