网站首页 > 博客文章 正文
今天有个做人事的朋友在整理合同,大概有200多个word文档,希望转换为pdf格式。免费的方式只能一个一个转,批量的收费,咋办。
体现java强大的时刻到了。我就顺手写了个批量将word转为pdf的java代码。
一 jar包下载
使用的是
aspose-words-15.12.0-jdk16.jar 这个jar,
下载链接:
https://pan.baidu.com/s/1X_gO1cwLUzoHGtX8UmF6XA 提取码: a3f2
二 pom.xml文件配置
将下载的jar包复制到 resources/lib目录下。
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.12.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/aspose-words-15.12.0-jdk16.jar</systemPath>
</dependency>
三 配置license.xml
如果不配置的话,会有水印。
在 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>
四 java代码
public class WordToPdfTest {
public static void main(String[] args) throws Exception {
//org.springframework.core.io.ClassPathResource
//加载licence.xml
InputStream inputStream = new ClassPathResource("/license.xml").getInputStream();
License licenseObj = new License();
licenseObj.setLicense(inputStream);
//word文件存放的目录
File sourcedir = new File("D:\\tmp\\dev_source");
File[] sourceFiles = sourcedir.listFiles();
//循环遍历word文件
for (File file : sourceFiles) {
//获取带后缀的文件名 123.docx
String sourceName = file.getName();
//去掉文件名后缀
String targetName = sourceName.split("\\.")[0];
FileOutputStream os = null;
try {
//以原文件名 创建一个 pdf
File targetFile = new File("D:\\tmp\\dev_target\\" + targetName + ".pdf");
//构建pdf文件输出流
os = new FileOutputStream(targetFile);
//已file的文件绝对路径为参数,获取Document对象
Document doc = new Document(file.getAbsolutePath());
//将文档内容 以pdf格式 输出到上面的输出流。
doc.save(os, SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
os.close();
}
}
}
}
}
源文件 目录
执行完上述代码后
试验成功。欢迎大家尝试。
猜你喜欢
- 2025-05-09 介绍一款国产开源免费的在线文件文档预览的kkFileView
- 2025-05-09 Tomcat介绍和配置使用(tomcat的常用配置)
- 2025-05-09 厉害!简短精致:IDEA SpringBoot 免重启热部署(热加载)
- 2025-05-09 Go使用gotk开发跨平台桌面程序,打包压缩后仅8MB
- 2025-05-09 Spring路径-10-SpringBoot开发部署与测试
- 2025-05-09 音乐盒Java在线音乐jsp源代码Mysql
- 2025-05-09 Optimizer For Eclipse,性能大有不同!
- 2025-05-09 Spring-ai-alibaba以及阿里云百炼的简单使用
- 2025-05-09 信创终端 PDF 处理神器:免费使用 hhdesk 轻松合并与拆分 PDF!
- 2025-05-09 Mcinabox安装包和运行库分享(蓝奏云网盘)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (61)
- googlecloud (64)
- messagesource (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)