Java利用Jacob实现excel,ppt,word在线转PDF并预览

项目中上传的excel,ppt,word等文件,由于无法直接在HTML页面查看,所以在后台转成pdf再在页面上展示。word、excel文档转换起来一般没什么问题,PPT转换成PDF问题很多。

说明:(1).本文中说的解决方案支持将doc,docx,xls,xlsx,ppt,pptx这些格式转成pdf。

           (2).方案不止这一种,只是提供一种思路。

1.首先下载jacob.zip ,地址:https://sourceforge.net/projects/jacob-project/

解压完成后,根据自己系统版本,64位系统就用 x64的dll,32位系统就用x86的dll。将dll文件入自己的jdk的jre的bin安装目录下,本人jdk目录C:\Program Files\Java\jdk1.8.0_201\jre\bin,放到下面即可。如下图所示

2.将jacob.jar包引入到项目中,其中<systemPath>${JAVA_HOME}/lib/jacob.jar</systemPath>中jar包的路径自己定义,能找到这个jar包就可以了,我自己是将jacob.jar 放到JDK安装目录下了,在通过路径获取到。

<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>19.0</version>
		</dependency>

		<dependency>
			<groupId>com.jacob</groupId>
			<artifactId>jacob</artifactId>
			<version>1.18</version>
			<scope>system</scope>
			<systemPath>${JAVA_HOME}/lib/jacob.jar</systemPath>
		</dependency>

3.下面直接看下测试代码及其中遇到的问题吧

private static final int wdFormatPDF = 17;//word保存为pdf格式宏,值为17 
    //word转PDF格式,数据库路径D:/file/20200624/HFILE2020062400859.docx (本文案例)
	@ResponseBody
	@RequestMapping("/wordtoPdf")
	public ModelAndView wordtoPdf(OnlineExperience exper) {
		ModelAndView mv = new ModelAndView("exper/view_file");
		exper = experienceService.queryExperienceById(exper);//查询数据库文件路径
		ActiveXComponent app = null;
		Dispatch doc = null;
		try {
			app = new ActiveXComponent("Word.Application");
			app.setProperty("Visible", new Variant(false));
			Dispatch docs = app.getProperty("Documents").toDispatch();

			String startFile = exper.getContent();// 转换前的文件路径
			int pdf = startFile.indexOf(".");
			String pdfPath = startFile.substring(0, pdf);
			String overFile = pdfPath + ".pdf";// 转换后的文件路劲

			doc = Dispatch.call(docs, "Open", startFile).toDispatch();
			File tofile = new File(overFile);
			if (tofile.exists()) {
				tofile.delete();
			}
			Dispatch.call(doc, "SaveAs", overFile, wdFormatPDF);
			exper.setContent(this.editImgPath(overFile));
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			Dispatch.call(doc, "Close", false);
			if (app != null)
				app.invoke("Quit", new Variant[] {});
		} // 结束后关闭进程
		ComThread.Release();

		mv.addObject("exper", exper);
		return mv;
	}


   // excel转换为pdf
	private static final int xlTypePDF = 0;
	public static boolean exceltoPDF(String inputFile, String pdfFile) {
		try {
			ActiveXComponent app = new ActiveXComponent("Excel.Application");
			app.setProperty("Visible", false);
			Dispatch excels = app.getProperty("Workbooks").toDispatch();
			Dispatch excel = Dispatch.call(excels, "Open", inputFile, false, true).toDispatch();
			Dispatch.call(excel, "ExportAsFixedFormat", xlTypePDF, pdfFile);
			Dispatch.call(excel, "Close", false);
			app.invoke("Quit");
			return true;
		} catch (Exception e) {
			return false;
		}
	}

//这两个比较简单,就不多做说明
    // ppt转换为pdf
	public static boolean ppt2PDF(String inputFile, String pdfFile) {
		try {
			ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");//KWPP
			// app.setProperty("Visible", msofalse);
			Dispatch ppts = app.getProperty("Presentations").toDispatch();

			Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true, // ReadOnly
					true, // Untitled指定文件是否有标题
					false// WithWindow指定文件是否可见
			).toDispatch();
			Dispatch.call(ppt, "SaveAs", pdfFile, ppSaveAsPDF);
			Dispatch.call(ppt, "Close");
			app.invoke("Quit");
			return true;
		} catch (Exception e) {
			return false;
		}
	}

如果ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");出现以下原因,
提供一种解决办法:1.可将“PowerPoint.Application” 替换为“KWPP.Application”
com.jacob.com.ComFailException: Invoke of: SaveAs
Source: Microsoft PowerPoint
Description: Presentation.SaveAs : PowerPoint 无法将 ^0 保存到 ^1。

	at com.jacob.com.Dispatch.invokev(Native Method)
	at com.jacob.com.Dispatch.invokev(Dispatch.java:625)
	at com.jacob.com.Dispatch.callN(Dispatch.java:453)
	at com.jacob.com.Dispatch.call(Dispatch.java:541)
	at hk.wisdom.controller.OnlineExperienceController.toPdf(OnlineExperienceController.java:155)

提供一个小maven项目DEMO,可通过链接下载测试

链接:https://pan.baidu.com/s/1u6yLVpVnoXn5qK27w74n9Q 
提取码:sqbh

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值