jacorb转换word为html、pdf

public static void word2Html(String docfile,String htmlfile) {
ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
try {
app.setProperty("Visible", new Variant(false));// 设置word不可见
Object docs = app.getProperty("Documents").toDispatch();
Object doc = Dispatch.invoke((Dispatch) docs, "Open", Dispatch.Method,
new Object[] { docfile, new Variant(false), new Variant(true) }, new int[1]).toDispatch();// 打开word文件
Dispatch.invoke((Dispatch) doc, "SaveAs", Dispatch.Method
, new Object[] { htmlfile, new Variant(8) } // 8为html格式
, new int[1]);
Variant f = new Variant(false);
Dispatch.call((Dispatch) doc, "Close", f);
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
}
}

public static void word2PDF(String inputFile,String pdfFile){
    //打开word应用程序
    ActiveXComponent app = new ActiveXComponent("Word.Application");
    //设置word不可见
    app.setProperty("Visible", false);
    //获得word中所有打开的文档,返回Documents对象
    Dispatch docs = app.getProperty("Documents").toDispatch();
    //调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
    Dispatch doc = Dispatch.call(docs, "Open", inputFile, false, true ).toDispatch();
    //调用Document对象的SaveAs方法,将文档保存为pdf格式

    Dispatch.call(doc, "ExportAsFixedFormat",
            pdfFile, new Variant(17)        //word保存为pdf格式宏,值为17
            );
    //关闭文档
    Dispatch.call(doc, "Close", false);
    //关闭word应用程序
    app.invoke("Quit", 0);
}

public static void main(String[] args) {
String docfile = "D:/test.doc";
String htmlFile = "D:/test.html";
String pdfFile = "D:/test.pdf";
word2Html(docfile, htmlFile);
word2PDF(docfile, pdfFile);
}

猜你喜欢

转载自openstudy.iteye.com/blog/1833311
今日推荐