完美解决word转pdf(亲测有效)

本文介绍了如何使用AsposeWords库将Word文档转换为PDF,包括下载jar包、配置许可证和编写工具类的步骤。作者分享了一个示例,展示了如何调用wordToPdf方法进行转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

word搞了一天,找了无数的资料,以下这种方法是最好的。。。什么只能转3页了。。。什么字体库了。。。。各种坑

就三步

第一步:下载jar包,然后放在这里
在这里插入图片描述

第二步:引入jar包,放在pom.xml文件

<dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-words</artifactId>
            <version>15.8.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/aspose-words-15.8.0-jdk16.jar</systemPath>
</dependency>

第三步:写工具类

package com.xxx.util;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import lombok.SneakyThrows;

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

public class AsposeUtil {

        /**
         * 加载license 用于破解 不生成水印
         */
        @SneakyThrows
        private static void getLicense() {
            try (InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("License.xml")) {
                License license = new License();
                license.setLicense(is);
            }
        }

        /**
         * word转pdf
         * @param wordPath word文件保存的路径
         * @param pdfPath  转换后pdf文件保存的路径
         */
        @SneakyThrows
        public static void wordToPdf(String wordPath, String pdfPath) {
            getLicense();
            File file = new File(pdfPath);
            try (FileOutputStream os = new FileOutputStream(file)) {
                Document doc = new Document(wordPath);
                doc.save(os, SaveFormat.PDF);
            }
        }


}

最后就能直接用了,可以参考我的

 		String inPath = "D:\\/java" + "\\/123456.docx";
 		String outPath = "D:\\/java" + "\\/123456.pdf";
        AsposeUtil asposeUtil = new AsposeUtil();
        asposeUtil.wordToPdf(wordPath,pdfPath);

以下可能就是你需要的jar包咯~
链接: https://pan.baidu.com/s/1A4OZKApDA-CgcpmN-XaCag 提取码: 2f66 复制这段内容后打开百度网盘手机App,操作更方便哦

觉得有帮助的话,帮点个赞哦~多谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值