pom引入本地jar

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

jar附件自取

aspose-words-15.8.0-jdk16.jar

修改pom打包的时候能把本地jar包打进去

增加一下语句即可

<includeSystemScope>true</includeSystemScope>

引用

import com.aspose.words.*;
import com.aspose.words.Document;

去除aspose.words组件的水印

在resources资源目录下新建文件 license.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>

解析license.xml

在生成pdf之前执行以下方法即可

private static boolean getLicense() {
    boolean result = false;
    InputStream is = null;
    try {
        Resource resource = new ClassPathResource("license.xml");
        is = resource.getInputStream();
        License aposeLic = new License();
        aposeLic.setLicense(is);
        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

words转pdf 并解决linux环境导出中文乱码问题

引入字体文件

在resources目录下新建fonts目录把下面附件字体文件放进去

simsun.ttc

实现word转pdf

public static byte[] wordBytes2PdfBytes(byte[] wordBytes) {
        try {
            getLicense();
            Document document = new Document(new ByteArrayInputStream(wordBytes));
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

            // 加载字体文件
            ClassPathResource fontResource = new ClassPathResource("fonts/simsun.ttc");
            InputStream fontStream = fontResource.getInputStream();
            // 将字体文件转换为字节数组
            byte[] fontBytes = IOUtils.toByteArray(fontStream);

            // 设置字体文件夹路径
            FontSettings.setFontsSources(new FontSourceBase[]{
                    new MemoryFontSource(fontBytes)
            });
//            FontSettings.setFontsFolder("fonts"+ File.separator, true);
            document.save(outputStream, SaveFormat.PDF);
            return outputStream.toByteArray();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }