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

相关推荐
DS随心转小程序29 分钟前
ChatGPT和Gemini公式
人工智能·chatgpt·aigc·word·豆包·deepseek·ds随心转
蒝莱茹茈5 小时前
pdf-engine发布
pdf·pdfium
fl1768315 小时前
基于python实现PDF批量加水印工具
开发语言·python·pdf
lqj_本人5 小时前
Flutter PDF 渲染插件(pdf_image_renderer)适配鸿蒙 (HarmonyOS) 平台实战
flutter·pdf·harmonyos
mudtools7 小时前
C#中基于Word COM组件的数学公式排版实践
开发语言·c#·word
gc_22998 小时前
学习C#调用OpenXml操作word文档的基本用法(18:学习文档页眉类)
c#·word·openxml·页眉·headerpart
meng半颗糖1 天前
vue3+tpescript 点击按钮跳转新页面直接通过链接预览word
前端·vue.js·word
普通网友1 天前
Docker 磁盘占用分析和清理方法
pdf
盐焗西兰花1 天前
鸿蒙学习实战之路-PDF转换指定页面或指定区域为图片
学习·pdf·harmonyos
切糕师学AI1 天前
轻量pdf阅读器推荐
pdf