word转pdf
Word文档转换成PDF
工具类代码:
import com.aspose.words.SaveFormat; import java.io.File; import java.io.FileOutputStream; /** * word 转 pdf */ public class Word2Pdf { /** * word转pdf * inpath: 输入word的路径 * outpath: 输出pdf的路径 */ public static String wordTOPdf(String inPath, String outPath,String fileName) throws Exception { if(!new File(outPath).exists()){ new File(outPath).mkdirs(); } // pdf 路径 String pdfPath = outPath + "/" + fileName + ".pdf"; File file = new File(pdfPath); // 生成pdf os FileOutputStream os = new FileOutputStream(file); //解决乱码 //如果是windows执行,不需要加这个 //TODO 如果是linux执行,需要添加这个***** //FontSettings.setFontsFolder("/usr/share/fonts",true); com.aspose.words.Document doc = new com.aspose.words.Document(inPath); //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换 doc.save(os, SaveFormat.PDF); // long now = System.currentTimeMillis(); // System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); return pdfPath; } }
用到的Jar包:
<!-- word转pdf(依赖windows本地的wps) -->
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>19.2</version> </dependency>
[注]:需要手动安装依赖jar包
mvn install:install-file -Dfile=D:\apache-maven-3.3.9\lib\aspose-words-19.2-jdk16.jar -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=19.2 -Dpackaging=jar
-Dfile 后边是jar包的路径,就是刚刚第一步的路径
-DgroupId:这个就是maven引用中的groupId,如下边的那个maven引用
-DartifactId : 这个是artifactId
-Dversion:这个是版本号,如果没有可以自己定义一个,到时候引用的时候会用到
-Dpackaging:这个一般都是jar,因为你引入的就是jar包
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示