word文档转pdf

1、导入所需依赖 aspose-words-15.8.0-jdk16.jar 下载

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

2、编写 license.xml license.xml授权码下载

备注:将license.xml放到resources下

<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>
    授权码
  </Signature>
</License>

3、所需加载license.xml方法

/**
 * <p>
 * 服务初始化之后,执行方法
 * </p>
 *
 * @author AJIE
 * @description
 * @date 2022/03/02 
 */
@Slf4j
@Component
public class StartAppRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        log.info("《服务初始化执行处理》 start...");
        try {
            log.info("实现`aspose-words`授权 -> 去掉头部水印");
            /*
              实现匹配文件授权 -> 去掉头部水印 `Evaluation Only. Created with Aspose.Words. Copyright 2003-2018 Aspose Pty Ltd.` |
                                          `Evaluation Only. Created with Aspose.Cells for Java. Copyright 2003 - 2020 Aspose Pty Ltd.`
             */
            InputStream is = new ClassPathResource("license.xml").getInputStream();
            License license = new License();
            license.setLicense(is);
        } catch (Exception e) {
            log.error("《`aspose-words`授权》 失败: {}", e.getMessage());
        }
        log.info("《服务初始化执行处理》 end...");
    }

}

4、word转pdf工具类

/**
 * <p>
 * word 转 pdf 工具类
 * </p>
 */
@Slf4j
public class Word2PdfUtil {

    /**
     * `word` 转 `pdf`
     *
     * @param wordBytes: word字节码
     * @return 生成的`pdf`字节码
     * @author li
     * @date  2022/03/02 
     */
    @SneakyThrows(Exception.class)
    public static byte[] wordBytes2PdfBytes(byte[] wordBytes) {
        Document document = new Document(new ByteArrayInputStream(wordBytes));
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        document.save(outputStream, SaveFormat.PDF);
        // 返回生成的`pdf`字节码
        return outputStream.toByteArray();
    }

    /**
     * `word` 转 `pdf`
     *
     * @param wordBytes:   word字节码
     * @param pdfFilePath: 需转换的`pdf`文件路径
     * @return 生成的`pdf`文件数据
     * @author li
     * @date  2022/03/02 
     */
    @SneakyThrows(Exception.class)
    public static File wordBytes2PdfFile(byte[] wordBytes, String pdfFilePath) {
        Document document = new Document(new ByteArrayInputStream(wordBytes));
        document.save(pdfFilePath, SaveFormat.PDF);
        return new File(pdfFilePath);
    }


    /**
     * 将文件转换成byte数组
     * @param filePath  文件File类 通过new File(文件路径)
     * @return byte数组
     */
    public static byte[] File2byte(File filePath) {
        byte[] buffer = null;
        try {
            FileInputStream fis = new FileInputStream(filePath);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }


    //将文件转换成Byte数组
    public static byte[] getBytesByFile(String pathStr) {
        File file = new File(pathStr);
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            byte[] data = bos.toByteArray();
            bos.close();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

        /**
         * 将Byte数组转换成文件
         * @param bytes  字节码
         * @param filePath  文件路径
         * @param fileName  文件名
         */
    public static void getFileByBytes(byte[] bytes, String filePath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory()) {// 判断文件目录是否存在
                dir.mkdirs();
            }
            file = new File(filePath + "\\" + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

注意:启动后如下图为成功!
在这里插入图片描述

制作不易,麻烦给个小赞 (如要转载,请备注出处)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜鸟小杰子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值