Java代码将word/excle/ppt转pdf

0.前提条件:电脑要安装好office

1.准备工作:jacob-1.18.0.jar,jacob-1.18-x64.dll

  这个网址有这些东西:https://sourceforge.net/projects/jacob-project/

2.将jacob-1.18-x64.dll放在java所在的bin目录下;

3.编写转换的工具类代码:

import java.io.File;
import java.util.Date;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class PDFUtil {

    private static final int wdFormatPDF = 17;
    private static final int xlTypePDF = 0;
    private static final int ppSaveAsPDF = 32;
    public static final int EXCEL_HTML = 44;
    
    
    
    public static int word2PDF(String inputFile, String pdfFile) {
        try {
            // 打开Word应用程序
            ActiveXComponent app = new ActiveXComponent("Word.Application");
            long date = new Date().getTime();
            // 设置Word不可见
            app.setProperty("Visible", new Variant(false));
            // 禁用宏
            app.setProperty("AutomationSecurity", new Variant(3));
            // 获得Word中所有打开的文档,返回documents对象
            Dispatch docs = app.getProperty("Documents").toDispatch();
            // 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
            Dispatch doc = Dispatch.call(docs, "Open", inputFile, false, true).toDispatch();
            File tofile = new File(pdfFile);
            if (tofile.exists()) {
                tofile.delete();
            }
            Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF);// word保存为pdf格式宏,值为17
            // 关闭文档
            long date2 = new Date().getTime();
            int time = (int) ((date2 - date) / 1000);

            Dispatch.call(doc, "Close", false);
            // 关闭Word应用程序
            app.invoke("Quit", 0);
            return time;
        } catch (Exception e) {

            return -1;
        }

    }

    public static int excel2PDF(String inputFile, String pdfFile) {
        try {

            ComThread.InitSTA(true);
            ActiveXComponent ax = new ActiveXComponent("Excel.Application");
            long date = new Date().getTime();
            ax.setProperty("Visible", false);
            ax.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
            Dispatch excels = ax.getProperty("Workbooks").toDispatch();

            Dispatch excel = Dispatch.invoke(excels, "Open", Dispatch.Method, new Object[]{
                    inputFile, new Variant(false), new Variant(false)}, new int[9]).toDispatch();
            File toFile = new File(pdfFile);
            if (toFile.exists()) {
                toFile.delete();
            }
            Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, new Object[]{
                    new Variant(0), // PDF格式=0
                    pdfFile, new Variant(xlTypePDF) // 0=标准 (生成的PDF图片不会变模糊) 1=最小文件
                    // (生成的PDF图片糊的一塌糊涂)
            }, new int[1]);
            long date2 = new Date().getTime();
            int time = (int) ((date2 - date) / 1000);
            Dispatch.call(excel, "Close", new Variant(false));

            if (ax != null) {
                ax.invoke("Quit", new Variant[]{});
                ax = null;
            }
            ComThread.Release();
            return time;
        } catch (Exception e) {
            return -1;
        }
    }


    public static int ppt2PDF(String inputFile, String pdfFile) {
        try {
            ComThread.InitSTA(true);
            ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");
//            app.setProperty("Visible", false);
            long date = new Date().getTime();
            Dispatch ppts = app.getProperty("Presentations").toDispatch();
            Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true, // ReadOnly
                    //    false, // Untitled指定文件是否有标题
                    false// WithWindow指定文件是否可见
            ).toDispatch();
            File toFile = new File(pdfFile);
            if (toFile.exists()) {
                toFile.delete();
            }
            Dispatch.invoke(ppt, "SaveAs", Dispatch.Method, new Object[]{pdfFile,
                    new Variant(ppSaveAsPDF)}, new int[1]);
            Dispatch.call(ppt, "Close");
            long date2 = new Date().getTime();
            int time = (int) ((date2 - date) / 1000);
            app.invoke("Quit");
            return time;
        } catch (Exception e) {
            return -1;
        }
    }
    
}

4.利用工具类转换文档

public class TestPDFUtil {
    
    public static void main(String[] args) {
        
        String inputFile = "D:\\ING\\文件1.xlsx";
        String pdfFile = "D:\\ING\\文件2.pdf";
        
        PDFUtil.excel2PDF(inputFile, pdfFile);
        
        System.out.println("over......");
        
    }

}

 

posted @ 2020-08-14 20:11  不夹心饼干  阅读(624)  评论(0编辑  收藏  举报