使用jacob调用Windows的com对象,转换Office文件为pdfhtml等

Posted catoop

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用jacob调用Windows的com对象,转换Office文件为pdfhtml等相关的知识,希望对你有一定的参考价值。


1、介绍


     Jacob 是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。至于什么是COM组件,大家自己Google吧。



2、安装和配置



    Jacob是一个开源软件,它的官方站点是:

​http://danadler.com/jacob/​​ 大家可以到上面下载源代码研究,也可以直接下载编译后的二进制文件。



    下载包jacob_x.x.zip,解压后有几个文件:jacob.jar、jacob-x.x-M2-x86.dll


    把jacob-x.x-M2-x86.dll拷贝到%JAVA_HOME% 下的 bin 目录下,其中,%JAVA_HOME%就是JDK的安装目录。接着直接在java IDE中引用jacob.jar就可以使用了。



使用jacob调用Windows的com对象,转换Office文件为pdf、html等_jar


下面这个图,本文章中就未列出代码了,在我的资源共享中,有全的源代码,在我之前的博客中也有调用gs换的命令。

使用jacob调用Windows的com对象,转换Office文件为pdf、html等_java_02


3、换word为pdf、html、txt 的示例

package com.shanhy.demo.windowsoffice;

import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

/**
*
* 将jacob.dll放入JDK的bin目录下 把jacob.jar放入项目的buildPath中(web项目放到WEB-INF\\lib目录下)
*
* @author 单红宇
*
*/
public class ConvertToPdf

/*转PDF格式值*/
private static final int wdFormatPDF = 17;
private static final int xlFormatPDF = 0;
private static final int ppFormatPDF = 32;
private static final int msoTrue = -1;
private static final int msofalse = 0;

/*转HTML格式值*/
private static final int wdFormatHTML = 8;
private static final int ppFormatHTML = 12;
private static final int xlFormatHTML = 44;

/*转TXT格式值*/
private static final int wdFormatTXT = 2;

public boolean convert2PDF(String inputFile, String pdfFile)
String suffix = getFileSufix(inputFile);
File file = new File(inputFile);
if (!file.exists())
System.out.println("文件不存在!");
return false;

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 word2PDF(inputFile, pdfFile);
else if (suffix.equals("ppt") || suffix.equals("pptx"))
return ppt2PDF(inputFile, pdfFile);
else if (suffix.equals("xls") || suffix.equals("xlsx"))
return excel2PDF(inputFile, pdfFile);
else
System.out.println("文件格式不支持转换!");
return false;



/**
* 获取文件后缀
*
* @param fileName
* @return
* @author SHANHY
*/
private String getFileSufix(String fileName)
int splitIndex = fileName.lastIndexOf(".");
return fileName.substring(splitIndex + 1);


/**
* Word文档转换
*
* @param inputFile
* @param pdfFile
* @author SHANHY
*/
private boolean word2PDF(String inputFile, String pdfFile)
ComThread.InitSTA();

long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch doc = null;
try
app = new ActiveXComponent("Word.Application");// 创建一个word对象
app.setProperty("Visible", new Variant(false)); // 不可见打开word
app.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
Dispatch docs = app.getProperty("Documents").toDispatch();// 获取文挡属性

System.out.println("打开文档 >>> " + inputFile);
// Object[]第三个参数是表示“是否只读方式打开”
// 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
doc = Dispatch.call(docs, "Open", inputFile, false, true).toDispatch();
// 调用Document对象的SaveAs方法,将文档保存为pdf格式
System.out.println("转换文档 [" + inputFile + "] >>> [" + pdfFile + "]");
Dispatch.call(doc, "SaveAs", pdfFile, wdFormatPDF);//word保存为pdf格式宏,值为17
// Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF); // word保存为pdf格式宏,值为17

long end = System.currentTimeMillis();

System.out.println("用时:" + (end - start) + "ms.");
return java 有关word,excel,pdf转换成html 有几种方式

Java之Jacob调用COM接口DLL-----------------------------------dm。dll

JACOB - 远程调度COM接口调用

[JAVA]使用jacob进行服务端word转html pdf

java使用jacob调用com组件实现把excel的图表复制到word中

Java 使用 jacob 将 word 文档转换为 pdf 文件