1 首先是引入 jar 包

jar 包资源在这里 大家可以下载使用链接描述-这个是需要积分的 大家可以回复小编 发给大家

在这里插入图片描述
maven 中引用外包的jar包,在你的 pom 文件中加载 jar 内容

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

当使用 maven 来打包jar 包发布时,需要注意配置将外部的jar包进行打包 includeSystemScope 标签配置为 true

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
           
        </plugins>
    </build>
2 word 转 pdf 核心
    /**
     * 
     * @param inPath 输入的 word 文件地址 
     * @param outPath 输出的 pdf 文件地址
     * @throws Exception
     */
    public void doc2pdf(String inPath, String outPath) throws Exception {
        
        if (!getLicense()) {
            // 验证License 若不验证则转化出的pdf文档会有水印产生
            throw new Exception("验证License 不通过");
        }
      
        long old = System.currentTimeMillis();

        // 创建 文档 Document
        Document doc = new Document(inPath);
        // 新建一个空白pdf文档
        File file = new File(outPath);
        //输出流
        FileOutputStream os = new FileOutputStream(file);
        // 将文档保存为 pdf 格式的文件
        doc.save(os, SaveFormat.PDF);
        // EPUB, XPS, SWF 相互转换
        long now = System.currentTimeMillis();
        logger.error("转换完成 共耗时 " + ((now - old) / 1000.0) + "秒");
    }
3 签名文件信息加载
    public boolean getLicense() {
        boolean result = false;
        try {
            Resource resource = new ClassPathResource("pdf/license.txt");
            License aposeLic = new License();
            aposeLic.setLicense(resource.getInputStream());
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException("加载验证文件失败 " + e.getMessage());
        }
        return result;
    }

license.txt中保存着签名文件

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

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐