最近在做微信云打印服务器,云服务器与微信云服务器通信己经OK,现在有一个很核心的功能需要实现,就是服务器端文档的格式转换
目标:将office、txt、图片格式的文档转成PDF
查阅了一些资料,格式转换有4种方法:
1、Linux系统下采用JAVA语言调用Unoconv命令,通过LibreOffice将Office文档转成PDF;
2、Windows下采用C#语言调用.NET库中对应Office的COM组件将Office文档转成PDF;
3、Windows下采用JAVA语言调用OpenOffice将Office文件转成PDF;
4、Windows下采用JAVA语言调用Office的COM组件将Office文档转成PDF。
第1种方法的操作方法:在Linux系统命令行输入如下命令更新与安装Unoconv,LibreOffice为Linux系统自带程序:
sudo apt-get update
sudo apt-get install unoconv
通过格式转换命令
unoconv -f pdf xxx.doc
将xxx.doc转成pdf。
第3种方法经测试之后发现:若word文档页眉格式带表格等复杂格式时,转换之后的PDF页眉与前者不一致,故不采用。
第2种方法、第4种方法己实现,现将第4种方法的实现方式公布如下(第2种方法下一篇再公布):
目标:Windows下采用JAVA语言调用Office的COM组件将Office文档转成PDF
开发环境:win 7 myeclipse
需要组件:JCOM
步骤:首先下载JCOM组件包,包含jcom.dll、jcom.jar两个文件,jcom.dll放至系统盘windows/system32/下或JDK的Bin目录中;
JCom组件包下载地址: http://download.csdn.net/detail/secoler/9395978
然后,下面为格式转换代码
package com.pantum.format;
import jp.ne.so_net.ga2.no_ji.jcom.IDispatch;
import jp.ne.so_net.ga2.no_ji.jcom.ReleaseManager;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelApplication;
public class JComConvertor {
/**
* JCom调用MS Office转换word为PDF
*
* @param inputFile
* doc文档的绝对路径
* @param pdfFile
* 输出pdf文档的绝对路径,例如D:\\folder\\test.pdf
*/
public void word2PDF(String inputFile, String pdfFile) {
ReleaseManager rm = null;
IDispatch app = null;
try {
rm = new ReleaseManager();
app = new IDispatch(rm, "Word.Application");// 启动word
app.put("Visible", false); // 设置word不可见
IDispatch docs = (IDispatch) app.get("Documents"); // 获得word中所有打开的文档
IDispatch doc = (IDispatch) docs.method("Open", new Object[] {
inputFile, false, true });// 打开文档
doc.method("SaveAs", new Object[] { pdfFile, 17 });// 转换文档为pdf格式
doc.method("Close", new Object[] { false });
app.method("Quit", null);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
app = null;
rm.release();
rm = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* JCom调用MS Office转换excel为HTML
*
* @param inputFile
* 源文件绝对路径
* @param htmlFile
* 目标文件绝对路径
*/
public void excel2HTML(String inputFile, String htmlFile) {
ReleaseManager rm = null;
try {
rm = new ReleaseManager();
ExcelApplication ex = new ExcelApplication(rm);
ex.put("Visible", false);
IDispatch excs = (IDispatch) ex.get("Workbooks");
IDispatch doc = (IDispatch) excs.method("Open", new Object[] {
inputFile, false, true });// 打开文档
doc.method("SaveAs", new Object[] { htmlFile, 44 });
doc.method("Close", new Object[] { false });
ex.method("Quit", null);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rm.release();
rm = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* JCom调用MS Office转换Excel为XPS
*
* @param inputFile
* 源文件绝对路径
* @param xpsFile
* 目标文件绝对路径
*/
public void excel2XPS(String inputFile, String xpsFile) {
ReleaseManager rm = null;
try {
rm = new ReleaseManager();
ExcelApplication ex = new ExcelApplication(rm);
ex.put("Visible", false);
IDispatch excs = (IDispatch) ex.get("Workbooks");
IDispatch doc = (IDispatch) excs.method("Open", new Object[] {
inputFile, false, true });// 打开文档
doc.method("ExportAsFixedFormat", new Object[] { 1, xpsFile }); // 1代表XPS格式, 0代表PDF格式
doc.method("Close", new Object[] { false });
ex.method("Quit", null);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rm.release();
rm = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* JCom调用MS Office转换Excel为PDF
*
* @param inputFile
* 源文件绝对路径
* @param pdfFile
* 目标文件绝对路径
*/
public void excel2PDF(String inputFile, String pdfFile) {
ReleaseManager rm = null;
try {
rm = new ReleaseManager();
ExcelApplication ex = new ExcelApplication(rm);
ex.put("Visible", false);
IDispatch excs = (IDispatch) ex.get("Workbooks");
IDispatch doc = (IDispatch) excs.method("Open", new Object[] {
inputFile, false, true });// 打开文档
doc.method("ExportAsFixedFormat", new Object[] { 0, pdfFile }); // 1代表XPS格式, 0代表PDF格式
doc.method("Close", new Object[] { false });
ex.method("Quit", null);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rm.release();
rm = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* JCom调用MS Office转换PPT为PDF
*
* @param inputFile
* 源文件绝对路径
* @param pdfFile
* 目标文件绝对路径
*/
public void powerpoint2PDF(String inputFile, String pdfFile) {
ReleaseManager rm = null;
IDispatch app = null;
try {
rm = new ReleaseManager();
app = new IDispatch(rm, "PowerPoint.Application");// 启动word
// app.put("Visible", false); // 设置word不可见
IDispatch docs = (IDispatch) app.get("Presentations"); // 获得word中所有打开的文档
IDispatch doc = (IDispatch) docs.method("Open", new Object[] {
inputFile, false, true });// 打开文档
doc.method("SaveAs", new Object[] { pdfFile, 32 });// 转换文档为pdf格式
// doc.method("Close", new Object[] { false });
app.method("Quit", null);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
app = null;
rm.release();
rm = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* JCom调用MS Office转换PPT为JPG
*
* @param inputFile
* 源文件绝对路径
* @param jpgFile
* 目标文件绝对路径
*/
public void powerpoint2JPG(String inputFile, String jpgFile) {
ReleaseManager rm = null;
IDispatch app = null;
try {
rm = new ReleaseManager();
app = new IDispatch(rm, "PowerPoint.Application");// 启动word
// app.put("Visible", false); // 设置不可见
IDispatch docs = (IDispatch) app.get("Presentations"); // 获得word中所有打开的文档
IDispatch doc = (IDispatch) docs.method("Open", new Object[] {
inputFile, false, true });// 打开文档
doc.method("SaveAs", new Object[] { jpgFile, 17 });// 转换文档为pdf格式
// doc.method("Close", new Object[] { false });
app.method("Quit", null);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
app = null;
rm.release();
rm = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}