1. 程式人生 > >Java使用Jacob將wps的Word、Excel、PPT轉化成PDF

Java使用Jacob將wps的Word、Excel、PPT轉化成PDF

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

/***
 *
 * @author create by 沙漠的波浪
 *
 * @date 2017年2月21日---下午2:29:27
 *
 */
public class Word2PDF {
    private static final int wdFormatPDF = 17;
    private static
final int xlTypePDF = 0; private static final int ppSaveAsPDF = 32; public static void main(String[] args) { int time = convert2PDF("D:/17-2-17訴保通服務平臺(法院).ppt", "D:/17-2-17訴保通服務平臺(法院).pdf"); if (time == -4) { System.out.println("轉化失敗,未知錯誤..."); } else if(time == -3
) { System.out.println("原檔案就是PDF檔案,無需轉化..."); } else if (time == -2) { System.out.println("轉化失敗,檔案不存在..."); }else if(time == -1){ System.out.println("轉化失敗,請重新嘗試..."); }else if (time < -4) { System.out.println("轉化失敗,請重新嘗試..."); }else
{ System.out.println("轉化成功,用時: " + time + "s..."); } } /*** * 判斷需要轉化檔案的型別(Excel、Word、ppt) * * @param inputFile * @param pdfFile */ private static int convert2PDF(String inputFile, String pdfFile) { String kind = getFileSufix(inputFile); File file = new File(inputFile); if (!file.exists()) { return -2;//檔案不存在 } if (kind.equals("pdf")) { return -3;//原檔案就是PDF檔案 } if (kind.equals("doc")||kind.equals("docx")||kind.equals("txt")) { return Word2PDF.word2PDF(inputFile, pdfFile); }else if (kind.equals("ppt")||kind.equals("pptx")) { return Word2PDF.ppt2PDF(inputFile, pdfFile); }else if(kind.equals("xls")||kind.equals("xlsx")){ return Word2PDF.Ex2PDF(inputFile, pdfFile); }else { return -4; } } /*** * 判斷檔案型別 * * @param fileName * @return */ public static String getFileSufix(String fileName) { int splitIndex = fileName.lastIndexOf("."); return fileName.substring(splitIndex + 1); } /*** * * Word轉PDF * * @param inputFile * @param pdfFile * @return */ private static int word2PDF(String inputFile, String pdfFile) { // TODO Auto-generated method stub try { // 開啟Word應用程式 ActiveXComponent app = new ActiveXComponent("KWPS.Application"); System.out.println("開始轉化Word為PDF..."); 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(); /*** * * 呼叫Document物件的SaveAs方法,將文件儲存為pdf格式 * * Dispatch.call(doc, "SaveAs", pdfFile, wdFormatPDF * word儲存為pdf格式巨集,值為17 ) * */ Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF);// word儲存為pdf格式巨集,值為17 System.out.println(doc); // 關閉文件 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) { // TODO: handle exception return -1; } } /*** * * Excel轉化成PDF * * @param inputFile * @param pdfFile * @return */ private static int Ex2PDF(String inputFile, String pdfFile) { try { ComThread.InitSTA(true); ActiveXComponent ax = new ActiveXComponent("KET.Application"); System.out.println("開始轉化Excel為PDF..."); 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(); // 轉換格式 Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, new Object[] { new Variant(0), // PDF格式=0 pdfFile, new Variant(xlTypePDF) // 0=標準 (生成的PDF圖片不會變模糊) 1=最小檔案 // (生成的PDF圖片糊的一塌糊塗) }, new int[1]); // 這裡放棄使用SaveAs /* * Dispatch.invoke(excel,"SaveAs",Dispatch.Method,new Object[]{ * outFile, new Variant(57), new Variant(false), new Variant(57), * new Variant(57), new Variant(false), new Variant(true), new * Variant(57), new Variant(true), new Variant(true), new * Variant(true) },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) { // TODO: handle exception return -1; } } /*** * ppt轉化成PDF * * @param inputFile * @param pdfFile * @return */ private static int ppt2PDF(String inputFile, String pdfFile) { try { ComThread.InitSTA(true); ActiveXComponent app = new ActiveXComponent("KWPP.Application"); // app.setProperty("Visible", false); System.out.println("開始轉化PPT為PDF..."); 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(); Dispatch.invoke(ppt, "SaveAs", Dispatch.Method, new Object[]{ pdfFile,new Variant(ppSaveAsPDF)},new int[1]); System.out.println("PPT"); Dispatch.call(ppt, "Close"); long date2 = new Date().getTime(); int time = (int) ((date2 - date) / 1000); app.invoke("Quit"); return time; } catch (Exception e) { // TODO: handle exception return -1; } } }