当前位置: 首页 > news >正文

将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包可以在我资源库下载

相关文章:

  • Day48:WEB攻防-PHP应用文件上传中间件CVE解析第三方编辑器已知CMS漏洞
  • CMake学习笔记(一)一个最简单的CMakeLists嵌套示例
  • 使用Urllib库创建第一个爬虫程序
  • 创新研报 | 2024+人工智能安全报告
  • 基于javaweb(springboot+mybatis)生活美食分享平台管理系统设计和实现以及文档报告
  • OD_2024_C卷_100分_70、停车场车辆统计【JAVA】【逻辑分析】
  • 监控API的指标
  • MySQL内置函数
  • 机器学习 - save和load训练好的模型
  • PHP 读取嵌入式数据 SQLite3
  • 一个单生产-多消费模式下无锁方案(ygluu/卢益贵)
  • 数字乡村引领新风尚:科技赋能农村实现全面进步
  • 零基础机器学习(3)之机器学习的一般过程
  • GPT4.0
  • [flask]flask的路由
  • 35 跨域相关问题, 以及常见的解决方式
  • 【笔记】Hbase基础笔记
  • 刚进公司第一天-电脑环境搭建
  • 基于AT89C51单片机的智能交通灯设计
  • APS54083 大功率深度调光降压恒流驱动IC PWM 线性调光 车灯IC
  • https://app.hackthebox.com/machines/Inject
  • Spring —— Spring简单的读取和存储对象 Ⅱ
  • 渗透测试之冰蝎实战
  • Mybatis、TKMybatis对比
  • Microsoft Office 2019(2022年10月批量许可版)图文教程
  • 《谷粒商城基础篇》分布式基础环境搭建
  • 哈希表题目:砖墙
  • Vue 3.0 选项 生命周期钩子
  • 【车载嵌入式开发】AutoSar架构入门介绍篇
  • 【计算机视觉 | 目标检测】DETR风格的目标检测框架解读