评论

收藏

[Java] Java使用jacob将微软office中word、excel、ppt转成pdf

编程语言 编程语言 发布于:2021-10-06 14:39 | 阅读数:401 | 评论:0

这篇文章主要为大家详细介绍了Java使用jacob将微软office中word、excel、ppt转成pdf,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了java使用jacob将微软office文档转成pdf的具体代码,供大家参考,具体内容如下
在使用jacb前,我们需要去下载 jacob.jar 和 jacob-1.18-x64.dll
其次,我们需要将jacob-1.18-x64.dll放入到jdk的bin目录下才可以使用
第三,使用jacb之前,我们需要确保office能正常使用
如果你现在使用的是maven工程,那么不好意思,现在还没有发布正式的jacb资源文件,我们需要自定的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();
  } 
 }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持CodeAE代码之家
原文链接:https://blog.csdn.net/jesionsly/article/details/51728218

关注下面的标签,发现更多相似文章