Java使用Aspose-Words实现Word转换Pdf
1、Maven命令安装jar包到本地仓库
mvn install:install-file -Dfile=jar包的位置 -DgroupId=上面的groupId -DartifactId=上面的artifactId -Dversion=上面的version -Dpackaging=jar
eg、mvn install:install-file -Dfile=C:\Users\admin\Desktop\doc\aspose-words-15.8.0-jdk16.jar -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=15.8.0 -Dpackaging=jar
2、引入Aspose-Words依赖
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8.0</version>
</dependency>
3、license.xml文件内容(放在/src/main/resources目录下)
<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>
4、WordPdfUtil.java——封装工具类
package com.lynch.asap.config.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
/**
* Word 转 Pdf 帮助类
*
* @author admin
*
*/
public class WordPdfUtil {
private static boolean license = false;
public static void main(String[] args) {
// doc2pdf("C:\\Users\\admin\\Desktop\\doc\\111.docx","C:\\Users\\admin\\Desktop\\doc\\aaa.pdf");
// doc2pdf("C:\\Users\\admin\\Desktop\\doc\\222.docx","C:\\Users\\admin\\Desktop\\doc\\bbb.pdf");
// doc2pdf("C:\\Users\\admin\\Desktop\\doc\\333.docx","C:\\Users\\admin\\Desktop\\doc\\ccc.pdf");
doc2pdf("/root/micro/333.docx","/root/micro/ccc.pdf");
}
static {
try {
// license.xml放在src/main/resources文件夹下
InputStream is = WordPdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
license = true;
} catch (Exception e) {
license = false;
System.out.println("License验证失败...");
e.printStackTrace();
}
}
/**
* doc转pdf
*
* @param wordPath
* @param pdfPath
*/
public static void doc2pdf(String wordPath, String pdfPath) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!license) {
System.out.println("License验证不通过...");
return;
}
try {
long old = System.currentTimeMillis();
//新建一个pdf文档
File file = new File(pdfPath);
FileOutputStream os = new FileOutputStream(file);
//Address是将要被转化的word文档
Document doc = new Document(wordPath);
//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
doc.save(os, SaveFormat.PDF);
long now = System.currentTimeMillis();
os.close();
//转化用时
System.out.println("Word 转 Pdf 共耗时:" + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
System.out.println("Word 转 Pdf 失败...");
e.printStackTrace();
}
}
}
该段代码如果想要在Linux服务器上完美运行,需要给Linux服务器安装中文字体库,否则所有中文字符将会乱码,点击此处查看解决方案
分类:
Java后端架构
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
2017-08-26 Disruptor入门