aspose office转pdf java

仅供学习 请勿商用!

package com.wisdom.util;

import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import com.aspose.cells.Workbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;


/**
 * Created by tanly on 2018/10/18 0018.
 */
public class TanlyTtest {

    public static boolean getLicense(String type) {
        boolean result = false;
        try {
            File file = new File("C:/Users/Administrator/Desktop/aposeLic/license.xml");
            InputStream is = new FileInputStream(file);
            if (type.equals("excel")) {
                com.aspose.cells.License aposeLic = new com.aspose.cells.License();
                aposeLic.setLicense(is);
            } else if (type.equals("ppt")) {
                com.aspose.slides.License aposeLic = new com.aspose.slides.License();
                aposeLic.setLicense(is);
            } else {
                com.aspose.words.License aposeLic = new com.aspose.words.License();
                aposeLic.setLicense(is);
            }
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        String rootPath = "C:/Users/Administrator/Desktop/aposeLic";
        File dir = new File(rootPath + "/source/");
        File[] all = dir.listFiles();
        if (all == null) {
            return;
        }
        for (File sfile : all) {
            String fileType = sfile.getName().substring(sfile.getName().lastIndexOf(".") + 1, sfile.getName().length());
            if ("docx".equals(fileType) || "doc".equals(fileType) || "rtf".equals(fileType) || "txt".equals(fileType)) {
                if (getLicense("word")) {//WORD
                    try {
                        long start = System.currentTimeMillis();
                        File pdfFile = new File( rootPath + "/target/" + sfile.getName().substring(0, sfile.getName().lastIndexOf(".")) + ".pdf");
                        FileOutputStream os = new FileOutputStream(pdfFile);
                        Document doc = new Document(sfile.getAbsolutePath());
                        doc.save(os, com.aspose.words.SaveFormat.PDF);
                        System.out.println("固化:" + fileType + " 耗时:" + ((System.currentTimeMillis() - start) / 1000.0) + "秒");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println("WORD证书失败");
                }
            } else if ("xls".equals(fileType) || "xlsx".equals(fileType) || "xlsm".equals(fileType)) {
                if (getLicense("excel")) {//EXCEL
                    try {
                        long start = System.currentTimeMillis();
                        Workbook wb = new Workbook(sfile.getAbsolutePath());
                        File pdfFile = new File( rootPath + "/target/" + sfile.getName().substring(0, sfile.getName().lastIndexOf(".")) + ".pdf");
                        FileOutputStream fileOS = new FileOutputStream(pdfFile);
                        wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);
                        System.out.println("固化:" + fileType + " 耗时:" + ((System.currentTimeMillis() - start) / 1000.0) + "秒");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println("EXCEL证书失败");
                }
            } else if ("ppt".equals(fileType) || "pptx".equals(fileType) || "pps".equals(fileType)) {
                if (getLicense("ppt")) {//PPT
                    try {
                        long start = System.currentTimeMillis();
                        InputStream slides = new FileInputStream(sfile.getAbsoluteFile());
                        Presentation pres = new Presentation(slides);
                        File pdfFile = new File( rootPath + "/target/" + sfile.getName().substring(0, sfile.getName().lastIndexOf(".")) + ".pdf");
                        FileOutputStream fileOS = new FileOutputStream(pdfFile);
                        pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf);
                        System.out.println("固化:" + fileType + " 耗时:" + ((System.currentTimeMillis() - start) / 1000.0) + "秒");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println("PPT证书失败");
                }
            } else {
                System.out.println("格式不支持:" + fileType);
            }
        }
        System.out.println("\n>>>>>>>>>固化结束!");
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值