将word转为PDF的几种简单方式

第一种:使用spire.doc.jar包,用时7秒左右。
引入spire.doc-11.1.1.jar包,该包带水印,建议使用免费版的spire.doc.free-5.2.0.jar,免费版只能转三页。
 

package web.tools.excel;

import com.spire.doc.*;


public class test {

    public static void main(String[] args) {
        Document doc = new Document();
        doc.loadFromFile("D:/Backup/Downloads/我的源文件.docx");
        doc.saveToFile("D:/Backup/Downloads/输出文件.pdf", FileFormat.PDF);
        doc.close();
        System.out.println("转化完毕-----------------------");
    }

}
第二种:使用aspose-words-15.8.0-jdk16.jar包,用时3秒左右。
   /**
         * 将word文档转为PDF后输出
         * 烟雨江南
         * @param urlIn word文档所在全路径  ;dir输出文档的目录
         * @return
         */
        public static String wordToPdf(String urlIn,String dir){
            String url="";
            try {
                //自定义新文件名
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
                long dateTime = new Date().getTime();
                String currentTimestr = sdf1.format(dateTime);
                String newFileName = currentTimestr + "-" + suiJishu() + ".pdf";
                url=dir+"/"+newFileName;
                //
                String s = "<License><Data><Products><Product>Aspose.Total for                 Java</Product>        
                ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
                License license = new License();
                license.setLicense(is);
                com.aspose.words.Document document = new com.aspose.words.Document(urlIn);
                File file=new File(url);
                FileOutputStream out=new FileOutputStream(file);
                document.save(out,SaveFormat.PDF);
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return url;
            }
备注:jar包可以在我资源库下载

相关推荐

  1. word转为PDF简单方式

    2024-03-27 07:00:06       40 阅读
  2. ArrayList<Integer>()转为int[]方式

    2024-03-27 07:00:06       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-27 07:00:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-27 07:00:06       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-27 07:00:06       82 阅读
  4. Python语言-面向对象

    2024-03-27 07:00:06       91 阅读

热门阅读

  1. 【手写AI代码目录】准备发布的教程

    2024-03-27 07:00:06       34 阅读
  2. vue 文件预览(docx、.xlsx、pdf)

    2024-03-27 07:00:06       42 阅读
  3. net core 使用 iTextSharp 生成PDF

    2024-03-27 07:00:06       26 阅读
  4. 触发器的工艺结构原理及选型参数总结

    2024-03-27 07:00:06       43 阅读
  5. 决策树介绍

    2024-03-27 07:00:06       37 阅读
  6. 深入学习Spark SQL:处理结构化数据的利器

    2024-03-27 07:00:06       36 阅读
  7. 决策树-计算信息熵

    2024-03-27 07:00:06       35 阅读
  8. 决策树学习心得

    2024-03-27 07:00:06       42 阅读
  9. Stable Diffusion 本地部署教程

    2024-03-27 07:00:06       37 阅读
  10. 压力测试(QPS)及测试工具Locust

    2024-03-27 07:00:06       39 阅读
  11. Spark SizeTrackingAppendOnlyMap 相关源代码分析

    2024-03-27 07:00:06       37 阅读
  12. Stable Diffusion XL之核心基础内容

    2024-03-27 07:00:06       38 阅读
  13. k8s 的资源清单

    2024-03-27 07:00:06       36 阅读