java实现word,ppt,excel转pdf
2013-04-26 10:13
399 查看
只用office自带的功能实现word,ppt,excel到pdf的转变
PS.你的office必须是07及以上,并且需要在文件另存为选项中有,“另存为pdf或XPS” 的一项
如果没有,你需要下载office相应插件SaveAsPDFandXPS
安装完插件后在word的帮助里面,选搜索→点脱机开发人员帮助
再点 Word 2007 开发人员参考→新增内容→新的成员和常量→WdSaveFormat→wdFormatPDF
同样 在ppt里面,我也找得到对象ppt到pdf的常量
但在excel里面找了很长时间都没找到, 既然excel的文件另存为那里有“另存为pdf”,那就应该有这个常量啊。请大神们赐教啊
工程必备文件
需要jar包
jacob.jar 下载链接 http://download.csdn.net/detail/da_zhuang/5302587
需要dll文件(放在C:\WINDOWS\system32下)
jacob.dll 下载链接 http://download.csdn.net/detail/da_zhuang/5302572
import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class Word2Pdf {
static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
static final int wdFormatPDF = 17;// word转PDF 格式
static final int ppSaveAsPDF = 32;// ppt 转PDF 格式
public static void main(String[] args) {
String source1 = "D:\\test.doc";
String source2 = "D:\\a.xls";
String source3 = "D:\\aa.ppt";
String target1 = "D:\\test1.pdf";
String target2 = "D:\\test2.pdf";
String target3 = "D:\\test3.pdf";
Word2Pdf pdf = new Word2Pdf();
pdf.word2pdf(source1, target1);
pdf.excel2pdf(source2, target2);
pdf.ppt2pdf(source3, target3);
}
public void word2pdf(String source,String target){
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("打开文档" + source);
Dispatch doc = Dispatch.call(docs,//
"Open", //
source,// FileName
false,// ConfirmConversions
true // ReadOnly
).toDispatch();
System.out.println("转换文档到PDF " + target);
File tofile = new File(target);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc,//
"SaveAs", //
target, // 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);
}
}
public void ppt2pdf(String source,String target){
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("打开文档" + source);
Dispatch presentation = Dispatch.call(presentations,//
"Open",
source,// FileName
true,// ReadOnly
true,// Untitled 指定文件是否有标题。
false // WithWindow 指定文件是否可见。
).toDispatch();
System.out.println("转换文档到PDF " + target);
File tofile = new File(target);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(presentation,//
"SaveAs", //
target, // 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");
}
}
public void excel2pdf(String source, String target) {
System.out.println("启动Excel");
long start = System.currentTimeMillis();
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动excel(Excel.Application)
try {
app.setProperty("Visible", false);
Dispatch workbooks = app.getProperty("Workbooks").toDispatch();
System.out.println("打开文档" + source);
Dispatch workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method, new Object[]{source, new Variant(false),new Variant(false)}, new int[3]).toDispatch();
Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] {
target, new Variant(57), new Variant(false),
new Variant(57), new Variant(57), new Variant(false),
new Variant(true), new Variant(57), new Variant(true),
new Variant(true), new Variant(true) }, new int[1]);
Variant f = new Variant(false);
System.out.println("转换文档到PDF " + target);
Dispatch.call(workbook, "Close", f);
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", new Variant[] {});
}
}
}
}
PS.你的office必须是07及以上,并且需要在文件另存为选项中有,“另存为pdf或XPS” 的一项
如果没有,你需要下载office相应插件SaveAsPDFandXPS
安装完插件后在word的帮助里面,选搜索→点脱机开发人员帮助
再点 Word 2007 开发人员参考→新增内容→新的成员和常量→WdSaveFormat→wdFormatPDF
同样 在ppt里面,我也找得到对象ppt到pdf的常量
但在excel里面找了很长时间都没找到, 既然excel的文件另存为那里有“另存为pdf”,那就应该有这个常量啊。请大神们赐教啊
工程必备文件
需要jar包
jacob.jar 下载链接 http://download.csdn.net/detail/da_zhuang/5302587
需要dll文件(放在C:\WINDOWS\system32下)
jacob.dll 下载链接 http://download.csdn.net/detail/da_zhuang/5302572
import java.io.File;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class Word2Pdf {
static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
static final int wdFormatPDF = 17;// word转PDF 格式
static final int ppSaveAsPDF = 32;// ppt 转PDF 格式
public static void main(String[] args) {
String source1 = "D:\\test.doc";
String source2 = "D:\\a.xls";
String source3 = "D:\\aa.ppt";
String target1 = "D:\\test1.pdf";
String target2 = "D:\\test2.pdf";
String target3 = "D:\\test3.pdf";
Word2Pdf pdf = new Word2Pdf();
pdf.word2pdf(source1, target1);
pdf.excel2pdf(source2, target2);
pdf.ppt2pdf(source3, target3);
}
public void word2pdf(String source,String target){
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("打开文档" + source);
Dispatch doc = Dispatch.call(docs,//
"Open", //
source,// FileName
false,// ConfirmConversions
true // ReadOnly
).toDispatch();
System.out.println("转换文档到PDF " + target);
File tofile = new File(target);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc,//
"SaveAs", //
target, // 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);
}
}
public void ppt2pdf(String source,String target){
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("打开文档" + source);
Dispatch presentation = Dispatch.call(presentations,//
"Open",
source,// FileName
true,// ReadOnly
true,// Untitled 指定文件是否有标题。
false // WithWindow 指定文件是否可见。
).toDispatch();
System.out.println("转换文档到PDF " + target);
File tofile = new File(target);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(presentation,//
"SaveAs", //
target, // 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");
}
}
public void excel2pdf(String source, String target) {
System.out.println("启动Excel");
long start = System.currentTimeMillis();
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动excel(Excel.Application)
try {
app.setProperty("Visible", false);
Dispatch workbooks = app.getProperty("Workbooks").toDispatch();
System.out.println("打开文档" + source);
Dispatch workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method, new Object[]{source, new Variant(false),new Variant(false)}, new int[3]).toDispatch();
Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] {
target, new Variant(57), new Variant(false),
new Variant(57), new Variant(57), new Variant(false),
new Variant(true), new Variant(57), new Variant(true),
new Variant(true), new Variant(true) }, new int[1]);
Variant f = new Variant(false);
System.out.println("转换文档到PDF " + target);
Dispatch.call(workbook, "Close", f);
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", new Variant[] {});
}
}
}
}
相关文章推荐
- java实现word,ppt,excel,jpg转pdf
- java利用jacob实现word,ppt,excel,jpg转pdf
- java实现word,ppt,excel,jpg转pdf
- java实现word,ppt,excel,jpg转pdf
- java实现爬虫技术,读取txt,word,excel,ppt,pdf,html等格式的文件
- java实现爬虫技术,读取txt,word,excel,ppt,pdf,html等格式的文件
- Java 使用jacob实现各类办公文档(ppt,Excel,word,text,imge)转换成PDF
- .NET WEB 实现office文档(word、excel、ppt)转PDF
- java通过url在线预览Word、excel、ppt、pdf、txt文档中的内容【只获得其中的文字】
- asp.net实现word、excel、ppt、pdf在线预览
- Ubuntu 服务器 PHP实现word、excel、ppt、pdf 等文档在线阅读功能的实现
- java实现在线预览--poi实现word、excel、ppt转html
- 将Office(如:Word、Excel、PPT 等)文件转PDF(通过OpenOffice实现)
- Asp.net MVC 利用(aspose+pdfobject.js) 实现在线预览word、excel、ppt、pdf文件
- JAVA提取Word,Excel,PPT,PDF,TXT等文档文字内容
- JAVA如何把word,excel,ppt转成PDF,已经过测试成功。
- JAVA实现word doc docx pdf excel的在线浏览 - 仿百度文库 源码
- java实现导出excel、word、 pdf
- PHP 实现 word/excel/ppt 转换为 PDF
- Java使用Jacob将wps的Word、Excel、PPT转化成PDF