Java 将word转为PDF

需要引入的依赖包

<dependency>
    <groupId>cn.com.anfu</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>18.9</version>
</dependency>

<dependency>
    <groupId>cn.com.anfu</groupId>
    <artifactId>aspose-words-jdk16</artifactId>
    <version>18.5</version>
</dependency>

工具类

@Slf4j
public class DocTransferUtil {
	//当类被加时就自动执行下方代码块
    static {
        //去掉转成PDF后的文件上方有Aspose的对应的版权信息,需要使用授权下方的license。
        try {
            // 设置word license
            ClassPathResource classPathResource = new ClassPathResource("license/license-word.xml");
            InputStream is = classPathResource.getInputStream();
            License aposeLic = new License();
            aposeLic.setLicense(is);

            // 设置pdf license
            ClassPathResource classPathResource2 = new ClassPathResource("license/license-pdf.xml");
            InputStream licensePdf = classPathResource2.getInputStream();
            com.aspose.pdf.License aposeLicPdf = new com.aspose.pdf.License();
            aposeLicPdf.setLicense(licensePdf);
            is.close();
            licensePdf.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

	/**
     * word转pdf
     *
     * @param word
     * @return
     */
    public static byte[] word2pdf(byte[] word) {
        try {
            long old = System.currentTimeMillis();
            //Address是将要被转化的word文档
            Document doc = new Document(new ByteArrayInputStream(word));
            //新建一个空白pdf文档
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            doc.save(os, SaveFormat.PDF);
            long now = System.currentTimeMillis();
            //转化用时
            log.info("word2pdf 耗时 {} 秒", ((now - old) / 1000.0));
            return os.toByteArray();
        } catch (Exception e) {
            log.info("word2pdf 异常:"+e);
            throw new RuntimeException(e.getMessage());
        }
    }
 }

两个xml文件的相对位置
在这里插入图片描述

测试

@GetMapping("/testword2pdf")
publicvoid testword2pdf() throws Exception {
    byte[] src = cn.hutool.core.io.FileUtil.readBytes("C:\\Users\\Administrator\\Desktop\\测试文件.docx");//原始的word文件
    long old = System.currentTimeMillis();
    byte[] target = DocTransferUtil.word2pdf(src);
    long now = System.currentTimeMillis();
    //转化用时
    log.info("word2pdf 耗时 {} 秒", ((now - old) / 1000.0));
    cn.hutool.core.io.FileUtil.writeBytes(target, "C:\\Users\\Administrator\\Desktop\\123432.pdf");//生成的pdf文件
}

在这里插入图片描述
两个xml文件的内容如下:
license-pdf.xml

<License>
  <Data>
    <Products>
      <Product>Aspose.Total for Java</Product>
    </Products>
    <EditionType>Enterprise</EditionType>
    <SubscriptionExpiry>29991231</SubscriptionExpiry>
    <LicenseExpiry>29991231</LicenseExpiry>
    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
  </Data>
<Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>

license-word.xml

<License>
  <Data>
    <Products>
      <Product>Aspose.Words for Java</Product>
    </Products>
    <EditionType>Enterprise</EditionType>
    <SubscriptionExpiry>20991231</SubscriptionExpiry>
    <LicenseExpiry>20991231</LicenseExpiry>
  </Data>
</License>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值