Java 将word转为PDF

文章目录

  • 需要引入的依赖包
  • 工具类
  • 测试

需要引入的依赖包

<dependency>
    <groupId>cn.com.anfugroupId>
    <artifactId>aspose-pdfartifactId>
    <version>18.9version>
dependency>

<dependency>
    <groupId>cn.com.anfugroupId>
    <artifactId>aspose-words-jdk16artifactId>
    <version>18.5version>
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文件的相对位置
Java 将word转为PDF_第1张图片

测试

@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文件
}

Java 将word转为PDF_第2张图片
两个xml文件的内容如下:
license-pdf.xml

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

license-word.xml

<License>
  <Data>
    <Products>
      <Product>Aspose.Words for JavaProduct>
    Products>
    <EditionType>EnterpriseEditionType>
    <SubscriptionExpiry>20991231SubscriptionExpiry>
    <LicenseExpiry>20991231LicenseExpiry>
  Data>
License>

你可能感兴趣的:(Java,java,word,pdf)