如何用jacob获取word文档批注?
1. 初始化com的线程,非常重要,否则第二次创建com对象的时候会出现can't co-create object异常 (参见jacob的帮助文档),完成操作com组件后要调用 realease方法 ComThread.InitSTA();// 初始化com的线程,非常重要!!使用结束后要调用 realease方法 2. 初始化word应用程序,新建一个空白文档,取得文档内容对象//Instantiate objWord //Declare word object ActiveXComponent objWord = new ActiveXComponent("Word.Application"); //Assign a local word object Dispatch wordObject = (Dispatch) objWord.getObject(); //Create a Dispatch Parameter to show the document that is opened Dispatch.put((Dispatch) wordObject, "Visible", new Variant(true));// new Variant(true)表示word应用程序可见 Tip:设置一个对象的属性的时候,利用Dispatch的put方法,给属性赋值。
上面这行语句相当于vb的 wordObject.Visible = true 语句 //Instantiate the Documents Property Dispatch documents = objWord.getProperty("Documents").toDispatch(); //documents表示word的所有文档窗口,(word是多文档应用程序) //Add a new word document, Current Active Document Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add命令创建一个新文档,用Open命令可以打开一个现有文档 Tip:调用一个对象的方法的时候,利用Dispatch的call方法,上面的语句相当于vb的document = documents.Add() 语句。
Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的内容 Tip:取得一个对象的成员变量(属性)时利用Dispatch的get方法,上面的语句相当于vb的wordContent = document.Content语句 3. 取得word文档的内容后,可以对其内容进行操作 Dispatch.call(wordContent, "InsertAfter", "这里是一个段落的内容");//插入一个段落 4. 设置刚插入的段落的文字格式 Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落 int paragraphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落数 // 找到刚输入的段落,设置格式 Dispatch lastParagraph = Dispatch.call(paragraphs, "Item", new Variant(paragraphCount)). toDispatch(); // 最后一段 Dispatch lastParagraphRange = Dispatch.get(lastParagraph, "Range"). toDispatch(); Dispatch font = Dispatch.get(lastParagraphRange, "Font").toDispatch(); Dispatch.put(font, "Bold", new Variant(true)); // 设置为黑体 Dispatch.put(font, "Italic", new Variant(true)); // 设置为斜体 Dispatch.put(font, "Name", new Variant("宋体")); // Dispatch.put(font, "Size", new Variant(12)); //小四 注意:如果想插入一个新的空白行,也需要设置段落的文字格式,否则新插入行的文字格式会于刚插入的段落的格式相同。
5. 将当前文档保存 Dispatch.call(document, "SaveAs", new Variant("C: abc.doc")); // 保存一个新文档 6. 释放COM线程 ComThread.Release();//释放com线程。
根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理 完整测试代码:(StudyJacob.java 附件中有本文章和java源文件) import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; import com.jacob.com.ComThread; public class StudyJacob { public static void main(String[] args) { ComThread.InitSTA();// 初始化com的线程,非常重要!!使用结束后要调用 realease方法 //Instantiate objWord //Declare word object ActiveXComponent objWord = new ActiveXComponent("Word.Application"); //Assign a local word object Dispatch wordObject = (Dispatch) objWord.getObject(); //Create a Dispatch Parameter to show the document that is opened Dispatch.put((Dispatch) wordObject, "Visible", new Variant(true));// new Variant(true)表示word应用程序可见 //Instantiate the Documents Property Dispatch documents = objWord.getProperty("Documents").toDispatch(); //documents表示word的所有文档窗口,(word是多文档应用程序) //Add a new word document, Current Active Document Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add命令创建一个新文档,用Open命令可以打开一个现有文档 Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的内容 Dispatch.call(wordContent, "InsertAfter", "这里是一个段落的内容");//插入一个段落 Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落 int paragraphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落数 // 找到刚输入的段落,设置格式 Dispatch lastParagraph = Dispatch.call(paragraphs, "Item", new Variant(paragraphCount)). toDispatch(); // 最后一段 Dispatch lastParagraphRange = ...
我在java中用jacob把word文档转化成pdf文件出来了问题?
需要用到插件jacob,自己去下载吧。
import com.jacob.activeX.ActiveXComponent;import com.jacob.com.ComThread;import com.jacob.com.Dispatch;import com.jacob.com.Variant;public class D2P {private ActiveXComponent wordCom = null;private Object wordDoc = null;private final Variant False = new Variant(false);private final Variant True = new Variant(true);/** *//** *//** *//*** 打开word文档** @param filePath* word文档* @return 返回word文档对象*/public boolean openWord(String filePath) {//建立ActiveX部件wordCom = new ActiveXComponent("Word.Application");try {//返回wrdCom.Documents的DispatchDispatch wrdDocs = wordCom.getProperty("Documents").toDispatch();//调用wrdCom.Documents.Open方法打开指定的word文档,返回wordDocwordDoc = Dispatch.invoke(wrdDocs, "Open", Dispatch.Method,new Object[] { filePath }, new int[1]).toDispatch();return true;} catch (Exception ex) {ex.printStackTrace();}return false;}/** *//** *//** *//*** 关闭word文档*/public void closeWord() {//关闭word文件wordCom.invoke("Quit", new Variant[] {});}/** *//** *//** *//*** * 将word文档打印为PS文件后,使用Distiller将PS文件转换为PDF文件 *** @param sourceFilePath* 源文件路径 ** @param destinPSFilePath* 首先生成的PS文件路径 ** @param destinPDFFilePath* 生成PDF文件路径*/public void docToPDF(String sourceFilePath, String destinPSFilePath,String destinPDFFilePath) {if (!openWord(sourceFilePath)) {closeWord();return;}//建立Adobe Distiller的com对象ActiveXComponent distiller = new ActiveXComponent("PDFDistiller.PDFDistiller.1");try {//设置当前使用的打印机,我的Adobe Distiller打印机名字为"Adobe PDF"wordCom.setProperty("ActivePrinter", new Variant("MS Publisher Color Printer"));//设置printout的参数,将word文档打印为postscript文档。
目前只使用了前5个参数,如果要使用更多的话可以参考MSDN的office开发相关api//是否在后台运行Variant Background = False;//是否追加打印Variant Append = False;//打印所有文档int wdPrintAllDocument = 0;Variant Range = new Variant(wdPrintAllDocument);//输出的postscript文件的路径Variant OutputFileName = new Variant(destinPSFilePath);Dispatch.callN((Dispatch) wordDoc, "PrintOut", new Variant[] {Background, Append, Range, OutputFileName });System.out.println("由word文档打印为ps文档成功!");//调用Distiller对象的FileToPDF方法所用的参数,详细内容参考Distiller Api手册//作为输入的ps文档路径Variant inputPostScriptFilePath = new Variant(destinPSFilePath);//作为输出的pdf文档的路径Variant outputPDFFilePath = new Variant(destinPDFFilePath);//定义FileToPDF方法要使用adobe pdf设置文件的路径,在这里没有赋值表示并不使用pdf配置文件Variant PDFOption = new Variant("");//调用FileToPDF方法将ps文档转换为pdf文档Dispatch.callN(distiller, "FileToPDF", new Variant[] {inputPostScriptFilePath, outputPDFFilePath, PDFOption });System.out.println("由ps文档转换为pdf文档成功!");} catch (Exception ex) {ex.printStackTrace();} finally {closeWord();wordCom=null;//释放在程序线程中引用的其它com,比如Adobe PDFDistillerComThread.Release();}}public static void main(String[] argv) {D2P d2p = new D2P();d2p.docToPDF("d:/12.doc", "d:/1p.ps", "d:/1p.pdf");//这里是你建一个叫12.doc的word文档,生成的文档将在D盘下//1p.ps和1p.pdf(这是我们要的)}}
用java实现打印功能 怎么写 求大神求源码
PrintService[] services = PrinterJob.lookupPrintServices();//查找系统中所有的可用打印服务PrintService service = ServiceUI.printDialog(null, 500, 500, services, services[0], null, new HashPrintRequestAttributeSet());//创建一个默认的打印机选择对话框DocPrintJob job = service.createPrintJob();//创建一个打印任务Doc doc = new SimpleDoc(new FileInputStream(new File("D:\\image\\MM.jpg")), DocFlavor.INPUT_STREAM.JPEG, null);//创建一个打印的文档job.print(doc, null);//打印这是jdk提供的打印接口,你也可以找找其它的第三方类库,jdk提供打印接口功能一般般
JACOB类使用问题
public class Jacob { /** * 打开文件 * * @param documents * @param inputDocPath * @return */ private Dispatch open(Dispatch documents, String inputDocPath) { return Dispatch.call(documents, "Open", inputDocPath).toDispatch(); } /** * 选定内容 * * @param word * @return */ private Dispatch select(ActiveXComponent word) { return word.getProperty("Selection").toDispatch(); } /** * 把插入点移动到文件首位置 * * @param selection */ private void moveStart(Dispatch selection) { Dispatch.call(selection, "HomeKey", new Variant(6)); } /** * 从选定内容或插入点开始查找文本 * * @param selection * 选定内容 * @param toFindText * 要查找的文本 * @return true:查找到并选中该文本;false:未查找到文本。
*/ private boolean find(Dispatch selection, String toFindText) { // 从selection所在位置开始查询 Dispatch find = Dispatch.call(selection, "Find").toDispatch(); // 设置要查找的内容 Dispatch.put(find, "Text", toFindText); // 向前查找 Dispatch.put(find, "Forward", "True"); // 设置格式 Dispatch.put(find, "format", "True"); // 大小写匹配 Dispatch.put(find, "MatchCase", "True"); // 全字匹配 Dispatch.put(find, "MatchWholeWord", "True"); // 查找并选中 return Dispatch.call(find, "Execute").getBoolean(); } /** * 把选定内容替换为设定文本 * * @param selection * @param newText */ private void replace(Dispatch selection, String newText) { Dispatch.put(selection, "Text", newText); } /** * 全局替换 * * @param selection * @param oldText * @param replaceObj */ private void replaceAll(Dispatch selection, String oldText, Object replaceObj) { moveStart(selection); String newText = (String) replaceObj; while (find(selection, oldText)) { replace(selection, newText); Dispatch.call(selection, "MoveRight"); } } /** * 打印 * * @param document */ private void print(Dispatch document) { Dispatch.call(document, "PrintOut"); } /** * 保存文件 * * @param word * @param outputPath */ private void save(ActiveXComponent word, String outputPath) { Dispatch.call(Dispatch.call(word, "WordBasic").getDispatch(), "FileSaveAs", outputPath); } /** * 关闭文件 * * @param doc */ private void close(Dispatch doc) { Dispatch.call(doc, "Close", new Variant(true)); } /** * 保存打印doc文档 * * @param inputDocPath * @param outPutDocPath * @param data * @param isPrint */ public void saveDoc(String inputDocPath, String outPutDocPath, HashMap data, boolean isPrint) { // 初始化com的线程 ComThread.InitSTA(); // word运行程序对象 ActiveXComponent word = new ActiveXComponent("Word.Application"); // 文档对象 Dispatch wordObject = (Dispatch) word.getObject(); // 设置属性 Variant(true)表示word应用程序可见 Dispatch.put((Dispatch) wordObject, "Visible", new Variant(false)); // word所有文档 Dispatch documents = word.getProperty("Documents").toDispatch(); // 打开文档 Dispatch document = this.open(documents, inputDocPath); Dispatch selection = this.select(word); Iterator keys = data.keySet().iterator(); String oldText; Object newValue; while (keys.hasNext()) { oldText = (String) keys.next(); newValue = data.get(oldText); this.replaceAll(selection, oldText, newValue); } // 是否打印 if (isPrint) { this.print(document); } this.save(word, outPutDocPath); this.close(document); word.invoke("Quit", new Variant[0]); // 关闭com的线程 ComThread.Release(); } }
在linux环境下,java怎么实现从word格式转换为pdf格式
import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; /** * @author XuMing Li * * @version 1.00, 2007-4-9 * */ public class D2P { private ActiveXComponent wordCom = null; private Object wordDoc = null; private final Variant False = new Variant(false); private final Variant True = new Variant(true); /** * 打开word文档 * * @param filePath * word文档 * @return 返回word文档对象 */ public boolean openWord(String filePath) { //建立ActiveX部件 wordCom = new ActiveXComponent( "Word.Application "); try { //返回wrdCom.Documents的Dispatch Dispatch wrdDocs = wordCom.getProperty( "Documents ").toDispatch(); //调用wrdCom.Documents.Open方法打开指定的word文档,返回wordDoc wordDoc = Dispatch.invoke(wrdDocs, "Open ", Dispatch.Method, new Object[] { filePath }, new int[1]).toDispatch(); return true; } catch (Exception ex) { ex.printStackTrace(); } return false; } /** * 关闭word文档 */ public void closeWord() { //关闭word文件 wordCom.invoke( "Quit ", new Variant[] {}); } /** * * 将word文档打印为PS文件后,使用Distiller将PS文件转换为PDF文件 * * * @param sourceFilePath * 源文件路径 * * @param destinPSFilePath * 首先生成的PS文件路径 * * @param destinPDFFilePath * 生成PDF文件路径 */ public void docToPDF(String sourceFilePath, String destinPSFilePath, String destinPDFFilePath) { if (!openWord(sourceFilePath)) { closeWord(); return; } //建立Adobe Distiller的com对象 ActiveXComponent distiller = new ActiveXComponent( "PDFDistiller.PDFDistiller.1 "); try { //设置当前使用的打印机,我的Adobe Distiller打印机名字为 "Adobe PDF " wordCom.setProperty( "ActivePrinter ", new Variant( "Adobe PDF ")); //设置printout的参数,将word文档打印为postscript文档。
目前只使用了前5个参数,如果要使用更多的话可以参考MSDN的office开发相关api //是否在后台运行 Variant Background = False; //是否追加打印 Variant Append = False; //打印所有文档 int wdPrintAllDocument = 0; Variant Range = new Variant(wdPrintAllDocument); //输出的postscript文件的路径 Variant OutputFileName = new Variant(destinPSFilePath); Dispatch.callN((Dispatch) wordDoc, "PrintOut ", new Variant[] { Background, Append, Range, OutputFileName }); System.out.println( "由word文档打印为ps文档成功! "); //调用Distiller对象的FileToPDF方法所用的参数,详细内容参考Distiller Api手册 //作为输入的ps文档路径 Variant inputPostScriptFilePath = new Variant(destinPSFilePath); //作为输出的pdf文档的路径 Variant outputPDFFilePath = new Variant(destinPDFFilePath); //定义FileToPDF方法要使用adobe pdf设置文件的路径,在这里没有赋值表示并不使用pdf配置文件 Variant PDFOption = new Variant( " "); //调用FileToPDF方法将ps文档转换为pdf文档 Dispatch.callN(distiller, "FileToPDF ", new Variant[] { inputPostScriptFilePath, outputPDFFilePath, PDFOption }); System.out.println( "由ps文档转换为pdf文档成功! "); } catch (Exception ex) { ex.printStackTrace(); } finally { closeWord(); } } public static void main(String[] argv) { D2P d2p = new D2P(); // d2p.openWord( "c:/12.doc "); // d2p.callWordMacro( "c:/12.docc ", "MyWordMacro ", // new String[] { "这是调用word宏的测试程序 " }); d2p.docToPDF( "d:/12.doc ", "c:/1p.ps ", "c:/1p.pdf "); } }
jacob 实现excel 转换pdf时候,只能转换当前活动sheet,如何实现所有...
展开全部 在打开的“Word选项”对话框中,切换到“显示”选项卡。
在“打印选项”区域列出了可选的打印选项,选中每一项的作用介绍如下:(1)选中“打印在Word中创建的图形”选项,可以打印使用Word绘图工具创建的图形;(2)选中“打印背景色和图像”选项,可以打印为Word文档设置的背景颜色和在Word文档中插入的图片;(3)选中“打印文档属性”选项,可以打印Word文档内容和文档属性内容(例如文档创建日期、最后修改日期等内容);(4)选中“打印隐藏文字”选项,可以打印Word文档中设置为隐藏属性的文字;(5)选中“打印前更新域”选项,在打印Word文档以前首先更新Word文档中的域;(6)选中“打印前更新链接数据”选项,在打印Word文档以前首先更新Word文档中的链接。
...
转载请注明出处51数据库 » jacob打印word文件