word、ppt、excel、txt转pdf——第一篇docx、doc

该文章介绍了一个Java方法,通过Aspose库将Word文档(doc和docx)转换为PDF格式,同时展示了如何使用正确的license.xml避免生成带有水印的PDF。文章提供了相关依赖的Maven配置,并包含代码示例进行文件转换。

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

word转pdf(用正确的license.xml,无水印,doc和docx都可转)

这是引用的jar,aspose-words-15.8.0-jdk16.jar,和文中License匹配。下载jar引用或者maven引用。(可以在下载后直接放入项目或者先添加到maven再用)。maven仓库用http://repo.e-iceblue.cn/repository/maven-public/

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<!-- word转pdf -->
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words</artifactId>
    <version>15.8.0</version>
    <classifier>jdk16</classifier>
</dependency>

下面是代码

package util;

import java.io.*;
import com.aspose.words.SaveFormat;
import com.aspose.words.Document;
import com.aspose.words.License;

public class WordToPdfUtils {
    /**
     * 验证aspose.word组件是否授权:无授权的文件有水印标记
     * 需要使用(aspose-words-15.8.0-jdk16.jar),版本要对应。无水印
     */
    public static boolean isWordLicense() {
        boolean result = false;
        try {
            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 inputStream = new ByteArrayInputStream(s.getBytes());
            License license = new License();
            license.setLicense(inputStream);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void wordToPdf(String oldPath, String outputPath) {
        //凭证 不然切换后有水印
        // 验证License
        if (!isWordLicense()) {
            return;
        }
        FileOutputStream os = null;
        try {
            //生成一个空的PDF文件
            File file = new File(oldPath);
            os = new FileOutputStream(file);
            //要转换的word文件
            Document doc = new Document(outputPath);
            doc.save(os, SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //测试方法
    public static void main(String[] args) {
        WordToPdfUtils.wordToPdf("word文件地址","生成pdf地址");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值