• java实现word转pdf


    package cn.net.cobot.health.web.export.helper;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    
    import org.apache.commons.lang3.StringUtils;
    import org.apache.tika.io.IOUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import com.aspose.words.Document;
    import com.aspose.words.License;
    import com.aspose.words.PdfSaveOptions;
    import com.aspose.words.SaveFormat;
    
    import cn.net.cobot.health.web.util.report.AsposeUtil;
    
    public class AsposeHelper {
        private static Logger logger = LoggerFactory.getLogger(AsposeHelper.class);
    
        /**
         * word转pdf
         *
         * @param wordPath        word文件路径-含扩展名
         * @param pdfPath         转换后pdf文件保存的路径-含扩展名
         * @param includeBookmark 转换后的pdf文件中是否包含目录
         * @return String<br>
         *         为null,成功<br>
         *         不是null,失败信息
         */
        public static String convertWord2Pdf(String wordPath, String pdfPath, boolean includeBookmark) {
            try {
                getLicense();
            } catch (Exception e) {
                //if (logger.isErrorEnabled()) {
                    logger.error("aspose license无效", e);
                //}
                return "aspose license无效";
            }
            if (StringUtils.isBlank(wordPath) || StringUtils.isBlank(pdfPath)) {
                String err = "参数无效:请指定word文件名或者pdf文件名";
                //if (logger.isErrorEnabled()) {
                    logger.error(err);
                //}
                return err;
            }
            String ret = null;
            long st = System.currentTimeMillis();
            File pdfFile = new File(pdfPath);
            Document asposeDoc = null;
            FileOutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream(pdfFile);
                asposeDoc = new Document(wordPath);
                PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
                // 输出pdf格式文档
                pdfSaveOptions.setSaveFormat(SaveFormat.PDF);
                if (includeBookmark) {
                    // 设置3级doc书签需要保存到pdf的heading中
                    pdfSaveOptions.getOutlineOptions().setHeadingsOutlineLevels(4);
                    // 设置pdf中默认展开1级
                    pdfSaveOptions.getOutlineOptions().setExpandedOutlineLevels(1);
                }
                asposeDoc.save(outputStream, pdfSaveOptions);
                long end = System.currentTimeMillis();
                if (logger.isInfoEnabled()) {
                    logger.info("word转pdf[{}目录]:{},用时{}ms", (includeBookmark ? "" : "不含"), wordPath, (end - st));
                }
                ret = null;
            } catch (Exception e) {
                //if (logger.isErrorEnabled()) {
                    logger.info("word转pdf异常[" + (includeBookmark ? "" : "不含") + "目录]:" + wordPath, e);
                //}
                ret = "word转pdf异常";
            } finally {
                IOUtils.closeQuietly(outputStream);
            }
            return ret;
        }
    
        private static void getLicense() throws Exception {
            try (InputStream is = AsposeUtil.class.getClassLoader()
                    .getResourceAsStream("Aspose.Total.Product.Family.lic")) {
                License license = new License();
                license.setLicense(is);
            }
        }
    }
  • 相关阅读:
    ArcGIS三种方式打断相交线------Feature To Line工具
    ArcGIS三种方式打断相交线------Planarize Lines工具
    3种方法快速制作tpk文件 [转]
    将oracle冷备份恢复到另外一个数据库实例中
    常用ArcGIS for Silverlight 开发API介绍
    Bear + Reminders 是完美的Thing 3 的替代品
    2019科技盛会汇总
    电子产品使用感受之——为什么我把Apple Watch S2 升级到了 S4?
    我了解到的新知识之——电热水器用电安全
    我了解到的新知识之----遇到路由器DNS被篡改我该怎么办?
  • 原文地址:https://www.cnblogs.com/lxz123/p/16164126.html
Copyright © 2020-2023  润新知