• WordtoPdfUtil word转pdf


    jar:

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

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

     

    import java.io.File;

    import com.jacob.activeX.ActiveXComponent;

    import com.jacob.com.ComThread;

    import com.jacob.com.Dispatch;

     

    public class WordtoPdfUtil {

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

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

     

       /**

        * 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");

       }

    }

  • 相关阅读:
    过滤器(Filter)
    DBUtils结果集处理器介绍
    Tomcat配置连接c3p0连接池
    JdbcUtils
    数据库连接池
    JDBC处理事务
    JDBC入门(5)--- 时间类型、大数据
    JDBC入门(4)--- 批处理
    JDBC入门(3)--- PrepareStatement
    JDBC入门(2)--- ResultSet之滚动结果集
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10401025.html
Copyright © 2020-2023  润新知