使用jacob调用Windows的com对象,进行word、ppt等转换成ptf、html (2)

}
return false;
}

/**
* PPT文档转换
*
* @param inputFile
* @param pdfFile
* @author SHANHY
*/
private boolean ppt2PDF(String inputFile, String pdfFile) {
ComThread.InitSTA();

long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch ppt = null;
try {
app = new ActiveXComponent("PowerPoint.Application");// 创建一个PPT对象
// app.setProperty("Visible", new Variant(false)); // 不可见打开(PPT转换不运行隐藏,所以这里要注释掉)
// app.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
Dispatch ppts = app.getProperty("Presentations").toDispatch();// 获取文挡属性

System.out.println("打开文档 >>> " + inputFile);
// 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
ppt = Dispatch.call(ppts, "Open", inputFile,
true,// ReadOnly
true,// Untitled指定文件是否有标题
false// WithWindow指定文件是否可见
).toDispatch();

System.out.println("转换文档 [" + inputFile + "] >>> [" + pdfFile + "]");
Dispatch.call(ppt, "SaveAs", pdfFile, ppFormatPDF);

long end = System.currentTimeMillis();

System.out.println("用时:" + (end - start) + "ms.");

return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("========Error:文档转换失败:" + e.getMessage());
} finally {
Dispatch.call(ppt, "Close");
System.out.println("关闭文档");
if (app != null)
app.invoke("Quit", new Variant[] {});
}
ComThread.Release();
ComThread.quitMainSTA();
return false;
}

/**
* Excel文档转换
*
* @param inputFile
* @param pdfFile
* @author SHANHY
*/
private boolean excel2PDF(String inputFile, String pdfFile) {
ComThread.InitSTA();

long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch excel = null;
try {
app = new ActiveXComponent("Excel.Application");// 创建一个PPT对象
app.setProperty("Visible", new Variant(false)); // 不可见打开
// app.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
Dispatch excels = app.getProperty("Workbooks").toDispatch();// 获取文挡属性

System.out.println("打开文档 >>> " + inputFile);
// 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
excel = Dispatch.call(excels, "Open", inputFile, false, true).toDispatch();
// 调用Document对象方法,将文档保存为pdf格式
System.out.println("转换文档 [" + inputFile + "] >>> [" + pdfFile + "]");
// Excel 不能调用SaveAs方法
Dispatch.call(excel, "ExportAsFixedFormat", xlFormatPDF, pdfFile);

long end = System.currentTimeMillis();

System.out.println("用时:" + (end - start) + "ms.");
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("========Error:文档转换失败:" + e.getMessage());
} finally {
Dispatch.call(excel, "Close", false);
System.out.println("关闭文档");
if (app != null)
app.invoke("Quit", new Variant[] {});


}

return false;
}

/**
* 测试
*
* @param args
* @author SHANHY
*/
public static void main(String[] args) {
ConvertToPdf d = new ConvertToPdf();
d.convert2PDF("g:\\test.docx", "g:\\test.pdf");
d.convert2PDF("g:\\testppt.pptx", "g:\\testppt.pdf");
d.convert2PDF("g:\\testexcel.xlsx", "g:\\testexcel.pdf");
}

}

 

读、写Word的简单示例

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

 


4、jacob.jar的结构

jacob包括两个部分:

com.jacob.activeX: ActiveXComponent类
com.jacob.com: 其它类和元素

5、Jacob类

Jacob的结构很简单,包含以下几个类:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzgjgd.html