好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java使用jacob.jar将word转pdf

本文实例为大家分享了java利用jacob.jar将word转pdf的具体代码,供大家参考,具体内容如下

1.jacob.jar配置说明

jacob 就是 java-com bridge的缩写,提供自动化的访问com的功能,使用jacob.jar首先电脑要安装了office。
将jacob.jar jacob.jar导入到项目lib目录使用前,还要然后把jacob.bll放入c:\windows\system32目录下,同时还要放入java/jdk/jre/bin目录下(选择bll文件的时候,如果是32位就选86,64位选64)。

2.程序代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

package core.util;

 

import java.io.file;

 

import com.jacob.activex.activexcomponent;

import com.jacob测试数据.dispatch;

 

public class word2pdf {

  static final int wddonotsavechanges = 0 ; // 不保存待定的更改。

  static final int wdformatpdf = 17 ; // pdf 格式

  

  public static void wordtopdf(string wordpath,string pdfpath) {

 

    system.out.println( "启动word..." );

    long start = system.currenttimemillis();

    activexcomponent app = null ;

    try {

   //打开word应用程序

      app = new activexcomponent( "word.application" );

   ////设置应用操作是文档不在明面上显示,只在后台静默处理。

      app.setproperty( "visible" , false );

   //获得文档集合,用来操作我们需要处理的文档.

      dispatch docs = app.getproperty( "documents" ).todispatch();

      system.out.println( "打开文档..." + wordpath);

   //打开word文档

      dispatch doc = dispatch.call(docs, //

          "open" , //

          wordpath, // filename

          false , // confirmconversions

          true // readonly

          ).todispatch();

  

      system.out.println( "转换文档到pdf..." + pdfpath);

      file tofile = new file(pdfpath);

   //创建存放pdf的文件夹

      if (tofile.exists()) {

        tofile.delete();

      }

   //将word另存为pdf

      dispatch.call(doc, //

          "saveas" , //

          pdfpath, // filename

          wdformatpdf);

      //关闭word文档

      dispatch.call(doc, "close" , false );

      long end = system.currenttimemillis();

      system.out.println( "转换完成..用时:" + (end - start) + "ms." );

    } catch (exception e) {

      system.out.println( "========error:文档转换失败:" + e.getmessage());

    } finally {

      if (app != null )

        app.invoke( "quit" , wddonotsavechanges);

    }

  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/huanshirenjian/article/details/46407979

查看更多关于java使用jacob.jar将word转pdf的详细内容...

  阅读:21次