java实现word,ppt,excel转pdf

只用office自带的功能实现word,ppt,excel到pdf的转变

PS.你的office必须是07及以上,并且需要在文件另存为选项中有,“另存为pdf或XPS” 的一项

如果没有,你需要下载office相应插件SaveAsPDFandXPS

安装完插件后在word的帮助里面,选搜索点脱机开发人员帮助

再点 Word 2007 开发人员参考新增内容新的成员和常量WdSaveFormatwdFormatPDF
同样 在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[] {});
   }
  }
 }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值