word转pdf文档

word转pdf文档

1.依赖

其中的aspose-words依赖地址:https://releases.aspose.com/java/repo/com/aspose/

<!--word文档转化为pdf文档-->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>3.0.0-RC1</version>
        </dependency>
        <dependency>
            <groupId>com.github.jai-imageio</groupId>
            <artifactId>jai-imageio-jpeg2000</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-words</artifactId>
            <version>15.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>3.0.0-RC1</version>
        </dependency>
        <dependency>
            <groupId>com.github.jai-imageio</groupId>
            <artifactId>jai-imageio-jpeg2000</artifactId>
            <version>1.3.0</version>
        </dependency>

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

3.破解文件及格式转换方法

package com.fangzhou.units;

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;

/**
 * @Author: chongchong
 * @DateTime: 2023/9/25 16:43
 * @Description:
 */
public class AsposeUtil {
    /**
     * 加载license 用于破解 不生成水印
     *
     * @author LCheng
     * @date 2020/12/25 13:51
     */
    @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文件保存的路径
     * @author LCheng
     * @date 2020/12/25 13:51
     */
    @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);
        }
    }
}

4.转换文档格式

package com.fangzhou.wordtopdf;

import com.fangzhou.units.AsposeUtil;

/**
 * @Author: chongchong
 * @DateTime: 2023/9/25 14:37
 * @Description:
 */
public class ToPdf {

    public static void main(String[] args) throws Exception {

        //加载Word文档,此两种方式会出现水印,学习可用
     /*   Document doc = new Document("C:\\Users\\chongchong\\Desktop\\repositories\\23分型报告单模板.doc");
        //创建 PDF 保存选项对象
        *//*PdfSaveOptions saveOptions = new PdfSaveOptions();
        //将文档另存为 PDF
        doc.save("C:\\Users\\chongchong\\Desktop\\repositories\\23分型报告单.pdf", saveOptions);*//*
        doc.save("C:\\Users\\chongchong\\Desktop\\repositories\\23分型报告单.pdf", SaveFormat.PDF);*/

//   此种方式不会生成水印,真实场景可用  
        AsposeUtil.wordToPdf("C:\\Users\\chongchong\\Desktop\\repositories\\word\\23分型报告单模板.docx",
                "C:\\Users\\chongchong\\Desktop\\repositories\\23分型报告单.pdf");
    }
}
posted @ 2023-10-13 13:25  冲冲不会啊  阅读(74)  评论(0编辑  收藏  举报