「javajacob性能」java性能差
本篇文章给大家谈谈javajacob性能,以及java性能差对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java利用jacob实现word文档水印操作
 - 2、关于JAVA中的jacob的问题
 - 3、如何利用Java-JACOB操作WORD文档
 - 4、java中使用jacob怎么读取word里的部分内容,以字符串形式返回
 - 5、java用jacob操作word的问题
 - 6、java 操作 word jacob
 
java利用jacob实现word文档水印操作
package com.ymo.word;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class TestJacobWord {
private ActiveXComponent wrdCom = null;
private Dispatch doc = null;
private Dispatch activeDoc = null;
private Dispatch docSelect = null;
private Dispatch docs = null;
private static TestJacobWord instance = null;
private String docName = "";
public static TestJacobWord getInstance() {
if (instance == null) {
instance = new TestJacobWord();
}
return instance;
}
private boolean initWord() {
boolean flag = false;
ComThread.InitSTA();
wrdCom = new ActiveXComponent("word.Application");
try {
docs = wrdCom.getProperty("Documents").toDispatch();
wrdCom.setProperty("Visible", new Variant(false));
flag = true;
} catch (Exception e) {
flag = false;
e.printStackTrace();
}
return flag;
}
private void createNewDocument() {
doc = Dispatch.call(docs, "Add").toDispatch();
docSelect = Dispatch.get(wrdCom, "Selection").toDispatch();
}
private void getActiveDoc() {
activeDoc = wrdCom.getProperty("ActiveWindow").toDispatch();
System.out.println(activeDoc.getProgramId());
}
private void openDocument(String docPath) {
if (this.doc != null) {
closeDocument();
}
this.doc = Dispatch.call(docs, "Open", docPath, new Variant(false),
new Variant(false)).toDispatch();
docSelect = Dispatch.get(wrdCom, "Selection").toDispatch();
}
private void closeDocument() {
if (doc != null) {
Dispatch.call(doc, "Save");
Dispatch.call(doc, "Close", new Variant(true));
doc = null;
}
}
private void setImgWaterMark(String waterMarkPath) {
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 pic = Dispatch.call(shapes, "AddPicture", waterMarkPath)
.toDispatch();
Dispatch.call(pic, "Select");
Dispatch.put(pic, "Left", new Variant(10));
Dispatch.put(pic, "Top", new Variant(200));
Dispatch.put(pic, "Width", new Variant(150));
Dispatch.put(pic, "Height", new Variant(80));
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", "PowerPlusWaterMarkObject
1");
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, "RelativeHorizontalPositi
on", new Variant(0));
Dispatch.put(shapeRange, "RelativeVerticalPosition
", new Variant(0));
Dispatch.put(view, "SeekView", new Variant(0));
}
private void closeWord() {
// 关闭word文件
wrdCom.invoke("Quit", new Variant[] {});
// 释放com线程
ComThread.Release();
}
public String getDocName() {
return docName;
}
public void setDocName(String docName) {
this.docName = docName;
}
private boolean addWaterMark(String wordPath, String waterMarkPath) {
boolean flag = false;
try {
if (initWord()) {
openDocument(wordPath);
getActiveDoc();
setImgWaterMark(waterMarkPath);
closeDocument();
closeWord();
flag = true;
} else {
flag = false;
}
} catch (Exception e) {
flag = false;
e.printStackTrace();
closeDocument();
closeWord();
}
return flag;
}
public static void main(String[] args) {
TestJacobWord jacob = TestJacobWord.getInstance();
// jacob.addWaterMark("F://test//test.doc", "F://test//ymo.jpg");
try {
if (jacob.initWord()) {
jacob.openDocument("F://test/test.doc");
jacob.getActiveDoc();
jacob.setTextWaterMark("重庆宇能科技有限公司");
jacob.closeDocument();
jacob.closeWord();
}
} catch (Exception e) {
e.printStackTrace();
jacob.closeDocument();
jacob.closeWord();
}
}
}
关于JAVA中的jacob的问题
docs 这个变量时Object类型的
Dispatch.invoke()第一个参数需要Dispatch类型的
把docs和doc都申明为Dispatch类型的,或者强制转化。
你不用IDE工具的吗?用eclipse的话这段话都有错误提示的,一看就知道了。
如何利用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(); // 最后一段
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)); //小四
Dispatch.call(document, "SaveAs", new Variant("C:
abc.doc")); // 保存一个新文档
ComThread.Release();//释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理
}
}
java中使用jacob怎么读取word里的部分内容,以字符串形式返回
FileInputStream in = new FileInputStream("D://22.doc ");
WordExtractor extractor = new WordExtractor();
String str = extractor.extractText(in);
int start = str.indexOf("AA");
int end = str.indexOf("BB");
System.out.println(str.substring(start, end));
记得导入tm-extractors-0.4.jar 包
java用jacob操作word的问题
MSWordManager msWord_Open = new MSWordManager(false);
如果这个类是可以共用的话,可以声明为一个实例变量。不然的话,你可以选择每个按钮的监听事件里new一个这个对象
java 操作 word jacob
这是word模板替换。这样做,你先把那个word复制下来,然后在清空目标,然后在粘贴。我们用POI,jacob没用过,具体思想是这样。。
关于javajacob性能和java性能差的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。