java通过jacob实现office在线预览功能

 更新时间:2019年08月21日 15:25:18   作者:一只仰望天空的菜鸟  
这篇文章主要为大家详细介绍了java通过jacob实现office在线预览功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

简介:

这篇文章中的代码都是参考于网上的,只做一个记录。主要做的就是实现一个office在线预览功能。

第一步:装office

第二步:下载jacob

打开网址下载,目前最新的是1.19版本。

第三步:配置jdk

解压下载完的jacob压缩包,根据jdk的版本选择dll中的一个,放入/jdk/jre/bin中。

第四步:在项目中引入jar包

在maven官网上找不到com.jacob的jar包,只能手动引入,这个jar包在jacob的压缩包中有。

<dependency>
 <groupId>com.jacob</groupId>
 <artifactId>jacob</artifactId>
 <version>1.19</version>
 <scope>system</scope>
 <systemPath>${project.basedir}/lib/jacob.jar</systemPath>
</dependency>

第五步:将office转化为pdf文件

这里需要再次说明,这个代码不是我写的,这里只是做个记录,方便下次用到的时候直接使用。

import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletResponse;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;

@RestController
public class PdfConvert {

 @RequestMapping("/PdfConvert.do")
 public void PdfConvert(HttpServletResponse response) {
 String path = "C:\\Users\\acer\\Desktop\\测试.doc";
 String path2 = "C:\\Users\\acer\\Desktop\\测试.pdf";
 word2PDF(path, path2);
 String path3 = "C:\\Users\\acer\\Desktop\\测试2.ppt";
 String path4 = "C:\\Users\\acer\\Desktop\\测试2.pdf";
 ppt2PDF(path3, path4);
 String path5 = "C:\\Users\\acer\\Desktop\\测试3.xls";
 String path6 = "C:\\Users\\acer\\Desktop\\测试3.pdf";
 excel2PDF(path5, path6);
 }

 public boolean word2PDF(String inputFile, String pdfFile) {
 ActiveXComponent app = new ActiveXComponent("Word.Application");
 try {
  app.setProperty("Visible", false);
  Dispatch docs = app.getProperty("Documents").toDispatch();
  Dispatch doc = Dispatch.call(docs, "Open", new Object[]{inputFile, false, true}).toDispatch();
  Dispatch.call(doc, "ExportAsFixedFormat", new Object[]{pdfFile, 17});
  Dispatch.call(doc, "Close", new Object[]{false});
  app.invoke("Quit", 0);
  return true;
 } catch (Exception var6) {
  app.invoke("Quit", 0);
  return false;
 }
 }

 public boolean excel2PDF(String inputFile, String pdfFile) {
 ComThread.InitSTA(true);
 ActiveXComponent app = new ActiveXComponent("Excel.Application");
 try {
  app.setProperty("Visible", false);
  app.setProperty("AutomationSecurity", new Variant(3));
  Dispatch excels = app.getProperty("Workbooks").toDispatch();
  Dispatch excel = Dispatch.invoke(excels, "Open", 1, new Object[]{inputFile, new Variant(false), new Variant(false)}, new int[9]).toDispatch();
  Dispatch.invoke(excel, "ExportAsFixedFormat", 1, new Object[]{new Variant(0), pdfFile, new Variant(0)}, new int[1]);
  Dispatch.call(excel, "Close", new Object[]{false});
  if (app != null) {
  app.invoke("Quit", new Variant[0]);
  app = null;
  }

  ComThread.Release();
  return true;
 } catch (Exception var6) {
  app.invoke("Quit");
  return false;
 }
 }

 public boolean ppt2PDF(String inputFile, String pdfFile) {
 ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");

 try {
  Dispatch ppts = app.getProperty("Presentations").toDispatch();
  Dispatch ppt = Dispatch.call(ppts, "Open", new Object[]{inputFile, true, true, false}).toDispatch();
  Dispatch.call(ppt, "SaveAs", new Object[]{pdfFile, 32});
  Dispatch.call(ppt, "Close");
  app.invoke("Quit");
  return true;
 } catch (Exception var6) {
  app.invoke("Quit");
  return false;
 }
 }
}

第六步:在页面上展示pdf

后端:

@RequestMapping("/GetPdf.do")
 public void GetPdf(HttpServletResponse response) {
 //从数据库中查出文件位置和文件名字
 String pdfpath = "C:\\Users\\acer\\Desktop\\测试.pdf";
 String pdfname = "测试";
 try {
  File file = new File(pdfpath);
  if (!file.exists()) {
  response.getWriter().write("该文档生成pdf失败,请下载文档查看");
  return;
  }
  InputStream fis = new FileInputStream(pdfpath);
  byte[] buffer = new byte[1024];
  response.reset();
  response.addHeader("Content-Disposition", "inline;filename=" + java.net.URLEncoder.encode(pdfname, "UTF-8"));
  response.addHeader("Content-Length", "" + file.length());
  response.setContentType("application/pdf");
  OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
  int nbytes = 0;
  while ((nbytes = fis.read(buffer)) != -1) {
  toClient.write(buffer, 0, nbytes);
  toClient.flush();
  }
  toClient.flush();
  toClient.close();
  fis.close();
 } catch (Exception ex) {
  ex.printStackTrace();
 }
 }

前端:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8"/>
 <title>pdf在线预览</title>
</head>
<body>
<div style=" width: 100%; height: 100%;">
 <embed src="/GetPdf.do"
  type="application/pdf" style="overflow: auto; width: 100%; height: 800px;" />
</div>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • java实现RedisTemplate操作哈希数据

    java实现RedisTemplate操作哈希数据

    RedisTemplate是Spring Data Redis提供的一个用于操作Redis的模板类,本文主要介绍了java实现RedisTemplate操作哈希数据,具有一定的参考价值,感兴趣的可以了解一下
    2024-09-09
  • Java 中Object的wait() notify() notifyAll()方法使用

    Java 中Object的wait() notify() notifyAll()方法使用

    这篇文章主要介绍了Java 中Object的wait() notify() notifyAll()方法使用的相关资料,需要的朋友可以参考下
    2017-05-05
  • Spring框架AOP基础之代理模式详解

    Spring框架AOP基础之代理模式详解

    代理模式(Proxy Parttern)为一个对象提供一个替身,来控制这个对象的访问,即通过代理对象来访问目标对象。本文将通过示例详细讲解一下这个模式,需要的可以参考一下
    2022-11-11
  • Springbean的几种注入方式都了解吗

    Springbean的几种注入方式都了解吗

    这篇文章主要介绍了Springbean的几种注入方式都了解吗,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • Java org.w3c.dom.Document 类方法引用报错

    Java org.w3c.dom.Document 类方法引用报错

    这篇文章主要介绍了Java org.w3c.dom.Document 类方法引用报错的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • spring boot2.0总结介绍

    spring boot2.0总结介绍

    今天小编就为大家分享一篇关于spring boot2.0总结介绍,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Spring控制Bean加载顺序的操作方法

    Spring控制Bean加载顺序的操作方法

    正常情况下,Spring 容器加载 Bean 的顺序是不确定的,那么我们如果需要按顺序加载 Bean 时应如何操作?本文将详细讲述我们如何才能控制 Bean 的加载顺序,需要的朋友可以参考下
    2024-05-05
  • 解析Idea为什么不推荐使用@Autowired进行Field注入

    解析Idea为什么不推荐使用@Autowired进行Field注入

    这篇文章主要介绍了Idea不推荐使用@Autowired进行Field注入的原因,网上文章大部分都是介绍两者的区别,没有提到为什么,当时想了好久想出了可能的原因,今天来总结一下
    2022-05-05
  • Mybatis查询Sql结果未映射到对应得实体类上的问题解决

    Mybatis查询Sql结果未映射到对应得实体类上的问题解决

    使用mybatis查询表数据得时候,发现对应得实体类字段好多都是null,本文主要介绍了Mybatis查询Sql结果未映射到对应得实体类上的问题解决,具有一定的参考价值,感兴趣的可以了解一下
    2024-02-02
  • java分布式面试接口如何保证幂等及概念理解

    java分布式面试接口如何保证幂等及概念理解

    这篇文章主要为大家介绍了java分布式面试中接口如何保证幂等的问题解答以及概念描述,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-03-03

最新评论