word,excel,ppt转pdf

☆樱花仙子☆ 提交于 2020-01-01 22:10:24
第一步 需要下载jar包和jacob-1.14.3-x64.dll *   <dependency> *      <groupId>net.sf.jacob-project</groupId> *      <artifactId>jacob</artifactId> *      <version>1.14.3</version> *   </dependency> *第二步 把jacob-1.14.3-x64.dll 放到java/bin目录下 电脑环境 win10  office2016支持文件类型 doc,docx,xls,xlsx,ppt,pptx
下面是源代码package com.example.demo.xs;import com.jacob.activeX.ActiveXComponent;import com.jacob.com.Dispatch;import com.jacob.com.Variant;import java.io.File;import java.util.logging.Level;import java.util.logging.Logger;/** * 需要jar包和jacob-1.14.3-x64.dll *   <dependency> *      <groupId>net.sf.jacob-project</groupId> *      <artifactId>jacob</artifactId> *      <version>1.14.3</version> *   </dependency> * *  把jacob-1.14.3-x64.dll 放到java/bin目录下 * * 电脑环境 win10  office2016 * @author 苑庆涛 * @create 2019-07-22 16:22 */public class Test {    private static Logger logger = Logger.getLogger(WordApp.class.getName());    public static void main(String[] args) {    }    /**     *  示例 toPdf("d:\\testold\\e3.xls","d:\\testnew\\e3.pdf","xlsx")     * @param src 文件路径     * @param tar pdf文件路径     * @param type 文件类型     */    public static void toPdf(String src,String tar,String type){        if(type.startsWith("doc")){            wordToPdf(src,tar);        }else if(type.startsWith("xls")){            excelToPdf(src,tar);        }else if(type.startsWith("ppt")){            pptToPdf(src,tar);        }    }    public static void wordToPdf(String src, String tar) {        ActiveXComponent app = null;        Dispatch doc = null;        try {            app = new ActiveXComponent("Word.Application");            app.setProperty("Visible", new Variant(false));            app.setProperty("AutomationSecurity", new Variant(3));            Dispatch docs = app.getProperty("Documents").toDispatch();            doc = Dispatch.call(docs, "Open",                    src, false, true)                    .toDispatch();            File target = new File(tar);            if (target.exists()) {                target.delete();            }            Dispatch.call(doc, "SaveAs", tar,17);        } catch (Throwable t) {            logger.log(Level.SEVERE, t.getMessage(), t);        } finally {            if (doc != null) {                try {                    Dispatch.call(doc, "Close", false);                } catch (Throwable t) {                    logger.log(Level.SEVERE, t.getMessage(), t);                }            }            if (app != null) {                try {                    app.invoke("Quit",0);                    app.safeRelease();                } catch (Throwable t) {                    logger.log(Level.SEVERE, t.getMessage(), t);                }            }        }    }    public static void excelToPdf(String src, String tar) {        ActiveXComponent app = null;        Dispatch doc = null;        try {            app = new ActiveXComponent("Excel.Application");            app.setProperty("Visible", new Variant(false));            app.setProperty("AutomationSecurity", new Variant(3));            Dispatch docs = app.getProperty("Workbooks").toDispatch();            doc = Dispatch.call(docs, "Open", src, false, true)                    .toDispatch();            File target = new File(tar);            if (target.exists()) {                target.delete();            }            Dispatch.call(doc, "ExportAsFixedFormat", 0, tar );        } finally {            if (doc != null) {                try {                    Dispatch.call(doc, "Close", false);                } catch (Throwable t) {                    logger.log(Level.SEVERE, t.getMessage(), t);                }            }            if (app != null) {                try {                    app.invoke("Quit");                    app.safeRelease();                } catch (Throwable t) {                    logger.log(Level.SEVERE, t.getMessage(), t);                }            }        }    }    public static void pptToPdf(String src, String tar) {        ActiveXComponent app = null;        Dispatch doc = null;        try {            app = new ActiveXComponent("PowerPoint.Application");            app.setProperty("AutomationSecurity", new Variant(3));            Dispatch docs = app.getProperty("Presentations").toDispatch();            doc = Dispatch.call(                    docs,                    "Open", src, true, true,false).toDispatch();            File target = new File(tar);            if (target.exists()) {                target.delete();            }            Dispatch.call(doc, "SaveAs",tar,32);        } finally {            if (doc != null) {                try {                    Dispatch.call(doc, "Close");                } catch (Throwable t) {                    logger.log(Level.SEVERE, t.getMessage(), t);                }            }            if (app != null) {                try {                    app.invoke("Quit");                    app.safeRelease();                } catch (Throwable t) {                    logger.log(Level.SEVERE, t.getMessage(), t);                }            }        }    }}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!