java使用jacob控制office组件实现word、excel、PPT转换成PDF

java使用jacob控制office组件实现word、excel、PPT转换成PDF

本文介绍java直接调用Microsoft Office 或 WPS组件实现word、excel、PPT转换成PDF。此方法只适用windows服务器

  1. 首先要下载jacob-1.14.3-x64.dll或jacob-1.14.3-x86.dll组件,将组件放到jdk\bin目录下
    下载地址
    网盘下载 提取码:mz4i
  2. 配置jacob-1.14.3maven
   <dependency>
   	    <groupId>net.sf.jacob-project</groupId>
   	    <artifactId>jacob</artifactId>
   	    <version>1.14.3</version>
   	</dependency>
  1. 实现代码
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

/**
1. office转换工具
*/
public class OfficeConverterUtils {
  private static final int WDFO_RMATPDF = 17;
  private static final int XLTYPE_PDF = 0;
  private static final int PPT_SAVEAS_PDF = 32;
  public static final int WORD_HTML = 8;
  public static final int WORD_TXT = 7;
  public static final int EXCEL_HTML = 44;

  private OfficeConverterUtils() {
      throw new AssertionError();
  }

  /**
   * office转换成PDF
   * @param officePath office相关文件
   * @param postfix    文件后缀
   * @param pdfPath    PDF文件路径
   * @return 成功失败
   */
  public static boolean officeToPdf(String officePath, String postfix, String pdfPath) {
      if (postfix.startsWith("ppt")) {
          return pptToPdf(officePath, pdfPath);
      } else if (postfix.startsWith(".xls")) {
          return excelToPdf(officePath, pdfPath);
      } else {
          return wordToPdf(officePath, pdfPath);
      }
  }

  /**
   * word转换成PDF
   * @param wordPath 文件路径
   * @param pdfPath  文件路径
   * @return 成功失败
   */
  public static boolean wordToPdf(String wordPath, String pdfPath) {
      ActiveXComponent msWordApp = new ActiveXComponent("Word.Application");
      msWordApp.setProperty("Visible", new Variant(false));
      Dispatch docs = Dispatch.get(msWordApp, "Documents").toDispatch();
      Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[]{wordPath, new Variant(false), new Variant(true)}, new int[1]).toDispatch();

      Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{pdfPath, new Variant(WDFO_RMATPDF)}, new int[1]);
      if (null != doc) {
          Dispatch.call(doc, "Close", false);
      }
      return true;
  }

  /**
   * excel转换成PDF
   * @param inputPath 文件路径
   * @param pdfPath   文件路径
   * @return 成功失败
   */
  public static boolean excelToPdf(String inputPath, String pdfPath) {
      ActiveXComponent activeXComponent = new ActiveXComponent("Excel.Application");
      activeXComponent.setProperty("Visible", false);

      Dispatch excels = activeXComponent.getProperty("Workbooks").toDispatch();
      Dispatch excel = Dispatch.call(excels, "Open", inputPath, false, true).toDispatch();
      Dispatch.call(excel, "ExportAsFixedFormat", XLTYPE_PDF, pdfPath);
      Dispatch.call(excel, "Close", false);
      activeXComponent.invoke("Quit");
      return true;
  }

  /**
   * ppt转换成PDF
   * @param inputPath 文件路径
   * @param pdfPath   文件路径
   * @return 成功失败
   */
  public static boolean pptToPdf(String inputPath, String pdfPath) {
      //		ComThread.InitSTA();
      ActiveXComponent activeXComponent = new ActiveXComponent("PowerPoint.Application");
      //		activeXComponent.setProperty("Visible", new Variant(false));
      Dispatch ppts = activeXComponent.getProperty("Presentations").toDispatch();

      Dispatch ppt = Dispatch.call(ppts, "Open", inputPath, false, // ReadOnly
              true, // Untitled指定文件是否有标题
              true// WithWindow指定文件是否可见
                                  ).toDispatch();
      Dispatch.callN(ppt, "SaveAs", new Variant(pdfPath));
      Dispatch.call(ppt, "Close");
      activeXComponent.invoke("Quit");
      return true;
  }
}

FQA

1.如果office转换成PDF报 com.jacob.com.ComFailException: Can’t get object clsid from progid错误。原因有可能是tomcat配置使用windows服务启动导致,具体原因还没有弄清除。处理方法tomcat将windows服务启动关闭,使用tomcat的脚本启动解决问题。

你可能感兴趣的:(java,web,springMVC,Windows)