首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > PowerDesigner >

word转pdf实现小结

2013-09-11 
word转pdf实现总结之前项目有需求,要求java代码实现word转pdf功能.调研了很多技术都不是很成熟.特别是中文

word转pdf实现总结

之前项目有需求,要求java代码实现word转pdf功能.调研了很多技术都不是很成熟.特别是中文乱码的问题.大部分框架都是在中文环节出了问题.(我很奢望我能出生在一个英语国度).最终的技术选型是利用jacob实现word转pdf功能.但是服务器部署在linux服务器上.所以又产生了新的问题就是:linux和windows交互文件.这个问题还是比较容易解决的.windows上用的是pscp.exe向linux回传文件.linux使用ftp向window发送文件.

?

1.linux向windows发送文件,使用如下脚本:

#!/bin/bashftp -v -n xxx.xxx.xxx.xxx << EOFuser 用户名 密码binaryhashlcd /user/locale/ (发送文件所在的文件夹)promptmput $1(传进来的第一个参数 代表发送的文件名)byeEOF

?java调用linux脚本的方式如下:

Process process = Runtime.getRuntime().exec(command)

?文件成功发送到windows服务器之后,会利用spring的httpinvoker技术远程通知windows服务器有新文件到了.关于httpinvoker的使用可以看这篇文件:http://blog.csdn.net/qq413041153/article/details/7971194

?

2.windows需要装office2007sp1以上版本就可以使用jacob命令调用word接口生成pdf了.windows上java利用jacob word转pdf的代码如下:

private  synchronized void word2PDF(String docfile, String toFile) {logger.info("WTopTools word2PDF begin");ActiveXComponent app = new ActiveXComponent("Word.Application");try {app.setProperty("Visible", new Variant(false));Dispatch docs = app.getProperty("Documents").toDispatch();Dispatch doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] { docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch();File file1 = new File(toFile);File file2 = new File(toFile + ".pdf");if (file1.exists()) {file1.delete();}if (file2.exists()) {file2.delete();}Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {toFile, new Variant(17) }, new int[1]);Variant f = new Variant(false);Dispatch.call(doc, "Close", f);logger.info("转换成功:" + toFile);} catch (Exception e) {e.printStackTrace();logger.info("转换失败:" + docfile + ",  " + e.getMessage());} finally {app.invoke("Quit", new Variant[] {});}try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}logger.info("WTopTools word2PDF end");}
?

3.window使用pscp.exe向linux发送文件.同时利用httpinvoker通知linux服务器文件转换完成.在第一次使用pscp.exe的使用会有提示操作.如果你直接用java代码调用的话会直接推出.所有第一次使用请用cmd手动执行一次,并且根据选项操作一下就可以了. window发送文件到linux的bat脚本代码如下:

pscp.exe -q -pw 密码 -P 端口 d:\file\%1  wap@xxx.xxx.xxx.xxx:/usr/local/

?其中-pw参数后面是linux密码. -p后面是端口号, 在后面是window本地要发送的文件.%1表示调用bat脚本时传进来的第一个参数.wap@xxx.xxx.xxx.xxx:/usr/local/表示用wap用户链接xxx.xxx.xxx.xxxlinux服务器,并且文件发送到/user/local文件夹下.

?

热点排行