Java使用aspose常用将txt、doc、docx、xls、xlsx、ppt、pptx、pdf、ofd文件转为图片,并提供官方下载地址

(1)新建license.xml文件,路径放在resources下 

(2)license.xml文件里面内容如下
<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)下载相关依赖jar包,只展示一个下载图片,j只展示了pdf下载官方地址Aspose.PDF 20.11 | Process Acrobat PDF Files via Java API

(4)相关依赖

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words</artifactId>
    <version>19.5-jdk</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/lib/aspose-words-19.5jdk.jar</systemPath>
</dependency>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-cells</artifactId>
    <version>8.5.2</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/lib/aspose-cells-8.5.2.jar</systemPath>
</dependency>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-slides</artifactId>
    <version>15.9.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/lib/aspose.slides-15.9.0.jar</systemPath>
</dependency>
<dependency>
    <groupId>com.aspose-pdf</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>20.11</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/lib/aspose-pdf-20.11.jar</systemPath>
</dependency>

(3)代码实现pdf转png

public static void pdfChangePng(InputStream input, OutputStream output) {
    // 验证License
    if (!getLicenseOfPDF()) {
        throw new RuntimeException("许可验证失败");
    }
    try {
        com.aspose.pdf.Document pdf = new com.aspose.pdf.Document(input);
        pdf.save(output, SaveFormat.PNG);
        output.flush();
        output.close();
        input.close();

    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("pdf转png失败");
    }
}
public static boolean getLicenseOfPDF() {
    boolean result = false;
    try {
        InputStream is = PictureUtil.class.getClassLoader().getResourceAsStream("license.xml");
        com.aspose.pdf.License aposeLic = new com.aspose.pdf.License();
        aposeLic.setLicense(is);
        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

(4)ppt转png

public static boolean getLicenseOfPPT() {
    boolean result = false;
    try {
        InputStream is = PictureUtil.class.getClassLoader().getResourceAsStream("license.xml");
        com.aspose.slides.License aposeLic = new com.aspose.slides.License();
        aposeLic.setLicense(is);
        result = true;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
public static void pptChangePng(InputStream input, OutputStream output) {
    // 验证License
    if (!getLicenseOfPPT()) {
        throw new RuntimeException("许可验证失败");
    }
    try {
        Presentation pres = new Presentation(input);
        pres.save(output, SaveFormat.PNG);
        output.flush();
        output.close();
        input.close();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("ppt转png失败");
    }

}

(5)其他类型不想写了,doc转png,还有excel转png,不会写的私我

(6)有些朋友可能需要把其他文件转成pdf,只需要,以ppt举例,改成下面这样就可以

com.aspose.slides.Presentation pres = new com.aspose.slides.Presentation(input);
pres.save(output, com.aspose.slides.SaveFormat.Pdf);

(7)需要将其他文件转pdf的朋友注意啦,转成的文件出现乱码的情况,需要在服务器上装字体库,不然转成的文件字体乱码,切记装字体库。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值