1. 程式人生 > >word、excel、ppt轉PDF檔案

word、excel、ppt轉PDF檔案

今天專案要求做個word、excel轉pdf的功能,百般搜刮,測試,終於完成.

下面這個是word、excel、ppt轉pdf功能,支援doc、docx、xls、xlsx、ppt、pptx轉pdf

1、 準備工作:windows環境、maven專案,普通專案也可以

下載jacob.zip,地址:  https://sourceforge.net/projects/jacob-project/

2、 點選  download  就行,下載解壓後顯示的是  jacob-1.19檔案   這個樣子

3、 64位系統就用 x64的dll,32位系統就用x86的dll。將dll檔案放入放入jdk/bin目錄下,如下圖所示:

4、 將壓縮包中的jacob.jar引入專案

<dependency>
         <groupId>com.google.guava</groupId>
         <artifactId>guava</artifactId>
         <version>19.0</version>
</dependency>
    
<dependency>
       <groupId>com.jacob</groupId>
       <artifactId>jacob</artifactId>
       <version>1.18-M2</version>
       <scope>system</scope>
       <systemPath>D:\Maven\maven-repository\jacob\jacob.jar</systemPath>
</dependency>

5、上程式碼

  1 import com.jacob.activeX.ActiveXComponent;
  2 import com.jacob.com.ComThread;
  3 import com.jacob.com.Dispatch;
  4 import com.jacob.com.Variant;
  5 
  6 public class Demo {
  7     private static final Integer WORD_TO_PDF_OPERAND = 17;
  8     private static final Integer PPT_TO_PDF_OPERAND = 32;
9 private static final Integer EXCEL_TO_PDF_OPERAND = 0; 10 11 public void docToPdf(String srcFilePath, String pdfFilePath) throws Exception { 12 ActiveXComponent app = null; 13 Dispatch doc = null; 14 try { 15 ComThread.InitSTA(); 16 app = new ActiveXComponent("Word.Application"); 17 app.setProperty("Visible", false); 18 Dispatch docs = app.getProperty("Documents").toDispatch(); 19 Object[] obj = new Object[]{ 20 srcFilePath, 21 new Variant(false), 22 new Variant(false),//是否只讀 23 new Variant(false), 24 new Variant("pwd") 25 }; 26 doc = Dispatch.invoke(docs, "Open", Dispatch.Method, obj, new int[1]).toDispatch(); 27 // Dispatch.put(doc, "Compatibility", false); //相容性檢查,為特定值false不正確 28 Dispatch.put(doc, "RemovePersonalInformation", false); 29 Dispatch.call(doc, "ExportAsFixedFormat", pdfFilePath, WORD_TO_PDF_OPERAND); // word儲存為pdf格式巨集,值為17 30 31 }catch (Exception e) { 32 e.printStackTrace(); 33 throw e; 34 } finally { 35 if (doc != null) { 36 Dispatch.call(doc, "Close", false); 37 } 38 if (app != null) { 39 app.invoke("Quit", 0); 40 } 41 ComThread.Release(); 42 } 43 } 44 45 public void pptToPdf(String srcFilePath, String pdfFilePath) throws Exception { 46 ActiveXComponent app = null; 47 Dispatch ppt = null; 48 try { 49 ComThread.InitSTA(); 50 app = new ActiveXComponent("PowerPoint.Application"); 51 Dispatch ppts = app.getProperty("Presentations").toDispatch(); 52 53 /* 54 * call 55 * param 4: ReadOnly 56 * param 5: Untitled指定檔案是否有標題 57 * param 6: WithWindow指定檔案是否可見 58 * */ 59 ppt = Dispatch.call(ppts, "Open", srcFilePath, true,true, false).toDispatch(); 60 Dispatch.call(ppt, "SaveAs", pdfFilePath, PPT_TO_PDF_OPERAND); // ppSaveAsPDF為特定值32 61 62 } catch (Exception e) { 63 e.printStackTrace(); 64 throw e; 65 } finally { 66 if (ppt != null) { 67 Dispatch.call(ppt, "Close"); 68 } 69 if (app != null) { 70 app.invoke("Quit"); 71 } 72 ComThread.Release(); 73 } 74 } 75 76 public void excelToPdf(String inFilePath, String outFilePath) throws Exception { 77 ActiveXComponent ax = null; 78 Dispatch excel = null; 79 try { 80 ComThread.InitSTA(); 81 ax = new ActiveXComponent("Excel.Application"); 82 ax.setProperty("Visible", new Variant(false)); 83 ax.setProperty("AutomationSecurity", new Variant(3)); // 禁用巨集 84 Dispatch excels = ax.getProperty("Workbooks").toDispatch(); 85 86 Object[] obj = new Object[]{ 87 inFilePath, 88 new Variant(false), 89 new Variant(false) 90 }; 91 excel = Dispatch.invoke(excels, "Open", Dispatch.Method, obj, new int[9]).toDispatch(); 92 93 // 轉換格式 94 Object[] obj2 = new Object[]{ 95 new Variant(EXCEL_TO_PDF_OPERAND), // PDF格式=0 96 outFilePath, 97 new Variant(0) //0=標準 (生成的PDF圖片不會變模糊) ; 1=最小檔案 98 }; 99 Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method,obj2, new int[1]); 100 101 } catch (Exception es) { 102 es.printStackTrace(); 103 throw es; 104 } finally { 105 if (excel != null) { 106 Dispatch.call(excel, "Close", new Variant(false)); 107 } 108 if (ax != null) { 109 ax.invoke("Quit", new Variant[] {}); 110 ax = null; 111 } 112 ComThread.Release(); 113 } 114 115 } 116 117 public static void main(String[] args) throws Exception { 118 String path = "C:\\Users\\lihao\\Desktop\\"; 119 // new Demo().docToPdf(path + "實習報告.doc", path+ "實習報告.pdf"); 120 new Demo().excelToPdf(path + "附件2:廈門金龍聯合汽車工業有限公司推廣應用車輛補助資金清算資訊明細表(青島2016年第二批).xls", path+ "附件2:廈門金龍聯合汽車工業有限公司推廣應用車輛補助資金清算資訊明細表(青島2016年第二批).pdf"); 121 // new Demo().pptToPdf(path + "1.pptx", path + "1.pdf"); 122 123 } 124 125 126 127 }

6、本文主要參考自:   https://www.cnblogs.com/xxyfhjl/p/6773786.html