【word转pdf】【最新版本jar】Java使用aspose-words实现word文档转pdf

【aspose-words-22.12-jdk17.jar】word文档转pdf

前置工作

1、下载依赖

通过百度网盘分享的文件:aspose-words-22.12-jdk17.jar

链接:https://pan.baidu.com/s/137_fNgHCKSSEqcavWGehtA?pwd=2xoe

提取码:2xoe

2、安装依赖到本地仓库

xxxxxxxxxx = jar下载之后的目录

复制代码
 mvn install:install-file -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=15.8.0 -Dpackaging=jar  -Dfile="F:\java\xxxxxxxxxx\aspose-words-15.8.0-jdk16.jar"

项目

1、配置pom.xml

java 复制代码
		<!-- word转pdf  -->
		<dependency>
             <groupId>com.aspose</groupId>
             <artifactId>aspose-words</artifactId>
             <version>15.8.0</version>
         </dependency>

2、配置许可码文件(不配置会有水印)

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

java 复制代码
<?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>

3、工具类

java 复制代码
package xxx.xxx.xx;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.online.config.exception.BusinessException;

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

/**
 * 使用Aspose将Word转换为PDF的工具类
 * 注意:需要在resource路径下配置license.xml文件
 */
@Slf4j
public class Word2PdfUtil {
    public static void main(String[] args) {
        String inputPath = "F:\\opt\\upFiles\\2024-07-24\\2024072411362566_1721792185072.docx";
        String outputPath = "C:\\Users\\123\\Desktop\\1\\667\\bbb.pdf";
        try {
            doc2pdf(inputPath, outputPath);
        } catch (BusinessException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将Word文档转换为PDF
     *
     * @param inPath  输入的Word文件路径
     * @param outPath 输出的PDF文件路径
     */
    public static void doc2pdf(String inPath, String outPath) throws BusinessException {
        // 如果未获取到许可证,则抛出业务异常
        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();
        }
    }

    public static ByteArrayOutputStream doc2pdfByteArrayOutputStream(InputStream inputStream)  {
        // 如果未获取到许可证,则抛出业务异常
        if (!getLicense()) {
            throw new JeecgBootException("未配置许可证!");
        }
        try {
            long startTime = System.currentTimeMillis();

            // 要转换的Word文档
            Document doc = new Document(inputStream);
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            doc.save(bao, SaveFormat.PDF);
            log.info("PDF转换完成,耗时:{}ms", System.currentTimeMillis() - startTime);
            return bao;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取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;
    }

}

4、效果

踩坑

1、pdf乱码

将win机器的c:\windows\fonts目录下的全部文件拷贝到生产服务器字体安装目录下,然后执行以下命令更新字体缓存

java 复制代码
# 创建目录,将win机器的c:\windows\fonts目录下的全部文件拷贝到此目录下
mkdir mkdir /usr/share/fonts/win
cd /usr/share/fonts 
sudo fc-cache -fv
# 执行命令让字体生效
source /etc/profile

2、word中带有图片转换

word中如果有jpg格式的图片,需要转换格式成 png

相关推荐
金銀銅鐵1 小时前
带您看懂字节码指令(番外二):递归里有什么——三层 fact 帧怎么叠起来
java·jvm·后端
两点王爷3 小时前
Java 面试高频:JWT、Cookie、Session、Token 鉴权详解
java·开发语言·面试
悟道书生5 小时前
hashMap/hashSet/hashCode详解
java
nice_lcj5206 小时前
依赖注入的三种方式:构造器、Setter、字段怎么选
java·spring·面试·依赖注入·字段注入·构造器注入
噢,我明白了7 小时前
实际项目导出方式应用
java·状态模式·导出
جيون داد ناالام ميづ8 小时前
七、MyBatis 源码剖析:核心流程拆解(初始化 + SQL 执行)
java·sql·mybatis
ClkLog-开源埋点用户分析8 小时前
【产品说明】【埋点分析】ClkLog 商业版 Java 运行环境安全更新说明
java·开发语言
秋饼8 小时前
Spring Framework 7 内置弹性实战:@Retryable 语义翻转、虚拟线程流量放大与 LLM 网关治理
java·ai·技术分享·后端开发
RuoyiOffice8 小时前
Flowable只是起点:一套OA背后的单据、资源与协作设计
java·vue.js·spring boot·flowable·企业管理·oa·ruoyi office