aspose office系列转换pdf文件

ASPOSE office系列转换PDF文件的正确姿势

相关jar和license.xml下载地址

1、jar

序号jar名称对应文件
1aspose.slidesPPT
2aspose-cellsEXCEL
3aspose-wordsWORD

2、pom依赖

<dependency>
            <groupId>acom.aspose.cells</groupId>
            <artifactId>aspose-cells-jdk16</artifactId>
            <version>15.8.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/aspose-cells-jdk16-8.5.2.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>aspose-words</groupId>
            <artifactId>aspose-words</artifactId>
            <version>15.8.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/aspose-words-jdk16-15.8.0.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>aspose.slides</groupId>
            <artifactId>slides</artifactId>
            <version>19.3</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/aspose.slides-19.3.jar</systemPath>
        </dependency>

3、代码


import java.io.*;
import java.util.Arrays;
import cn.hutool.core.collection.CollUtil;
import com.aspose.cells.Workbook;
import com.aspose.words.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aspose.slides.Presentation;

/**
 * office 转换 pdf
 *
 * @author TK
 * @since 2022-4-25
 */
public class Office2PdfUtil {

    private static final Logger log = LoggerFactory.getLogger(Office2PdfUtil.class);


    public static void main(String[] args) {

        String filename = "测试.doc";
        String inPath = "C:\\111\\pdf\\测试.doc";
        String outPath = "C:\\111\\pdf\\测试.pdf";

        Office2PdfUtil.toPdf(filename, inPath, outPath);
    }


    private static final String[] WORD = {"doc", "docx", "wps", "wpt", "txt"};
    private static final String[] EXCEL = {"xls", "xlsx", "et", "xlsm"};
    private static final String[] PPT = {"ppt", "pptx"};
    private static final String[] PDF = {"pdf"};
    private static final String[] IMG = {"bmp", "jpg", "png", "tif", "gif", "pcx", "tga", "exif", "fpx", "svg", "psd", "cdr", "pcd", "dxf", "ufo", "eps", "ai", "raw", "WMF", "webp", "avif", "apng"};

    private static final String TYPE_UNSUPPORT = "不支持的格式";
    private static final String TYPE_LICENSE = "证书过期";
    private static final String TYPE_SUCCESS = "转换成功";
    private static final String TYPE_ERROR = "转换失败";
    private static final String TYPE_WORD = "WORD";
    private static final String TYPE_EXCEL = "EXCEL";
    private static final String TYPE_PPT = "PPT";
    private static final String TYPE_PDF = "PDF";
    private static final String TYPE_IMG = "IMG";


    /**
     * 根据文件名判断文件类型
     *
     * @return
     */
    private static String getType(String fileName) {
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1); // 后缀
        if (CollUtil.contains(Arrays.asList(WORD), suffix)) {
            return TYPE_WORD;
        } else if (CollUtil.contains(Arrays.asList(EXCEL), suffix)) {
            return TYPE_EXCEL;
        } else if (CollUtil.contains(Arrays.asList(PPT), suffix)) {
            return TYPE_PPT;
        } else if (CollUtil.contains(Arrays.asList(PDF), suffix)) {
            return TYPE_PDF;
        } else if (CollUtil.contains(Arrays.asList(IMG), suffix)) {
            return TYPE_IMG;
        } else {
            return TYPE_UNSUPPORT;
        }
    }

    /**
     * Office 文件转换 PDF
     *
     * @param fileName
     * @param inPath
     * @param outPath
     * @return
     * @throws Exception
     */
    public static String toPdf(String fileName, String inPath, String outPath) {

        long old = System.currentTimeMillis();

        try {
            //获取文件后缀
            String type = getType(fileName);
            //验证文件类型
            switch (type) {
                case TYPE_UNSUPPORT:
                    return TYPE_UNSUPPORT;
                case TYPE_PDF:
                    return TYPE_UNSUPPORT;
                case TYPE_IMG:
                    return TYPE_UNSUPPORT;
                default:
                    break;
            }
            //License  验证
            if (!getLicense(type)) {
                log.info("License验证失败");
                return TYPE_LICENSE;
            }
            log.info("License验证成功........");
            log.info("开始本次转换........");
            // 新建一个空白pdf文档
            File file = new File(outPath);
            FileOutputStream os = new FileOutputStream(file);
            //根据类型转换文件
            switch (type) {
                case TYPE_WORD:
                    wordToPdfStream(inPath, os);
                    break;
                case TYPE_EXCEL:
                    excelToPdfStream(inPath, os);
                    break;
                case TYPE_PPT:
                    pptToPdfStream(inPath, os);
                    break;
                default:
                    break;
            }
            long now = System.currentTimeMillis();
            log.info("转换成功,共耗时:" + ((now - old) / 1000.0) + "秒........"); // 转化用时
        } catch (Exception e) {
            log.error("转换失败", e);
            return TYPE_ERROR;

        }
        return TYPE_SUCCESS;
    }


    /**
     * WORD to PDF
     *
     * @param inPath
     * @param os
     * @throws Exception
     */
    private static void wordToPdfStream(String inPath, FileOutputStream os) throws Exception {
        Document doc = new Document(inPath);
        doc.save(os, com.aspose.words.SaveFormat.PDF);
    }

    /**
     * EXCEL to PDF
     *
     * @param inPath
     * @param os
     * @throws Exception
     */
    private static void excelToPdfStream(String inPath, FileOutputStream os) throws Exception {
        Workbook excel = new Workbook(inPath);
        excel.save(os, com.aspose.cells.SaveFormat.PDF);
    }

    /**
     * PPT to PDF
     *
     * @param inPath
     * @param os
     * @throws Exception
     */
    private static void pptToPdfStream(String inPath, FileOutputStream os) throws Exception {
        Presentation ppt = new Presentation(inPath);
        ppt.save(os, com.aspose.slides.SaveFormat.Pdf);
    }


    /**
     * 获取License
     *
     * @return
     */
    public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = Excel2PdfUtils.class.getClassLoader().getResourceAsStream("\\license.xml");
            com.aspose.cells.License aposeLic = new com.aspose.cells.License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            log.error("License失效", e);
        }
        return result;
    }

    /**
     * 获取License
     *
     * @return
     */
    private static boolean getLicense(String type) {
        boolean result = false;
        try {

            //方式一:静态文件方式获取License
//            InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("\\license.xml");

            //方式二:字符串方式获取License
            String license = "<License>\n" +
                    "  <Data>\n" +
                    "    <Products>\n" +
                    "      <Product>Aspose.Total for Java</Product>\n" +
                    "    </Products>\n" +
                    "    <EditionType>Enterprise</EditionType>\n" +
                    "    <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
                    "    <LicenseExpiry>20991231</LicenseExpiry>\n" +
                    "    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
                    "  </Data>\n" +
                    "  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
                    "</License>";
            InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));


            if (type.equals(TYPE_WORD)) {
                com.aspose.words.License aposeLic = new com.aspose.words.License();
                aposeLic.setLicense(is);
                result = true;
            } else if (type.equals(TYPE_EXCEL)) {
                com.aspose.cells.License aposeLic = new com.aspose.cells.License();
                aposeLic.setLicense(is);
                result = true;
            } else if (type.equals(TYPE_PPT)) {
                com.aspose.slides.License aposeLic = new com.aspose.slides.License();
                aposeLic.setLicense(is);
                          result = true;
            } else {
                result = false;
            }
        } catch (Exception e) {
            result = false;
            log.error("License失效", e);
        }
        return result;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值