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

相关推荐
夕阳之后的黑夜21 小时前
Python脚本:为PDF批量添加水印
开发语言·python·pdf
weixin_416660071 天前
AI 生成复杂公式在 Word 中乱码的原因与解决方案
ai·word·数学公式
缺点内向1 天前
Word 自动化处理:如何用 C# 让指定段落“隐身”?
开发语言·c#·自动化·word·.net
夏沫mds1 天前
Node.js 实现高保真 PDF 压缩:从 Canvas 方案到 Ghostscript 的踩坑实录
pdf·node.js
开开心心_Every2 天前
家长控制电脑软件:定时锁屏管理使用时长
网络协议·tcp/ip·游戏·微信·pdf·excel·语音识别
。puppy2 天前
SQL 注入整理
数据库·sql·word
gc_22992 天前
学习C#调用OpenXml操作word文档的基本用法(20:学习嵌入文件类)
c#·word·openxml·嵌入文档
开开心心就好2 天前
免费抽奖工具支持批量导入+自定义主题
linux·运维·服务器·macos·pdf·phpstorm·1024程序员节
pass_port_csdn2 天前
zotero搬家,迁移换机/重装100% 完美克隆指南:文献PDF、插件配置、文献分类、标签、笔记等所有信息全克隆
笔记·pdf·zotero
开开心心_Every2 天前
电脑定时休息软件:久坐提醒养成活动习惯
游戏·微信·pdf·excel·语音识别·散列表·启发式算法