Office转PDF工具类

使用Jacob转换office文件,Jacob.dll文件需要放到jdk\bin目录下

Jacob.dll文件下载地址https://download.csdn.net/download/zss0101/10546500

package com.zss.util;

import java.io.File;

import com.jacob.activeX.ActiveXComponent;  
import com.jacob.com.Dispatch;
/** 
* @Description: 使用jacob转换office为pdf,也可以将tex文件转换为PDF
 */  
public class OfficeToPDF {  
       
     private static final int wdFormatPDF = 17;  
     private static final int xlTypePDF = 0;  
     private static final int ppSaveAsPDF = 32;  
     public static void main(String[] args) {  
          convertToPDF("D:\\介绍使用说明.docx","D:\\abc.pdf");  //输入,输出
    }  
      
    public static boolean convertToPDF(String inputFile, String pdfFile) {  
        String suffix =  getFileSufix(inputFile);  
        File file = new File(inputFile);  
        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 wordToPDF(inputFile,pdfFile);  
        }else if(suffix.equals("ppt")||suffix.equals("pptx")){  
            return pptToPDF(inputFile,pdfFile);  
        }else if(suffix.equals("xls")||suffix.equals("xlsx")){  
            return excelToPDF(inputFile,pdfFile);  
        }else{  
            System.out.println("文件格式不支持转换!");  
            return false;  
        }  
    }  
      
    public static String getFileSufix(String fileName){  
        int splitIndex = fileName.lastIndexOf(".");  
        return fileName.substring(splitIndex + 1);  
    }  
      
    /** 
     *  
    * @Title: wordToPDF 
    * @Description: 转换word文档为pdf 
     */  
    public static boolean wordToPDF(String inputFile,String pdfFile){  
        try{  
        //打开word应用程序  
        ActiveXComponent app = new ActiveXComponent("Word.Application");  
        //设置word不可见  
        app.setProperty("Visible", false);  
        //获得word中所有打开的文档,返回Documents对象  
        Dispatch docs = app.getProperty("Documents").toDispatch();  
        //调用Documents对象中Open方法打开文档,并返回打开的文档对象Document  
        Dispatch doc = Dispatch.call(docs,  
                                    "Open",  
                                    inputFile,  
                                    false,  
                                    true  
                                    ).toDispatch();  
     
        Dispatch.call(doc,  
                "ExportAsFixedFormat",  
                pdfFile,  
                wdFormatPDF     //word保存为pdf格式宏,值为17  
                );  
        //关闭文档  
        Dispatch.call(doc, "Close",false);  
        //关闭word应用程序  
        app.invoke("Quit", 0);  
        return true;  
    }catch(Exception e){  
        return false;  
    }  
    }  
      
    /** 
     *  
    * @Title: excelToPDF 
    * @Description: 转换excel为PDF 
     */  
    public static boolean excelToPDF(String inputFile,String pdfFile){  
        try{  
            ActiveXComponent app = new ActiveXComponent("Excel.Application");  
        app.setProperty("Visible", false);  
        Dispatch excels = app.getProperty("Workbooks").toDispatch();  
        Dispatch excel = Dispatch.call(excels,  
                                    "Open",  
                                    inputFile,  
                                    false,  
                                    true  
                                    ).toDispatch();  
        Dispatch.call(excel,  
                    "ExportAsFixedFormat",  
                    xlTypePDF,        
                    pdfFile  
                    );  
        Dispatch.call(excel, "Close",false);  
        app.invoke("Quit");  
        return true;  
    }catch(Exception e){  
        return false;  
    }  
           
    }  
      
    /** 
     *  
    * @Title: pptToPDF 
    * @Description: 转换ppt为office 
     */  
    public static boolean pptToPDF(String inputFile,String pdfFile){  
        try{  
        ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");  
        //app.setProperty("Visible", msofalse);  
        Dispatch ppts = app.getProperty("Presentations").toDispatch();  
           
        Dispatch ppt = Dispatch.call(ppts,  
                                    "Open",  
                                    inputFile,  
                                    true,//ReadOnly  
                                    true,//Untitled指定文件是否有标题  
                                    false//WithWindow指定文件是否可见  
                                    ).toDispatch();  
           
        Dispatch.call(ppt,  
                    "SaveAs",  
                    pdfFile,  
                    ppSaveAsPDF   
                    );  
                   
        Dispatch.call(ppt, "Close");  
           
        app.invoke("Quit");  
        return true;  
        }catch(Exception e){  
            return false;  
        }  
     }  
}  

猜你喜欢

转载自blog.csdn.net/zss0101/article/details/81081035