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

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值