记录 word转pdf的工具

word转pdf可以使用  aspose-words-15.8.0-jdk16.jar 这个jar包来进行格式转换,也是用过poi但是转换的样式感觉没有这个好。

我本地的JDK是1.8的

1、首先在Resources下面添加 对应的xml(License.xml)文件,用来去除水印

<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、对应的工具类代码

public class WordUtils {
        /**
         * 加载license 用于破解 不生成水印
         */
        @SneakyThrows
        private static void getLicense() {
            try (InputStream is = WordUtils.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);
            }
        }
}

3、对应的jar引用,这里是将jar包放入 src同级的lib目录里面。代码打包的时候记得将lib目录下的jar也打进去

        <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>

 

posted @ 2024-04-30 08:45  烟雨蒙尘  阅读(3)  评论(0编辑  收藏  举报