zoukankan      html  css  js  c++  java
  • Office 转 PDF

    采用 jacob 插件,调用 WPS 功能实现,需要 Windows 操作系统。这种方式转成 PDF 不会失真,针对 excel 设置 PDF 横向布局。

    import com.jacob.activeX.ActiveXComponent;
    import com.jacob.com.Dispatch;
    import com.jacob.com.Variant;
    
    import java.io.File;
    
    /**
     * office文档转成pdf,通过wps
     *
     */
    public class Office2PdfByWps {
    
        private static final Integer WORD_TO_PDF_OPERAND = 17;
        private static final Integer EXCEL_TO_PDF_OPERAND = 0;  // 0=标准(生成的PDF图片不会变模糊) 1=最小文件(生成的PDF图片糊的一塌糊涂)
        private static final Integer PPT_TO_PDF_OPERAND = 32;
    
        /**
         * Word转PDF
         *
         * @param input
         * @param output
         */
        public static void word2Pdf(String input, String output) {
            ActiveXComponent component = null;
            Dispatch doc = null;
            try {
                component = new ActiveXComponent("KWPS.Application");    // 打开WPS Word应用程序
                component.setProperty("Visible", new Variant(false));           // 设置Word不可见
                component.setProperty("AutomationSecurity", new Variant(3));    // 禁用宏
    
                Dispatch docs = component.getProperty("Documents").toDispatch();    // 获得Word中所有打开的文档,返回documents对象
                doc = Dispatch.call(docs, "Open", input, false, true).toDispatch(); // 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
    
                Dispatch.call(doc, "ExportAsFixedFormat", output, WORD_TO_PDF_OPERAND);// word保存为pdf格式宏,值为17
            } finally {
                if (doc != null) {
                    Dispatch.call(doc, "Close", false);
                    doc.safeRelease();
                }
                if (component != null) {
                    component.invoke("Quit", 0); // 关闭Word应用程序
                    component.safeRelease();
                }
            }
        }
    
        /**
         * Excel转化成PDF
         *
         * @param input
         * @param output
         */
        public static void excel2Pdf(String input, String output) {
            ActiveXComponent component = null;
            Dispatch excel = null;
            try {
                component = new ActiveXComponent("KET.Application");
                component.setProperty("Visible", false);
                component.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
    
                Dispatch excels = component.getProperty("Workbooks").toDispatch();
                excel = Dispatch.invoke(excels, "Open", Dispatch.Method, new Object[] { input, new Variant(false), new Variant(false) }, new int[9]).toDispatch();
    
                // 每个 sheet 都设成横向
                Dispatch sheets = Dispatch.get(excel,"Sheets").toDispatch();
                for (int i = 1; i <= Dispatch.get(sheets, "Count").getInt(); i++) {
                    Dispatch sheet = Dispatch.invoke(sheets, "Item", Dispatch.Get, new Object[] { new Integer(i) }, new int[1]).toDispatch();
                    Dispatch pageSetup = Dispatch.get(sheet, "PageSetup").toDispatch();
                    Dispatch.put(pageSetup, "Orientation", new Variant(2));
                }
    
                Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, new Object[] { new Variant(0), output, new Variant(EXCEL_TO_PDF_OPERAND) }, new int[1]);
            } finally {
                if (excel != null) {
                    Dispatch.call(excel, "Close", false);
                    excel.safeRelease();
                }
                if (component != null) {
                    component.invoke("Quit", new Variant[] {});
                    component.safeRelease();
                }
            }
        }
    
        /**
         * ppt转化成PDF
         *
         * @param input
         * @param output
         */
        public static void ppt2Pdf(String input, String output) {
            ActiveXComponent component = null;
            Dispatch ppt = null;
            try {
                component = new ActiveXComponent("KWPP.Application");
    
                Dispatch ppts = component.getProperty("Presentations").toDispatch();
                ppt = Dispatch.call(ppts, "Open", input, true, false).toDispatch();
    
                Dispatch.invoke(ppt, "SaveAs", Dispatch.Method, new Object[]{output, new Variant(PPT_TO_PDF_OPERAND)}, new int[1]);
            } finally {
                if (ppt != null) {
                    Dispatch.call(ppt, "Close");
                    ppt.safeRelease();
                }
                if (component != null) {
                    component.invoke("Quit");
                    component.safeRelease();
                }
            }
        }
    
    }
  • 相关阅读:
    互质与欧拉函数学习笔记
    Luogu P4588 [TJOI2018]数学计算 题解
    Luogu P1072 Hankson 的趣味题 题解
    Luogu [POI2002][HAOI2007]反素数 题解
    Luogu P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat… 题解
    JavaScript 实现简易版贪吃蛇(Day_13)
    IDEA 配置 Tomcat(详细)(Day_12)
    常用数据库连接池配置及使用(Day_11)
    大对象数据LOB的应用(Day_10)
    IDEA中配置maven 全解析教程(Day_08)
  • 原文地址:https://www.cnblogs.com/zhiqsyr/p/7240432.html
Copyright © 2011-2022 走看看