标签:
1、介绍
3、转换word为pdf、html、txt 的示例
package com.shanhy.demo.windowsoffice; import java.io.File; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.ComThread; import com.jacob.com.Dispatch; import com.jacob.com.Variant; /** * * 将jacob.dll放入JDK的bin目录下 * 把jacob.jar放入项目的buildPath中(web项目放到WEB-INF\lib目录下) * * @author 单红宇 * */ public class ConvertToPdf { // WORD 转换文档格式参数值:17为pdf,8为html,2为txt(支持的格式不限与此,其他格式暂为列出) static final int wdFormatPDF = 17;// PDF 格式 static final int wdFormatHTML = 8;// HTML 格式 static final int wdFormatTXT = 2;// TXT 格式 /** * Word文档转换 * * @param fromfileName * @param toFileName * @author SHANHY */ public void wordConvert(String fromfileName, String toFileName) { System.out.println("启动Word..."); ComThread.InitSTA(); long start = System.currentTimeMillis(); ActiveXComponent app = null; Dispatch doc = null; try { app = new ActiveXComponent("Word.Application");//创建一个word对象 app.setProperty("Visible", new Variant(false)); //不可见打开word app.setProperty("AutomationSecurity", new Variant(3)); //禁用宏 Dispatch docs = app.getProperty("Documents").toDispatch();//获取文挡属性 System.out.println("打开文档 >>> " + fromfileName); //Object[]第三个参数是表示“是否只读方式打开” doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] { fromfileName, new Variant(false), new Variant(true) }, new int[1]).toDispatch(); File tofile = new File(toFileName); if (tofile.exists()) { tofile.delete(); } int formatValue = -1; if(toFileName.toLowerCase().endsWith(".pdf")){ formatValue = wdFormatPDF; }else if(toFileName.toLowerCase().endsWith(".html")){ formatValue = wdFormatHTML; }else if(toFileName.toLowerCase().endsWith(".txt")){ formatValue = wdFormatTXT; }else{ formatValue = -1; } if(formatValue != -1){ System.out.println("转换文档 ["+fromfileName+"] >>> ["+toFileName+"]"); Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { toFileName, new Variant(formatValue) }, new int[1]); }else{ System.out.println("转换文件到目标文档不被支持!["+fromfileName+"] >>> ["+toFileName+"]"); } long end = System.currentTimeMillis(); System.out.println("用时:" + (end - start) + "ms."); } catch (Exception e) { e.printStackTrace(); System.out.println("========Error:文档转换失败:" + e.getMessage()); } finally { Dispatch.call(doc, "Close", false); System.out.println("关闭文档"); if (app != null) app.invoke("Quit", new Variant[] {}); } // 如果没有这句话,winword.exe进程将不会关闭 ComThread.Release(); ComThread.quitMainSTA(); } /** * PPT(PowerPoint)文档转换 * * @param fromfileName * @param toFileName * @author SHANHY */ public void pptConvert(String fromfileName, String toFileName) { System.out.println("启动PPT..."); ComThread.InitSTA(); long start = System.currentTimeMillis(); ActiveXComponent app = null; Dispatch doc = null; try { app = new ActiveXComponent("Word.Application");//创建一个word对象 app.setProperty("Visible", new Variant(false)); //不可见打开word app.setProperty("AutomationSecurity", new Variant(3)); //禁用宏 Dispatch docs = app.getProperty("Documents").toDispatch();//获取文挡属性 System.out.println("打开文档 >>> " + fromfileName); //Object[]第三个参数是表示“是否只读方式打开” doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] { fromfileName, new Variant(false), new Variant(true) }, new int[1]).toDispatch(); File tofile = new File(toFileName); if (tofile.exists()) { tofile.delete(); } int formatValue = -1; if(toFileName.toLowerCase().endsWith(".pdf")){ formatValue = wdFormatPDF; }else if(toFileName.toLowerCase().endsWith(".html")){ formatValue = wdFormatHTML; }else if(toFileName.toLowerCase().endsWith(".txt")){ formatValue = wdFormatTXT; }else{ formatValue = -1; } if(formatValue != -1){ System.out.println("转换文档 ["+fromfileName+"] >>> ["+toFileName+"]"); Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { toFileName, new Variant(formatValue) }, new int[1]); }else{ System.out.println("转换文件到目标文档不被支持!["+fromfileName+"] >>> ["+toFileName+"]"); } long end = System.currentTimeMillis(); System.out.println("用时:" + (end - start) + "ms."); } catch (Exception e) { e.printStackTrace(); System.out.println("========Error:文档转换失败:" + e.getMessage()); } finally { Dispatch.call(doc, "Close", false); System.out.println("关闭文档"); if (app != null) app.invoke("Quit", new Variant[] {}); } // 如果没有这句话,winword.exe进程将不会关闭 ComThread.Release(); ComThread.quitMainSTA(); } public static void main(String[] args) { ConvertToPdf d = new ConvertToPdf(); d.wordConvert("g:\\test.docx", "g:\\test.pdf"); } }
import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Variant; import com.jacob.com.Dispatch; public class Word { String strDir = "c:jacob_1.9"; String strInputDoc = strDir + "file_in.doc"; String strOutputDoc = strDir + "file_out.doc"; String strOldText = "[label:import:1]"; String strNewText = "I am some horribly long sentence, so long that [insert anything]"; boolean isVisible = true; boolean isSaveOnExit = true; public Word() { ActiveXComponent oWord = new ActiveXComponent("Word.Application"); oWord.setProperty("Visible", new Variant(isVisible)); Dispatch oDocuments = oWord.getProperty("Documents").toDispatch(); Dispatch oDocument = Dispatch.call(oDocuments, "Open", strInputDoc). toDispatch(); Dispatch oSelection = oWord.getProperty("Selection").toDispatch(); Dispatch oFind = oWord.call(oSelection, "Find").toDispatch(); Dispatch.put(oFind, "Text", strOldText); Dispatch.call(oFind, "Execute"); Dispatch.put(oSelection, "Text", strNewText); Dispatch.call(oSelection, "MoveDown"); Dispatch.put(oSelection, "Text", "nSo we got the next line including BR.n"); Dispatch oFont = Dispatch.get(oSelection, "Font").toDispatch(); Dispatch.put(oFont, "Bold", "1"); Dispatch.put(oFont, "Italic", "1"); Dispatch.put(oFont, "Underline", "0"); Dispatch oAlign = Dispatch.get(oSelection, "ParagraphFormat"). toDispatch(); Dispatch.put(oAlign, "Alignment", "3"); Dispatch oWordBasic = (Dispatch) Dispatch.call(oWord, "WordBasic"). getDispatch(); Dispatch.call(oWordBasic, "FileSaveAs", strOutputDoc); Dispatch.call(oDocument, "Close", new Variant(isSaveOnExit)); oWord.invoke("Quit", new Variant[0]); } public static void main(String[] args) { Word word = new Word(); } }
部分内容参考: http://www.cnblogs.com/vulcans/archive/2009/09/08/1562588.html
使用jacob调用Windows的com对象,转换Office文件为pdf、html等
标签:
原文地址:http://blog.csdn.net/catoop/article/details/43150671