jacob操作word查找替换转为pdf

时间:2022-11-28 06:40:20

最近需要简单操作word文档后转为pdf,即查找word中的替换字符将其替换为需要的参数,最后生成pdf,由于服务器是在window平台下,所以选择了较为简单的jacob去实现这一功能。

工具:myeclipseoffice软件、jacob组件如下:

 jacob操作word查找替换转为pdf

步骤:

1、将下载好的jacob组件中的jacob.jar包加到项目中,myeclipse8适合版本为1.17,将相应的jacob-1.17.dll根据电脑系统类型(32位或64)放到csystem32目录下,同时根据jdk的版本将jacob-1.17.dll放到jdk安装目录jre下的bin文件夹下。

 

2、在Word文档中用特殊字符占位用户签名位置(例如:******),利用jacob操作Word文档查找特殊字符并替换为用户名(需要传入的参数),生成完整版的Word版本

 

3Word版转换为PDF版,完整代码如下:


import java.io.File;

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

public class EditWord {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new EditWord().editWord(docPath, pdfPath);
System.out.println("结束");
}
// word文档
private Dispatch doc = null;
// word运行程序对象
private ActiveXComponent wps = null;
// 所有word文档集合
private Dispatch documents = null;
// 选定的范围或插入点
private Dispatch selection;
private static String docPath = "word文档的路径";
private static String pdfPath = "生成pdf文件的路径";

public synchronized boolean editWord(String docPath,String pdfPath){
ComThread.InitMTA(true);//线程启动
File pdfFile = new File(pdfPath);
long start = System.currentTimeMillis();
//启动wps程序
wps = new ActiveXComponent("kwps.Application");
//设置程序不可见
wps.setProperty("Visible", new Variant(false));
// 禁用宏
wps.setProperty("AutomationSecurity", new Variant(3));
//获取所有文档
documents = wps.getProperty("Documents").toDispatch();
//获取当前打开的文档
doc = Dispatch.call(documents, "Open", docPath,false,true).toDispatch();
selection = Dispatch.get(wps, "Selection").toDispatch();
//查找并替换相关内容
if(!replaceText("******占位符()","需要传入的参数")){
return false;
}else{
try{
if(pdfFile.exists()){
pdfFile.delete();
}
//调用另存为pdf命令
Dispatch.call(doc, "SaveAs", pdfPath, 17);
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end-start) + "ms.");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("转化出错:" + ex.getMessage());
return false;
} finally {
//关闭文档,wps,以及进程
Dispatch.call(doc, "Close", false);
doc = null;
System.out.println("关闭WPS");
if (wps != null) {
wps.invoke("Quit", new Variant[]{});
wps = null;
}
documents = null;
selection = null;
ComThread.Release();
ComThread.quitMainSTA();
}
return true;
}
}

//查找方法
@SuppressWarnings("static-access")
public boolean find(String toFindText) {
if (toFindText == null || toFindText.equals(""))
return false;
// 从selection所在位置开始查询
Dispatch find = wps.call(selection, "Find").toDispatch();
// 设置要查找的内容
Dispatch.put(find, "Text", toFindText);
// 向前查找
Dispatch.put(find, "Forward", "True");
// 设置格式
Dispatch.put(find, "Format", "True");
// 大小写匹配
Dispatch.put(find, "MatchCase", "True");
// 全字匹配
Dispatch.put(find, "MatchWholeWord", "false");
// 查找并选中
return Dispatch.call(find, "Execute").getBoolean();
}

//替换方法
public boolean replaceText(String toFindText, String newText) {
if (!find(toFindText))
return false;
Dispatch.put(selection, "Text", newText);
return true;
}

}

关于jacob操作word的详细文档可以参考:

点击打开链接