【jacob】java操作office让word转pdf

依赖

jacob.jar下载地址:http://www.downza.cn/soft/213490.html

需要在项目中引入该Jar包,并在java jdk和jre的bin目录下,添加 dll文件

 运行代码

call函数为office中的调用函数

public class Word2Pdf {
    public static void main(String[] args) {
        String source="D:\\JavaProject\\test2.doc";
        String target="D:\\JavaProject\\test2.pdf";
        //调用windows中的office程序
        ActiveXComponent app  = new ActiveXComponent("Word.application");
        app.setProperty("Visible",false);
        //获取文档
        Dispatch docs = app.getProperty("Documents").toDispatch();
        //打开指定的文档
        Dispatch doc = Dispatch.call(docs,"Open", source).toDispatch();
        //另存为 宏值17
        Dispatch.call(doc,"SaveAs",target,17);
        //调用关闭
        Dispatch.call(doc,"Close");
    }
}

猜你喜欢

转载自blog.csdn.net/kanseu/article/details/124421063