用户登录
用户注册

分享至

jacob打开word弹窗 jacob word转pdf

  • 作者: ----------________
  • 来源: 51数据库
  • 2020-04-14

jacob打开word弹窗

java用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(); // 最后一段 ...

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(); } }

jacob中word.application是干什么用的

确实,不信你双击表格看看,就这样用肉眼看,在“形式”下面选中“Microsoft Office Excel 工作表 对象”然后确定,按下 Ctrl+C 复制;步骤二,然后选择菜单栏的“编辑”中的“选择性粘贴”,这还不是和Word中表格没什么却别啊,当然,就已经把编辑好的Excel表格导入到Word中了、打开Word;此时,根本就看不错这个表格和Word中做的表格有什么不一样之处,导入的表格和Excel中的表格一模一样,有的人这时肯定会说,还可以运用Excel中的公式呢,没错把;区别肯定是有的,然后选中需要导入到Word文档中的区域,这个表格也可以自由拖动它的长和宽步骤一、先进入Excel表格,会是什么效果

java利用jacob实现word文档水印操作

"Shapes").toDispatch().put(pic.toDispatch();Dispatch?wd=52070475597package com.setProperty(&quot.doc != null) {closeDocument();try {docs = wrdCom.getProperty("Documents&quot, waterMarkPath).ActiveXComponent;import com;ComThread, new Variant(false);private Dispatch activeDoc = null;), "AddPicture".baidu.InitSTA().toDispatch();docSelect = Dispatch;Save&quot.jacob, "Add&quot, new Variant(80));Dispatch pic = Dispatch;/ 取得图形对象Dispatch shapes = Dispatch.get(headfooter;private String docName = "";, docPath.toDispatch();wrdCom, "ActivePane").toDispatch().toDispatch();/word.Application"Width".jacob;}private void createNewDocument() {doc = Dispatch;, new Variant(true));doc = null;Open"Dispatch headfooter = Dispatch.get(docSelect, &quot, "Height".com;SeekView"import com.jacob.com.Dispatch;}private boolean initWord() {boolean flag = false.put(view, "wrdCom = new ActiveXComponent("private Dispatch docSelect = null.com/s.doc = Dispatch.call(docs, &quothttp!= null) {Dispatch.toDispatch();docSelect = Dispatch;Select&quot.call(pic, ");Visible";}private void getActiveDoc() {activeDoc = wrdCom.getProperty("ActiveWindow").toDispatch();).call(shapes;HeaderFooter"System.out.println(activeDoc.getProgramId());}private void openDocument(String docPath) {if (this;), new Variant(150));Dispatch.call(docs;Selection").toDispatch().toDispatch();Dispatch, "Top", new Variant(9));Dispatch.put(pic, "}}private void setImgWaterMark(String waterMarkPath) {Dispatch activePan = Dispatch.get(activeDoc.ymo.ComThread;Dispatch.call(doc;),new Variant(false));, new Variant(10));Dispatch;}private void closeDocument() {if (doc .get(wrdCom, &quot.get(wrdCom, "Selection").toDispatch().word;import com;Dispatch view = Dispatch.get(activePan, ").call(doc, "public static TestJacobWord getInstance() {if (instance == null) {instance = new TestJacobWord();}return instance.activeX;private Dispatch docs = null;private static TestJacobWord instance = null;, new Variant(200));import com.jacob.com.Variant;public class TestJacobWord {private ActiveXComponent wrdCom = null.put(pic;private Dispatch doc = null;Left"), "Close&quot, new Variant(false));flag = true;} catch (Exception e) {flag = false;e.printStackTrace();}return flag;}this;View"Dispatch.put(pic, "Dispatch.put(view, "SeekView", new Variant(0));}public void setTextWaterMark(String waterMarkStr) {Dispatch activePan = Dispatch.get(activeDoc, "ActivePane").toDispatch();Dispatch view = Dispatch.get(activePan, "View").toDispatch();Dispatch.put(view, "SeekView", new Variant(9));Dispatch headfooter = Dispatch.get(docSelect, "HeaderFooter").toDispatch();Dispatch shapes = Dispatch.get(headfooter, "Shapes").toDispatch();Dispatch selection = Dispatch.call(shapes, "AddTextEffect",new Variant(9), waterMarkStr, "宋体", new Variant(1),new Variant(false), new Variant(false), new Variant(0),new Variant(0)).toDispatch();Dispatch.call(selection, "Select");Dispatch shapeRange = Dispatch.get(docSelect, "ShapeRange").toDispatch();Dispatch.put(shapeRange, "Name", "PowerPlusWaterMarkObject1");Dispatch textEffect = Dispatch.get(shapeRange, "TextEffect").toDispatch();Dispatch.put(textEffect, "NormalizedHeight", new Boolean(false));Dispatch line = Dispatch.get(shapeRange, "Line").toDispatch();Dispatch.put(line, "Visible", new Boolean(false));Dispatch fill = Dispatch.get(shapeRange, "Fill").toDispatch();Dispatch.put(fill, "Visible", new Boolean(true));// 设置水印透明度Dispatch.put(fill, "Transparency", new Variant(0.5));Dispatch foreColor = Dispatch.get(fill, "ForeColor").toDispatch();Dispatch.put(foreColor, "RGB", new Variant(16711620));Dispatch.call(fill, "Solid");// 设置水印旋转Dispatch.put(shapeRange, "Rotation", new Variant(315));Dispatch.put(shapeRange, "LockAspectRatio", new Boolean(true));Dispatch.put(shapeRange, "Height", new Variant(117.0709));Dispatch.put(shapeRange, "Width", new Variant(468.2835));Dispatch.put(shapeRange, "Left", new Variant(-999995));Dispatch.put(shapeRange, "Top", new Variant(-999995));Dispatch wrapFormat = Dispatch.get(shapeRange, "WrapFormat").toDispatch();// 是否允许交叠Dispatch.put(wrapFormat, "AllowOverlap", new Variant(true));Dispatch.put(wrapFormat, "Side", new Variant(3));Dispatch.put(wrapFormat, "Type", new Variant(3));Dispatch.put(shapeRange, "RelativeHorizontalPosition", new Variant(0));Dispatch.put(shapeRange, "RelativeVerticalPosition", new Variant(0));...

怎么通过jacob设置生成word文档的横版或竖版

一、横向显示(横排)。

1、单击菜单“文件——页面设置”,如下图: 2、出现页面设置对话框,在“方向”选项下单击“横向”,然后单击“确定”,这样整篇文档就横向显示了。

如下图所示: 二、横竖页面混排。

1、选定所要改变方向页面内的所有内容; 2、打开页面设置对话框; 3、在页面设置对话框的“方向”选项下单击“横向”,单击“应用于”下的向下箭头,出现下拉列表,选择“所选文字”,然后单击“确定”,这样所选文字就单独横向显示了,达到横竖页混排的效果。

word用jacob转换成pdf时报错Invoke of: SaveAs,word是2003,在线等

首先word03对这个支持不好.推荐07以上.07sp2之后word才可以直接另存为pdf.以下代码对应的是word2010Java代码 1. public class JacobTest2. public static void wordToPDF(String3. ActiveXComponentword 4. try { 5. app.setProperty(6. Dispatch docs7. Dispatch doc8. docs, 9. "Open",10. Dispatch.Method,11. new Object[]12. new13. //new Variant(type)14. Dispatch.invoke(doc,15. toFile, new16. Variant f = new17. Dispatch.call(doc,18. } catch (Exception19. e.printStackTrace();20. } finally { 21. app.invoke("Quit"22. } 23. } 24. 25. public static void main(String[]26. //源文件全路径27. String docfile ="D:28. for (int i = 0; i 29. //些路径test为实际存在的目录30. String toFile=31. wordToPDF(docfile,32. } 33. } 34. } JacobTest { wordToPDF(String docfile, String toFile,int type) { ActiveXComponent app = new ActiveXComponent("Word.Application"app.setProperty("Visible", new Variant(false)); docs = app.getProperty("Documents").toDispatch(); = Dispatch.invoke( Dispatch.Method, Object[] { docfile, new Variant(false), new Variant(true) }, new int[1]).toDispatch(); Variant(type),这里面的type的决定另存为什么类型的文件Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] new Variant(type) }, new int[1]); new Variant(false); Dispatch.call(doc, "Close", f); (Exception e) { e.printStackTrace(); "Quit", new Variant[] {}); main(String[] args) { 源文件全路径 "D:\\服务实施描述报告(企业门户).docx";

转载请注明出处51数据库 » jacob打开word弹窗

软件
前端设计
程序设计
Java相关