Java实现word转PDF

使用 aspose-words 进行转换

方案一:

xml 复制代码
<dependency>
  <groupId>com.aspose</groupId>
  <artifactId>aspose-words</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/src/main/resources/aspose-words-15.8.0-jdk16.jar</systemPath>
</dependency>

因为 aspose-words-15.8.0-jdk16.jar 从中央仓库拉取不到,所以从固定位置加载(如果能拉取到就直接指定),其中
system 表示这个依赖项将从本地文件系统的一个具体位置加载,而不是从Maven中央仓库或其他远程仓库。
systemPath 指定了JAR文件在本地系统上的确切路径。

方案二:

xml 复制代码
<dependency>
  <groupId>com.luhuiguo</groupId>
  <artifactId>aspose-words</artifactId>
  <version>22.10</version>
</dependency>

这个中央仓库能拉取到

代码实现:

java 复制代码
public void exportFile() {
    // wordPath 表示word文档地址
    String wordPath = "E:\IdeaProjects\files\download\cesdata.docx";
    File file = new File(wordPath);
    // 获取文件数据
    byte[] fileData = getFileData(wordPath);
    // pdfPath 表示PDF输出地址
    String pdfPath = "E:\IdeaProjects\files\download\cesdata.pdf";

    // 使用方案一需要设置license,不设置会有水印(方案二测试不设置也不会有水印,加上也行)
    String s = "<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>";
    ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
    License license = new License();
    license.setLicense(is);

    // 导出文件
    try (InputStream inputStream = new ByteArrayInputStream(fileData)) {
        com.aspose.words.Document document = new com.aspose.words.Document(inputStream);
        document.save(Files.newOutputStream(new File(pdfPath).toPath()), SaveFormat.PDF);
    }
}
java 复制代码
// 获取文件数据
public byte[] getFileData(String filePath) {
    try (FileInputStream fis = new FileInputStream(filePath);
         ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
        byte[] buffer = new byte[1024];
        int length;
        while ((length = fis.read(buffer)) != -1) {
            bos.write(buffer, 0, length);
        }
        return bos.toByteArray();
    } catch (Exception err) {
        return null;
    }
}
相关推荐
weixin_4723394618 分钟前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
小毛驴8501 小时前
Linux 后台启动java jar 程序 nohup java -jar
java·linux·jar
DKPT1 小时前
Java桥接模式实现方式与测试方法
java·笔记·学习·设计模式·桥接模式
Eiceblue2 小时前
【免费.NET方案】CSV到PDF与DataTable的快速转换
开发语言·pdf·c#·.net
好奇的菜鸟3 小时前
如何在IntelliJ IDEA中设置数据库连接全局共享
java·数据库·intellij-idea
DuelCode4 小时前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis
优创学社24 小时前
基于springboot的社区生鲜团购系统
java·spring boot·后端
幽络源小助理4 小时前
SpringBoot基于Mysql的商业辅助决策系统设计与实现
java·vue.js·spring boot·后端·mysql·spring
猴哥源码4 小时前
基于Java+springboot 的车险理赔信息管理系统
java·spring boot
YuTaoShao5 小时前
【LeetCode 热题 100】48. 旋转图像——转置+水平翻转
java·算法·leetcode·职场和发展