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

相关推荐
web打印社区13 小时前
C-Lodop 提示未准备好或 WebSocket 没准备好:先让本机服务起来
javascript·网络·websocket·网络协议·pdf·html
qq_3691736315 小时前
一句话将 Word、PPT、PDF 发布成链接
人工智能·pdf·word·powerpoint·效率工具·ai 办公
泡海椒1 天前
数据占比展示:jquick-pdf饼图动态数据渲染PDF教程
数据库·oracle·pdf
Dovis(誓平步青云)1 天前
多个链接不等于多份证据,新闻核验看板怎样合并来源
java·服务器·前端·javascript·人工智能·pdf·电脑
web打印社区2 天前
Windows 网页静默打印设置步骤:客户端、防火墙与联调清单
开发语言·前端·javascript·chrome·pdf·ecmascript
开开心心就好2 天前
图片白底怎么去掉?抠图工具抠完背景透明
java·服务器·开发语言·pdf·ocr·散列表·启发式算法
web打印社区2 天前
JS 静默打印怎么做:纯前端为什么不行,以及最小可跑通写法
开发语言·前端·javascript·websocket·网络协议·http·pdf
web打印社区2 天前
HTML5 静默打印:前端页面怎么调用打印机且尽量不弹窗
前端·javascript·vue.js·pdf·html·html5
晚来的棠棠2 天前
27考研数学超全资料PDF
考研·pdf
正经教主2 天前
【FDE系列】阶段3:Day 63:文档解析 — 把真实 PDF 手册变成可用文本
人工智能·pdf·fde