Java使用aspose-words实现word文档转pdf

Java使用aspose-words实现word文档转pdf

1.获取转换jar文件并安装到本地maven仓库

aspose-words-15.8.0-jdk16.jar包下载地址:https://zhouquanquan.lanzn.com/b00g257yja 密码:965f

下载aspose-words-15.8.0-jdk16.jar包后,通过maven命令手动安装到本地maven仓库

shell 复制代码
 mvn install:install-file -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=15.8.0 -Dpackaging=jar  -Dfile="C:\Users\zhouquan\Desktop\aspose-words-15.8.0-jdk16.jar"
 
 # -DgroupId:pom.xml中groupId
 # -DartifactId:pom.xml中artifactId
 # -Dversion:pom.xml中0.0.1-SNAPSHOT
 # -Dpackaging:jar或war,包的后缀名
 # -Dfile:包的真实地址

pom.xml

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

在resource路径下配置license.xml文件

xml 复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<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>

2. 转换代码

java 复制代码
package com.cxstar.common.utils;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.cxstar.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;

import java.io.FileOutputStream;
import java.io.InputStream;

/**
 * 使用Aspose将Word转换为PDF的工具类
 * 注意:需要在resource路径下配置license.xml文件
 */
@Slf4j
public class Word2PdfUtil {

    /**
     * 将Word文档转换为PDF
     *
     * @param inPath  输入的Word文件路径
     * @param outPath 输出的PDF文件路径
     */
    public static void doc2pdf(String inPath, String outPath) {
        // 如果未获取到许可证,则抛出业务异常
        if (!getLicense()) {
            throw new BusinessException("未配置许可证!");
        }
        try (FileOutputStream os = new FileOutputStream(outPath)) {
            long startTime = System.currentTimeMillis();

            // 要转换的Word文档
            Document doc = new Document(inPath);
            // 保存为PDF格式
            doc.save(os, SaveFormat.PDF);

            log.info("PDF转换完成,耗时:{}ms,输出路径:{}", System.currentTimeMillis() - startTime, outPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取Aspose的许可证
     * 如果未获取则转换的pdf带有水印
     *
     * @return 是否成功获取许可证
     */
    public static boolean getLicense() {
        boolean result = false;
        // license.xml文件应放在resource路径下
        try (InputStream is = Word2PdfUtil.class.getClassLoader().getResourceAsStream("license.xml");) {
            License license = new License();
            license.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}

3. 转换效果对比

图1. 使用aspose-words进行转换

图2.使用openoffice进行转换

相关推荐
哭哭啼13 小时前
JAVA服务问题诊断
java·开发语言·jvm
Sayuanni%314 小时前
SpringBoot 从注解到源码:核心知识点总结
java·spring boot·后端
坚定信念,勇往无前14 小时前
Maven 私有仓库-nexus
java
Minner-Scrapy14 小时前
Scrapy 2.17 源码解析:Scheduler 调度器与磁盘/内存双队列
java·爬虫·python·scrapy·网络爬虫·twisted
晚风醉蝶15 小时前
1-11-奇偶排序-OddEvenSort
java·数据结构·算法
夏天拐跑了西瓜15 小时前
Spring Cloud 微服务实战(三):一个服务挂了,凭什么拖垮整个系统?Sentinel 限流熔断实战
java·spring cloud·微服务
2601_9672642815 小时前
Jetpack Compose 实践指南:从入门到进阶
java·学习
m0_5873830016 小时前
社区家政系统开发实战:从需求分析到上线部署全指南
java·spring boot·spring·需求分析
凤山老林16 小时前
动态 i18n 体系落地:Spring Boot 多租户热加载与前后端协同实践
java·spring boot·后端·i18n
白露与泡影16 小时前
解密 Pi 的 Harness 工程:Agent 会话如何实现持久化与恢复
java·人工智能·算法