将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 小时前
75_Python自动化办公之Word与PDF
python·自动化·word
Am-Chestnuts6 小时前
DeepSeek 表格如何导出 Word/Excel:Markdown 表格、CSV 与 DS随心转方案对比
word·excel
Am-Chestnuts7 小时前
AI 公式复制到 Word 乱码怎么办:LaTeX 转 Word 与 DS随心转方案对比
人工智能·word
wenxin77wx18 小时前
3步部署OlmOCR:一行命令将PDF转为LLM训练数据
pdf·- olmocr
ShiXZ21320 小时前
PDF-OCR文件识别篇(七):数据入库
java·pdf·json·ocr·springboot
hong78171 天前
西门子触摸屏PROSAVE传输PDF组件没有选项
pdf·触摸屏·西门子
王莎莎-MinerU1 天前
当 RealDocBench 开始追问字段级问答,MinerU 为什么更适合做知识库与科研 Agent 的文档验收层
大数据·人工智能·算法·pdf·ocr
Eiceblue1 天前
使用 JavaScript 在 React 中实现 Word 转 PDF
javascript·react.js·word
weixin_3975740918 天前
PDF复杂表格的1:1还原引擎:跨页表格自动拼接技术实战
大数据·人工智能·pdf
Metaphor69218 天前
使用 Python 将 PDF 转换为 HTML
python·pdf·html