Java使用Jacob将Word、Excel、PPT转化成PDF

时间:2023-01-06 06:17:51

使用Jacob将金山WPS转化成PDF,其中WPS文字使用KWPS.Aplication、Excel表格是KET.Application、演示文档是KWPP.Application,废话不多说,直接上代码:

  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 /***
  7  *
  8  * @author create by 沙漠的波浪
  9  *
 10  * @date 2017年2月21日---下午2:29:27
 11  *
 12  */
 13 public class Word2PDF {
 14     private static final int wdFormatPDF = 17;
 15     private static final int xlTypePDF = 0;
 16     private static final int ppSaveAsPDF = 32;
 17 
 18     public static void main(String[] args) {
 19         
 20         int time = convert2PDF("D:/17-2-17诉保通服务平台(法院).ppt", "D:/17-2-17诉保通服务平台(法院).pdf");
 21                 
 22         if (time == -4) {
 23             System.out.println("转化失败,未知错误...");
 24         } else if(time == -3) {
 25             System.out.println("原文件就是PDF文件,无需转化...");
 26         } else if (time == -2) {
 27             System.out.println("转化失败,文件不存在...");
 28         }else if(time == -1){
 29             System.out.println("转化失败,请重新尝试...");
 30         }else if (time < -4) {
 31             System.out.println("转化失败,请重新尝试...");
 32         }else {
 33             System.out.println("转化成功,用时:  " + time + "s...");
 34         }
 35 
 36     }
 37 
 38     /***
 39      * 判断需要转化文件的类型(Excel、Word、ppt)
 40      * 
 41      * @param inputFile
 42      * @param pdfFile
 43      */
 44     private static int convert2PDF(String inputFile, String pdfFile) {
 45         String kind = getFileSufix(inputFile);
 46         File file = new File(inputFile);
 47         if (!file.exists()) {
 48             return -2;//文件不存在
 49         }
 50         if (kind.equals("pdf")) {
 51             return -3;//原文件就是PDF文件
 52         }
 53         if (kind.equals("doc")||kind.equals("docx")||kind.equals("txt")) {
 54             return Word2PDF.word2PDF(inputFile, pdfFile);
 55         }else if (kind.equals("ppt")||kind.equals("pptx")) {
 56             return Word2PDF.ppt2PDF(inputFile, pdfFile);
 57         }else if(kind.equals("xls")||kind.equals("xlsx")){
 58             return Word2PDF.Ex2PDF(inputFile, pdfFile);
 59         }else {
 60             return -4;
 61         }
 62     }
 63 
 64     /***
 65      * 判断文件类型
 66      * 
 67      * @param fileName
 68      * @return
 69      */
 70     public static String getFileSufix(String fileName) {
 71         int splitIndex = fileName.lastIndexOf(".");
 72         return fileName.substring(splitIndex + 1);
 73     }
 74 
 75     /***
 76      * 
 77      * Word转PDF
 78      * 
 79      * @param inputFile
 80      * @param pdfFile
 81      * @return
 82      */
 83 
 84     private static int word2PDF(String inputFile, String pdfFile) {
 85         // TODO Auto-generated method stub
 86         try {
 87             // 打开Word应用程序
 88             ActiveXComponent app = new ActiveXComponent("KWPS.Application");
 89             System.out.println("开始转化Word为PDF...");
 90             long date = new Date().getTime();
 91             // 设置Word不可见
 92             app.setProperty("Visible", new Variant(false));
 93             // 禁用宏
 94             app.setProperty("AutomationSecurity", new Variant(3));
 95             // 获得Word中所有打开的文档,返回documents对象
 96             Dispatch docs = app.getProperty("Documents").toDispatch();
 97             // 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
 98             Dispatch doc = Dispatch.call(docs, "Open", inputFile, false, true).toDispatch();
 99             /***
100              * 
101              * 调用Document对象的SaveAs方法,将文档保存为pdf格式
102              * 
103              * Dispatch.call(doc, "SaveAs", pdfFile, wdFormatPDF
104              * word保存为pdf格式宏,值为17 )
105              * 
106              */
107             Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF);// word保存为pdf格式宏,值为17
108             System.out.println(doc);
109             // 关闭文档
110             long date2 = new Date().getTime();
111             int time = (int) ((date2 - date) / 1000);
112 
113             Dispatch.call(doc, "Close", false);
114             // 关闭Word应用程序
115             app.invoke("Quit", 0);
116             return time;
117         } catch (Exception e) {
118             // TODO: handle exception
119             return -1;
120         }
121 
122     }
123 
124     /***
125      * 
126      * Excel转化成PDF
127      * 
128      * @param inputFile
129      * @param pdfFile
130      * @return
131      */
132     private static int Ex2PDF(String inputFile, String pdfFile) {
133         try {
134 
135             ComThread.InitSTA(true);
136             ActiveXComponent ax = new ActiveXComponent("KET.Application");
137             System.out.println("开始转化Excel为PDF...");
138             long date = new Date().getTime();
139             ax.setProperty("Visible", false);
140             ax.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
141             Dispatch excels = ax.getProperty("Workbooks").toDispatch();
142 
143             Dispatch excel = Dispatch
144                     .invoke(excels, "Open", Dispatch.Method,
145                             new Object[] { inputFile, new Variant(false), new Variant(false) }, new int[9])
146                     .toDispatch();
147             // 转换格式
148             Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, new Object[] { new Variant(0), // PDF格式=0
149                     pdfFile, new Variant(xlTypePDF) // 0=标准 (生成的PDF图片不会变模糊) 1=最小文件
150                                             // (生成的PDF图片糊的一塌糊涂)
151             }, new int[1]);
152 
153             // 这里放弃使用SaveAs
154             /*
155              * Dispatch.invoke(excel,"SaveAs",Dispatch.Method,new Object[]{
156              * outFile, new Variant(57), new Variant(false), new Variant(57),
157              * new Variant(57), new Variant(false), new Variant(true), new
158              * Variant(57), new Variant(true), new Variant(true), new
159              * Variant(true) },new int[1]);
160              */
161             long date2 = new Date().getTime();
162             int time = (int) ((date2 - date) / 1000);
163             Dispatch.call(excel, "Close", new Variant(false));
164 
165             if (ax != null) {
166                 ax.invoke("Quit", new Variant[] {});
167                 ax = null;
168             }
169             ComThread.Release();
170             return time;
171         } catch (Exception e) {
172             // TODO: handle exception
173             return -1;
174         }
175     }
176 
177     /***
178      * ppt转化成PDF
179      * 
180      * @param inputFile
181      * @param pdfFile
182      * @return
183      */
184     private static int ppt2PDF(String inputFile, String pdfFile) {
185         try {
186             ComThread.InitSTA(true);
187             ActiveXComponent app = new ActiveXComponent("KWPP.Application");
188 //            app.setProperty("Visible", false);
189             System.out.println("开始转化PPT为PDF...");
190             long date = new Date().getTime();
191             Dispatch ppts = app.getProperty("Presentations").toDispatch();
192             Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true, // ReadOnly
193                 //    false, // Untitled指定文件是否有标题
194                     false// WithWindow指定文件是否可见
195             ).toDispatch();
196             Dispatch.invoke(ppt, "SaveAs", Dispatch.Method, new Object[]{
197                     pdfFile,new Variant(ppSaveAsPDF)},new int[1]);
198             System.out.println("PPT");
199             Dispatch.call(ppt, "Close");
200             long date2 = new Date().getTime();
201             int time = (int) ((date2 - date) / 1000);
202             app.invoke("Quit");
203             return time;
204         } catch (Exception e) {
205             // TODO: handle exception
206             return -1;
207         }
208     }
209 }

 

 开发所需要的jar包和dll文件的下载地址:http://download.csdn.net/my