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

相关推荐
百事牛科技1 小时前
保护文档安全:PDF限制功能详解与实操
windows·pdf
开开心心就好8 小时前
安卓开源应用,超时提醒紧急人护独居安全
windows·决策树·计算机视觉·pdf·计算机外设·excel·动态规划
骆驼爱记录10 小时前
WPS页码设置:第X页共Y-1页
自动化·word·excel·wps·新人首发
2301_8169978811 小时前
Word 清除格式的方法
word
微光feng1 天前
毕业论文word引用操作汇总
word·目录·公式·毕业论文·交叉引用·题注
2301_816997881 天前
Word 功能区与快速访问工具栏
word
halen3331 天前
Hellowordl: The Masters Tool for Word Puzzle Enthusiasts
word
lpfasd1231 天前
Markdown 导出 Word 文档技术方案
开发语言·c#·word
Cxiaomu1 天前
Python 文件解析: Excel / Word / PDF 的解析、处理、预览与下载
python·word·excel
Felicia-侧听1 天前
如何统一PDF页面宽度?统一pdf宽度的2种方法
pdf·pdf宽度统一