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();
            }
        }
    }

}

 

posted @ 2017-07-26 16:33  zhiqsyr  阅读(413)  评论(0编辑  收藏  举报