您的位置首页百科知识

Word转换PDF在JAVA项目中的应用

Word转换PDF在JAVA项目中的应用

的有关信息介绍如下:

Word转换PDF在JAVA项目中的应用

现在社会,Word办公软件、PDF软件广泛的应用到我们的日常生活中,人们只熟悉他们基本的操作和功能,下面我就Word转换为PDF在JAVA项目中应用做相关介绍。

首先,安装word办公软件,安装完word办公软件,在安装word转pdf组件。

双击:SaveAsPDFandXPS.exe 逐步完成组件安装。

安装JDK1.6 ,下载JDK1.6 安装程序,双击执行程序逐步完成相关安装。

下载jacob-1.18-M1-x86.dll 或 jacob-1.18-M1-x64.dll 备用。

下载jacob.jar备用。

桌面找到我的电脑,双击进入,找到C盘,进入C盘目录下。

在C盘目录下找到WINDOWS,在WINDOWS盘符下找到system32点击进入。

a)如果系统为32位则将jacob-1.18-M1-x86.dll放到系统目录下的window/system32中和java安装目录下的bin目录下。

b)如果系统为64位则将jacob-1.18-M1-x64.dll放到系统目录下的window/system32中和java安装目录下的bin目录下。

将jacob.jar放入JAVA项目下lib文件夹中(项目--WebRoot--WEB-INF--lib)。

编写转化方法类:WordUtils。

代码相关:

public void saveAsFromWord(String sourceURL,String destPath,int docType){

JianYanBO bo = new JianYanBO();

//新建路径

makeDirByURL(destPath.substring(0, destPath.lastIndexOf("\\")+1));

//当发生以下异常时,将记录插入到tab_pdf表中

ActiveXComponent word =this.getActive(sourceURL, destPath, docType);

Dispatch doc =null;

// 核心代码

try {

//初始化com的线程

ComThread.InitSTA();

// 设置word属性

word.setProperty("Visible", new Variant(false));

word.setProperty("DisplayAlerts", new Variant(false));

try{

Thread.sleep(500);

}catch(Exception e){

}

Dispatch docs = word.getProperty("Documents").toDispatch();

Hashtable tab = new Hashtable();

try{

// 打开word文档

doc = Dispatch.invoke(

docs,

"Open",

Dispatch.Method,

new Object[] { sourceURL, new Variant(false),

new Variant(true) }, new int)

.toDispatch();

try{

Thread.sleep(2000);

}catch(Exception e){

}

Dispatch.invoke(doc, "SaveAs", Dispatch.Method,

new Object[] { destPath,

new Variant(docType) },

new int);

Variant file = new Variant(false);

// 关闭文档

Dispatch.call(doc, "Close", file);

String ip="";

try {

ip = InetAddress.getLocalHost().getHostAddress();

} catch (UnknownHostException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

String nowdate = df.format(new Date());

String val = bo.get_UniqueKey("TABPDF_SEQUENCE");

tab.put("ID", val);

tab.put("SOURCEURL", sourceURL);

tab.put("DEPTPATH", destPath);

tab.put("DOCTYPE", docType);

tab.put("CREATE_DATE", nowdate);

tab.put("FLAG", "0");

tab.put("IP", ip);

} catch (UnsatisfiedLinkError e) {

if(word!=null){

word.invoke("Quit", new Variant);

}

word = null;

ComThread.Release();

} catch (NoClassDefFoundError e) {

if(word!=null){

word.invoke("Quit", new Variant);

}

word = null;

ComThread.Release();

} catch(Exception e) {

if(word!=null){

word.invoke("Quit", new Variant);

}

word = null;

ComThread.Release();

}

try{

if(word!=null){

word.invoke("Quit", new Variant);

}

}catch (Exception e) {

e.printStackTrace();

System.out.println("关闭word进程失败");

}

word = null;

ComThread.Release();

System.out.println("转换完毕");

//转换成功后,修改表中状态

bo.UpdSql("update tab_pdf set flag='1' where DEPTPATH='"+destPath+"' and SOURCEURL='"+sourceURL+"' and DOCTYPE='"+docType+"'");

} catch (Exception e) {

e.printStackTrace();

System.out.println("文档转换失败");

}

}

相关异常处理,方法应用已各位项目为准。

项目中调用 WordUtils类下的savePdfFromWord(String url,String destPath,int docType)方法即可。

其中 url为需要打开的word文档的文件地址或者url地址

destPath为需要保存的文件地址

docType为保存的格式。Pdf为17.

1:Microsoft Word 97 - 2003 模板 (.dot)

2:文本文档 (.txt)

3:文本文档 (.txt)

4:文本文档 (.txt)

5:文本文档 (.txt)

6:RTF 格式 (.rtf)

7:文本文档 (.txt)

8:HTML 文档 (.htm)(带文件夹)

9:MHTML 文档 (.mht)(单文件)

10:MHTML 文档 (.mht)(单文件)

11:XML 文档 (.xml)

12:Microsoft Word 文档 (.docx)

13:Microsoft Word 启用宏的文档 (.docm)

14:Microsoft Word 模板 (.dotx)

15:Microsoft Word 启用宏的模板 (.dotm)

16:Microsoft Word 文档 (.docx)

17:PDF 文件 (.pdf)

18:XPS 文档 (.xps)

19:XML 文档 (.xml)

20:XML 文档 (.xml)

21:XML 文档 (.xml)

22:XML 文档 (.xml)

23:OpenDocument 文本 (.odt)

24:WTF 文件 (.wtf)