温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在Java中使用jacob将word转换成pdf

发布时间:2021-02-23 15:05:39 来源:亿速云 阅读:222 作者:戴恩恩 栏目:编程语言

本文章向大家介绍如何在Java中使用jacob将word转换成pdf,主要包括如何在Java中使用jacob将word转换成pdf的使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Java可以用来干什么

Java主要应用于:1. web开发;2. Android开发;3. 客户端开发;4. 网页开发;5. 企业级应用开发;6. Java大数据开发;7.游戏开发等。

需要自定的maven依赖,如下:

<dependency>  <groupId>com.jacob</groupId>  <artifactId>jacob</artifactId>  <version>1.7</version>  <scope>system</scope>  <systemPath>${basedir}/../fileConvertApp/src/main/webapp/WEB-INF/lib/jacob.jar</systemPath> </dependency>

然后需要注意的是jar的地址,需要根据自己的情况修改

接下来我们贴一下具体使用的代码片段

import java.io.File;   import org.apache.log4j.Logger;   import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant;   /**  * Converter Util  *   * @author Jason  *  */ public class OfficeConverterUtil {    /**  * log  */  private static Logger logger = Logger.getLogger(OfficeConverterUtil.class);  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;  public static final int PPT_SAVEAS_JPG = 17;  // private static final int msoTrue = -1;  // private static final int msofalse = 0;    /**  * @param argInputFilePath  * @param argPdfPath  * @return  */  public static boolean officeFileConverterToPdf(String argInputFilePath, String argPdfPath) {  if (argInputFilePath.isEmpty() || argPdfPath.isEmpty() || getFileSufix(argInputFilePath).isEmpty()) {  logger.debug("输入或输出文件路徑有誤!");  return false;  }    String suffix = getFileSufix(argInputFilePath);    File file = new File(argInputFilePath);  if (!file.exists()) {  logger.debug("文件不存在!");  return false;  }    // PDF如果不存在则创建文件夹  file = new File(getFilePath(argPdfPath));  if (!file.exists()) {  file.mkdir();  }    // 如果输入的路径为PDF 则生成失败  if (suffix.equals("pdf")) {  System.out.println("PDF not need to convert!");  return false;  }    if (suffix.equals("doc") || suffix.equals("docx") || suffix.equals("txt")) {  return wordToPDF(argInputFilePath, argPdfPath);  } else if (suffix.equals("xls") || suffix.equals("xlsx")) {  return excelToPdf(argInputFilePath, argPdfPath);  } else if (suffix.equals("ppt") || suffix.equals("pptx")) {  return pptToPdf(argInputFilePath, argPdfPath);  // return ppt2PDF(argInputFilePath, argPdfPath);  }    return false;  }    /**  * converter word to 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();  // long pdfStart = System.currentTimeMillis();  Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] { wordPath, new Variant(false), new Variant(true) }, new int[1]).toDispatch();    deletePdf(pdfPath);    Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { pdfPath, new Variant(WDFO_RMATPDF) }, new int[1]);  // long pdfEnd = System.currentTimeMillis();  logger.debug(wordPath + ",pdf转换完成..");  if (null != doc) {  Dispatch.call(doc, "Close", false);  }  return true;  }    /**  * excel to pdf  *   * @param inputFile  * @param pdfFile  * @return  */  public static boolean excelToPdf(String inputFile, String pdfFile) {  ActiveXComponent activeXComponent = new ActiveXComponent("Excel.Application");  activeXComponent.setProperty("Visible", false);    deletePdf(pdfFile);    Dispatch excels = activeXComponent.getProperty("Workbooks").toDispatch();  Dispatch excel = Dispatch.call(excels, "Open", inputFile, false, true).toDispatch();  Dispatch.call(excel, "ExportAsFixedFormat", XLTYPE_PDF, pdfFile);  Dispatch.call(excel, "Close", false);  activeXComponent.invoke("Quit");  return true;  }    /**  * ppt to pdf  *   * @param inputFile  * @param pdfFile  * @return  */  public static boolean pptToPdf(String inputFile, String pdfFile) { // ComThread.InitSTA();  ActiveXComponent activeXComponent = new ActiveXComponent("PowerPoint.Application"); // activeXComponent.setProperty("Visible", new Variant(false));  Dispatch ppts = activeXComponent.getProperty("Presentations").toDispatch();    deletePdf(pdfFile);    Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, false, // ReadOnly  true, // Untitled指定文件是否有标题  true// WithWindow指定文件是否可见  ).toDispatch();   // Dispatch ppt = Dispatch.invoke(ppts, "Open", Dispatch.Method, new Object[] { inputFile, new Variant(false), new Variant(true) }, new int[1]).toDispatch();   // Dispatch.call(ppt, "SaveAs", pdfFile, PPT_SAVEAS_PDF); // Dispatch.call(ppt, "SaveAs", pdfFile, new Variant(PPT_SAVEAS_PDF)); // Dispatch.call(ppt, "SaveAs", pdfFile, new Variant(PPT_SAVEAS_PDF)); // Dispatch.invoke(ppt, "SaveAs", Dispatch.Method, new Object[] { pdfFile, PPT_SAVEAS_PDF }, new int[1]); // Dispatch.invoke(ppt, "SaveAs", Dispatch.Method, new Object[] { new Variant(PPT_SAVEAS_PDF) }, new int[1]);  Dispatch.callN(ppt, "SaveAs", new Variant(pdfFile));    Dispatch.call(ppt, "Close");    activeXComponent.invoke("Quit"); // ComThread.Release();  return true;  }    /**  * ppt to img  *   * @param inputFile  * @param imgFile  * @return  */  public static boolean pptToImg(String inputFile, String imgFile) {  // 打开word应用程序  ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");  // 设置word不可见,office可能有限制  // app.setProperty("Visible", false);  // 获取word中国所打开的文档,返回Documents对象  Dispatch files = app.getProperty("Presentations").toDispatch();  // 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document  Dispatch file = Dispatch.call(files, "open", inputFile, true, true, false).toDispatch();  // 调用Document对象的SaveAs方法,将文档保存为pdf格式  // Dispatch.call(doc, "ExportAsFixedFormat", outputFile,  // PPT_TO_PDF);  Dispatch.call(file, "SaveAs", imgFile, PPT_SAVEAS_JPG);  // 关闭文档  // Dispatch.call(file, "Close", false);  Dispatch.call(file, "Close");  // 关闭word应用程序  // app.invoke("Quit", 0);  app.invoke("Quit");  return true;  }    /**  * get file extension  *   * @param argFilePath  * @return  */  public static String getFileSufix(String argFilePath) {  int splitIndex = argFilePath.lastIndexOf(".");  return argFilePath.substring(splitIndex + 1);  }    /**  * subString file path  *   * @param argFilePath  *   file path  * @return filePaths  */  public static String getFilePath(String argFilePath) {  int pathIndex = argFilePath.lastIndexOf("/");  return argFilePath.substring(0, pathIndex);  }    /**  * 如果PDF存在则删除PDF  *   * @param pdfPath  */  private static void deletePdf(String pdfPath) {  File pdfFile = new File(pdfPath);  if (pdfFile.exists()) {  pdfFile.delete();  }  }   }

根据自己的调试,试验一下吧。

另外还有一段WPS转PDF,也贴一下,供大家参考一下

public void wps2PDF(String inputFile,String pdfFile) {    File sFile = new File(inputFile);   File tFile = new File(pdfFile);   ActiveXComponent wps = null;   try {    ComThread.InitSTA();    wps = new ActiveXComponent("wps.application");    ActiveXComponent doc = wps.invokeGetComponent("Documents").invokeGetComponent("Open", new Variant(sFile.getAbsolutePath()));     doc.invoke("ExportPdf", new Variant(tFile.getAbsolutePath()));     doc.invoke("Close");     doc.safeRelease();    } catch (Exception e) {     System.out.println(e.getMessage());    } finally {     if (wps != null) {      wps.invoke("Terminate");      wps.safeRelease();     }    ComThread.Release();   }   }

到此这篇关于如何在Java中使用jacob将word转换成pdf的文章就介绍到这了,更多相关的内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI