java使用jacob将office文档转换为PDF格式

jacob 包下载地址: http://sourceforge.net/projects/jacob-project/

下载后,将jacob 与 jacob-1.19-x64.dll放到安装jdk目录中

jacob.jar  放入到 jdk/jre/lib/ext中

jacob-1.19-x64.dll放入到 jdk/jre/bin中

实现代码如下

package com.ypr.modules.op.utils;

import java.io.File;

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

public class ToPDF {

    private static final int wdFormatPDF = 17; // PDF 格式
    private static final int xlTypePDF = 0;  // xls格式

    public boolean toPDF(String sfileName, String toFileName) {
        System.out.println("------开始转换------");
        String suffix = getFileSufix(sfileName);
        File file = new File(sfileName);
        if (!file.exists()) {
            System.out.println("文件不存在!");
            return false;
        }
        if (suffix.equals("pdf")) {
            System.out.println("PDF not need to convert!");
            return false;
        }

        if (suffix.equals("doc") || suffix.equals("docx") || suffix.equals("txt")) {
            return word2PDF(sfileName, toFileName);
        } else if (suffix.equals("ppt") || suffix.equals("pptx")) {
            return ppt2PDF(sfileName, toFileName);
        } else if (suffix.equals("xls") || suffix.equals("xlsx")) {
            return excel2PDF(sfileName, toFileName);
        } else {
            System.out.println("文件格式不支持转换!");
            return false;
        }

    }
   //截取文件后缀方法
    public static String getFileSufix(String fileName) {
        int splitIndex = fileName.lastIndexOf(".");
        return fileName.substring(splitIndex + 1);
    }
  //转换word文档
    public boolean word2PDF(String sfileName, String toFileName) {
        long start = System.currentTimeMillis();
        ActiveXComponent app = null;
        Dispatch doc = null;
        boolean result = true;

        try {
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", new Variant(false));
            Dispatch docs = app.getProperty("Documents").toDispatch();
            doc = Dispatch.call(docs, "Open", sfileName).toDispatch();
            System.out.println("打开文档..." + sfileName);
            System.out.println("转换文档到 PDF..." + toFileName);
            File tofile = new File(toFileName);
            if (tofile.exists()) {
                tofile.delete();
            }
            Dispatch.call(doc, "SaveAs", toFileName, wdFormatPDF);
            long end = System.currentTimeMillis();
            System.out.println("转换完成..用时:" + (end - start) + "ms.");

            result = true;
        } catch (Exception e) {
            // TODO: handle exception

            System.out.println("========Error:文档转换失败:" + e.getMessage());
            result = false;
        } finally {
            Dispatch.call(doc, "Close", false);
            System.out.println("关闭文档");
            if (app != null) {
                app.invoke("Quit", new Variant[] {});
            }
        }

        ComThread.Release();

        return result;
    }
    //转换excel文档
    public boolean excel2PDF(String inputFile, String pdfFile) {
        ActiveXComponent app = null;
        Dispatch excel = null;
        boolean result = true;
        try {
            
            app = new ActiveXComponent("Excel.Application");
            app.setProperty("Visible", false);
            Dispatch excels = app.getProperty("Workbooks").toDispatch();
            excel = Dispatch.call(excels, "Open", inputFile, false, true).toDispatch();
            Dispatch.call(excel, "ExportAsFixedFormat", xlTypePDF, pdfFile);
            System.out.println("打开文档..." + inputFile);
            System.out.println("转换文档到 PDF..." + pdfFile);
            result = true;
        } catch (Exception e) {
            result = false;
        } finally {
            if (excel != null) {
                Dispatch.call(excel, "Close");
            }
            if (app != null) {
                app.invoke("Quit");
            }
        }
        return result;
    }
    //转换ppt文档
    public boolean ppt2PDF(String srcFilePath, String pdfFilePath) {
        ActiveXComponent app = null;
        Dispatch ppt = null;
        boolean result = true;
        try {
            ComThread.InitSTA();
            app = new ActiveXComponent("PowerPoint.Application");
            Dispatch ppts = app.getProperty("Presentations").toDispatch();

            // 因POWER.EXE的发布规则为同步,所以设置为同步发布
            ppt = Dispatch.call(ppts, "Open", srcFilePath, true, // ReadOnly
                    true, // Untitled指定文件是否有标题
                    false// WithWindow指定文件是否可见
            ).toDispatch();

            Dispatch.call(ppt, "SaveAs", pdfFilePath, 32); // ppSaveAsPDF为特定值32
            System.out.println("转换文档到 PDF..." + pdfFilePath);
            result = true; // set flag true;
        } catch (ComFailException e) {
            result = false;
        } catch (Exception e) {
            result = false;
        } finally {
            if (ppt != null) {
                Dispatch.call(ppt, "Close");
            }
            if (app != null) {
                app.invoke("Quit");
            }
            ComThread.Release();
        }
        return result;
    }

    public static void main(String[] args) {
        ToPDF d = new ToPDF();
        //d.wordToPDF("E:\\poi-test\\hadoop集群搭建.docx", "E:\\poi-test\\hadoop集群搭建.pdf");
        d.toPDF("E:\\poi-test\\私有云清单.xlsx", "E:\\poi-test\\私有云清单.pdf");
        
    }
}

 

posted @ 2018-12-06 15:43  ypr-  阅读(1211)  评论(0编辑  收藏  举报