WordtoPdfUtil word转pdf

jar:java

<dependency>
  <groupId>com.jacob</groupId>
  <artifactId>jacob</artifactId>
  <version>1.10</version>
</dependency>tomcat

在tomcat上使用时要在tomcat使用的jdk的jdk/jre/bin目录下放置配套的jacob.dll文件app

 

import java.io.File;ui

import com.jacob.activeX.ActiveXComponent;spa

import com.jacob.com.ComThread;orm

import com.jacob.com.Dispatch;ci

 

public class WordtoPdfUtil {文档

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

   static final int wdFormatPDF = 17;// PDF 格式it

 

   /**

    * word转pdf

    * @param wordSrc    word路径

    * @param pdfSrc     另存为pdf后的路径

    */

   public static void  wordToPdf(String wordSrc,String pdfSrc){

          long start = System.currentTimeMillis();

          ActiveXComponent app = null;

          Dispatch docs=null;

          try {

            System.runFinalizersOnExit(true);

            app = new ActiveXComponent("Word.Application");

              app.setProperty("Visible", false);

 

               docs = app.getProperty("Documents").toDispatch();

              System.out.println("打开文档" + wordSrc);

              Dispatch doc = Dispatch.call(docs,//

                      "Open", //

                      wordSrc,// FileName

                      false,// ConfirmConversions

                      true // ReadOnly

                      ).toDispatch();

 

              System.out.println("转换文档到PDF" + pdfSrc);

              File tofile = new File(pdfSrc);

               //若是输出目标文件夹不存在,则建立

              if (!tofile.getParentFile().exists()){

               tofile.getParentFile().mkdirs();

              }

              Dispatch.call(doc,//

                      "SaveAs", //

                      pdfSrc, // FileName

                      wdFormatPDF);

 

              Dispatch.call(doc, "Close", false);

              long end = System.currentTimeMillis();

             

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

          } catch (Exception e) {

            e.printStackTrace();

              System.out.println("========Error:文档转换失败:" + e.getMessage());

          } finally {

              if (app != null){

              app.invoke("Quit", wdDoNotSaveChanges);

              }

              if(docs != null){

               ComThread.Release();

                 ComThread.RemoveObject(docs);

              }      

          }

      }

   public static void main(String[] args) {

      WordtoPdfUtil.wordToPdf("C:\\Users\\Administrator\\Desktop\\qcs.docx","C:\\Users\\Administrator\\Desktop\\qcs.pdf");

   }

}