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

相关推荐
快乐的韭菜根根4 小时前
Word进阶
word
拓端研究室6 小时前
专题:2025年AI Agent智能体行业价值及应用分析报告:技术落地与风险治理|附140+ 份报告PDF、数据、可视化模板汇总下载
人工智能·pdf
黄色茶杯10 小时前
解决WPS的word文件嵌入EXCEL无法双击打开
word·excel·wps
gc_229921 小时前
学习C#调用OpenXml操作word文档的基本用法(7:Style类分析-5)
学习·word·openxml
C.果栗子1 天前
Blob格式的PDF文件调用打印,浏览器文件打印(兼容)
前端·javascript·pdf
Highcharts.js1 天前
Highcharts常见问题解析(5):如何将多个图表导出到同一张图片或 PDF?
pdf·highcharts
Kuo-Teng1 天前
LeetCode 139: Word Break
java·算法·leetcode·职场和发展·word·动态规划
麦烤楽鸡翅1 天前
pdf(攻防世界)
网络安全·pdf·ctf·misc·杂项·攻防世界·信息竞赛
Less is moree1 天前
PDF无法打印怎么解决?
pdf
python_DONG2 天前
word中怎么查看插入的图片占用内存大小来压缩word文件整体大小
word