使用java jacob转换office到pdf,注意事项:
1.本机需要安装office,或者openoffice(未测试过)
2.下载最新的jacob的jar,解压后将相关的dll文件拷贝到java/jre/bin目录下.
3.如果包如下错误:
no jacob-1.18-x64 in java.library.path 原因可能是找不到相关dll文件,将配套dll文件拷贝到相关使用的jdk/jre/bin目录
转换代码(代码来源于互联网,由于时间原因,原创暂时无法考证):
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
/**
*
* @ClassName: OfficeToPDF
* @Description: 使用jacob转换office为pdf,也可以将txt文件转换为pdf.
* @author ***************
* @date 2015年9月21日 上午11:15:44
*
*/
public class OfficeToPDF {
private static final int wdFormatPDF = 17;
private static final int xlTypePDF = 0;
private static final int ppSaveAsPDF = 32;
private static final int msoTrue = -1;
private static final int msofalse = 0;
public static void main(String[] args) {
convert2PDF("D:\\aaaa.ppt","D:\\bbbbb.pdf");
}
public static 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;
}
}
public static String getFileSufix(String fileName){
int splitIndex = fileName.lastIndexOf(".");
return fileName.substring(splitIndex + 1);
}
/**
*
* @Title: word2PDF
* @Description: 转换word文档为pdf
* @param @param inputFile
* @param @param pdfFile
* @param @return 设定文件
* @return boolean 返回类型
* @throws
*/
public static boolean word2PDF(String inputFile,String pdfFile){
try{
//打开word应用程序
ActiveXComponent app = new ActiveXComponent("Word.Application");
//设置word不可见
app.setProperty("Visible", false);
//获得word中所有打开的文档,返回Documents对象
Dispatch docs = app.getProperty("Documents").toDispatch();
//调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
Dispatch doc = Dispatch.call(docs,
"Open",
inputFile,
false,
true
).toDispatch();
//调用Document对象的SaveAs方法,将文档保存为pdf格式
/*
Dispatch.call(doc,
"SaveAs",
pdfFile,
wdFormatPDF //word保存为pdf格式宏,值为17
);
*/
Dispatch.call(doc,
"ExportAsFixedFormat",
pdfFile,
wdFormatPDF //word保存为pdf格式宏,值为17
);
//关闭文档
Dispatch.call(doc, "Close",false);
//关闭word应用程序
app.invoke("Quit", 0);
return true;
}catch(Exception e){
return false;
}
}
/**
*
* @Title: excel2PDF
* @Description: 转换excel为PDF
* @param @param inputFile
* @param @param pdfFile
* @param @return 设定文件
* @return boolean 返回类型
* @throws
*/
public static boolean excel2PDF(String inputFile,String pdfFile){
try{
ActiveXComponent app = new ActiveXComponent("Excel.Application");
app.setProperty("Visible", false);
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.call(excels,
"Open",
inputFile,
false,
true
).toDispatch();
Dispatch.call(excel,
"ExportAsFixedFormat",
xlTypePDF,
pdfFile
);
Dispatch.call(excel, "Close",false);
app.invoke("Quit");
return true;
}catch(Exception e){
return false;
}
}
/**
*
* @Title: ppt2PDF
* @Description: 转换ppt为office
* @param @param inputFile
* @param @param pdfFile
* @param @return 设定文件
* @return boolean 返回类型
* @throws
*/
public static boolean ppt2PDF(String inputFile,String pdfFile){
try{
ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");
//app.setProperty("Visible", msofalse);
Dispatch ppts = app.getProperty("Presentations").toDispatch();
Dispatch ppt = Dispatch.call(ppts,
"Open",
inputFile,
true,//ReadOnly
true,//Untitled指定文件是否有标题
false//WithWindow指定文件是否可见
).toDispatch();
Dispatch.call(ppt,
"SaveAs",
pdfFile,
ppSaveAsPDF
);
Dispatch.call(ppt, "Close");
app.invoke("Quit");
return true;
}catch(Exception e){
return false;
}
}
}