您的位置:首页 > 编程语言 > Java开发

java实现MsOffice文档向pdf文档转化

2014-08-15 16:14 543 查看
本篇文档实现功能,将word和ppt文档的文件转化成pdf格式的文档

应用到jacob

第一步:下载压缩包

(1)jacob官网下载jacob压缩包

(2)网址:http://sourceforge.net/projects/jacob-project/

第二步:配置环境并导入jar包

(1)将下载好的压缩包解压

(2)将jacob.jar包导入项目中

(3)将jacob-1.18-M2-x64.dll和jacob-1.18-M2-x86.dll文件复制粘贴到jdk安装目录bin目录下,jdk安装目录jre的bin目录下,系统盘C:\Windows\System32目录下。

[b]第三步:调用MsOffice2Pdf类的静态方法,实现MsOffice文档转换。[/b]

[b]MsOffice2Pdf类的源代码[/b]

public class MsOffice2Pdf {

static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
public static final int wdFormatPDF = 17;// word转PDF 格式
public static final int ppSaveAsPDF = 32;// ppt 转PDF 格式

/**
* 将指定路径的word文档转换成指定路径的pdf文档
* 此处路径为绝对路径
* @Title: word2PDF
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param inputFile
* @param pdfFile
* @return void    返回类型
* @author 尚晓飞
* @date 2014-8-15 上午10:25:47
*/
public static void word2PDF(String inputFile,String pdfFile){
System.out.println("启动Word");
long start = System.currentTimeMillis();
ActiveXComponent app = null;
try {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", false);

Dispatch docs = app.getProperty("Documents").toDispatch();
System.out.println("打开文档" +inputFile);
Dispatch doc = Dispatch.call(docs,//
"Open", //
inputFile,// FileName
false,// ConfirmConversions
true // ReadOnly
).toDispatch();

System.out.println("转换文档到PDF " + pdfFile);
File tofile = new File(pdfFile);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc,//
"SaveAs", //
pdfFile, // FileName
wdFormatPDF);

Dispatch.call(doc, "Close", false);
long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.");
} catch (Exception e) {
System.out.println("========Error:文档转换失败:" + e.getMessage());
} finally {
if (app != null)
app.invoke("Quit", wdDoNotSaveChanges);
}

}

/**
* 将ppt格式的msoffice文档转换成pdf格式的文档
* @Title: ppt2pdf
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param inputFile
* @param pdfFile
* @return void    返回类型
* @author 尚晓飞
* @date 2014-8-18 下午2:00:21
*/
public static void ppt2pdf(String inputFile,String pdfFile){
System.out.println("启动PPT");
long start = System.currentTimeMillis();
ActiveXComponent app = null;
try {
app = new ActiveXComponent("Powerpoint.Application");
Dispatch presentations = app.getProperty("Presentations").toDispatch();
System.out.println("打开文档" + inputFile);
Dispatch presentation = Dispatch.call(presentations,//
"Open",
inputFile,// FileName
true,// ReadOnly
true,// Untitled 指定文件是否有标题。
false // WithWindow 指定文件是否可见。
).toDispatch();

System.out.println("转换文档到PDF " + pdfFile);
File tofile = new File(pdfFile);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(presentation,//
"SaveAs", //
pdfFile, // FileName
ppSaveAsPDF);

Dispatch.call(presentation, "Close");
long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.");
} catch (Exception e) {
System.out.println("========Error:文档转换失败:" + e.getMessage());
} finally {
if (app != null) app.invoke("Quit");
}
}

}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: