JAVA使用aspose实现word文档转pdf文件
引入jar包
下载地址:https://yvioo.lanzous.com/iezpdno3mob
然后打开下载的目录打开cmd执行
mvn install:install-file -Dfile=aspose-words-15.8.0-jdk16.jar -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=15.8.0 -Dpackaging=jar
这是把jar包安装到本地仓库中
这样在pom文件里引入
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>15.8.0</version> </dependency>
当然也可以直接使用jar包
然后在项目根目录中创建一个文件(SpringBoot项目直接在resources下)
license.xml
<License> <Data> <Products> <Product>Aspose.Total for Java</Product> <Product>Aspose.Words for Java</Product> </Products> <EditionType>Enterprise</EditionType> <SubscriptionExpiry>20991231</SubscriptionExpiry> <LicenseExpiry>20991231</LicenseExpiry> <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber> </Data> <Signature> sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU= </Signature> </License>
使用工具类
AsposeUtil.java
import com.aspose.words.Document; import com.aspose.words.License; import com.aspose.words.SaveFormat; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; /** * @author yvioo。 */ public class AsposeUtil { /** * 验证License 若不验证则转化出的pdf文档会有水印产生 * @return */ public boolean getLicense() { boolean result = false; try { InputStream is =this.getClass().getClassLoader().getResourceAsStream("license.xml"); License aposeLic = new License(); aposeLic.setLicense(is); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } public static void main(String[] args) throws Exception { AsposeUtil bean = new AsposeUtil(); bean.word2Pdf2("C:\\1.docx","D:\\TEST.pdf"); } /** * word转pdf * inpath: 输入word的路径 * outpath: 输出pdf的路径 */ public void word2Pdf2(String inpath,String outpath) throws Exception { if (!getLicense()) { System.out.println("非法------------"); return; } long old = System.currentTimeMillis(); File file = new File(outpath); FileOutputStream os = new FileOutputStream(file); //解决乱码 //如果是windows执行,不需要加这个 //TODO 如果是linux执行,需要添加这个***** //FontSettings.setFontsFolder("/usr/share/fonts",true); Document doc = new 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) + "秒"); } /** * word转pdf * @param path pdf输出路径 * @param wordInput word输入流 * @param wordName word文档的名称 */ public void word2pdf(String path, InputStream wordInput, String wordName) throws FileNotFoundException { if (!getLicense()) { System.out.println("非法"); return; } //新建一个空白pdf文档 long old = System.currentTimeMillis(); File file = new File(path + wordName + ".pdf"); FileOutputStream os = new FileOutputStream(file); //Address是将要被转化的word文档 Document doc = null; try { doc = new Document(wordInput); } catch (Exception e) { e.printStackTrace(); } try { //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换 doc.save(os, SaveFormat.PDF); } catch (Exception e) { e.printStackTrace(); } long now = System.currentTimeMillis(); //转化用时 System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); } }
-----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------
(蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了