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

相关推荐
叫我莫言鸭19 小时前
关于word生成报告的POI学习2循环标题内容
java·学习·word
December31020 小时前
新手入门指南:PDF页面方向怎么旋转?
pdf·pdf页面旋转·旋转pdf页面方向·pdf旋转
Summer不秃20 小时前
使用 SnapDOM + jsPDF 生成高质量 PDF (含多页分页, 附源码)
前端·javascript·vue.js·pdf·node.js
bosins21 小时前
基于Python实现PDF文件个人隐私信息检查
开发语言·python·pdf
bosins21 小时前
基于Python开发PDF文件元数据查看器
开发语言·python·pdf
2501_946213901 天前
Zettlr(科研笔记) v4.0.0 中文免费版
java·经验分享·笔记·pdf·github·ocr·开源软件
java_logo1 天前
MinerU Docker 部署指南:PDF 结构化解析服务实践
docker·容器·pdf·mineru·mineru部署·mineru部署文档·mineru部署教程
教练、我想打篮球1 天前
127 apache poi3.11 写 word 中内嵌 表格换行的输出
word·excel·docx·换行
SiYuanFeng1 天前
【Word 排版】文本框/文字行间距异常偏大的解决方案
word
belldeep2 天前
python:markdown + python-docx 将 Markdown 文件格式转为 Word 文档
python·word·markdown·docx