1.前言

MS Office2010是可以的,理论上2007版本也可以,博主没试过;

Wps2015是可以的,理论上Wps2016也能用,Wps理论上还兼容MS Office的相关代码,有时间的可以试试;

Wps和Ms Office都需要导入jacob-1.18.jar包,以及将jacob-1.18-x64.dll或者jacob-1.18-x64.dll放置到jdk的bin目录(或者windows的System32/SysWoW64目录下)

而OpenOffice需要导入jodconverter-2.2.1.jar等相关包;

相关包的下载链接,包含实现代码:http://download.csdn.net/detail/huitoukest/9506740

包中的代码可能没有捕获NoClassDefFoundError异常和,建议使用者自行捕获;

2.MS Office2010

核心代码如下:

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;/*** @return 操作成功与否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置错误; 如果返回 0,*         则表示操作成功; 返回1, 则表示转换失败*/@Overridepublic int officeToPdf(OfficeToPDFInfo officeToPDFInfo) {String sourceFile=officeToPDFInfo.sourceUrl;String destFile=officeToPDFInfo.destUrl;File inputFile = new File(sourceFile);if (!inputFile.exists()) {return -1;// 找不到源文件, 则返回-1}// 如果目标路径不存在, 则新建该路径File outputFile = new File(destFile);if (!outputFile.getParentFile().exists()) {outputFile.getParentFile().mkdirs();}String extentionName=FileUtils.getFileExtension(sourceFile);if(extentionName.equalsIgnoreCase("ppt")||extentionName.equalsIgnoreCase("pptx")){ppt2pdf(officeToPDFInfo.sourceUrl,officeToPDFInfo.destUrl);}else if(extentionName.equalsIgnoreCase("doc")||extentionName.equalsIgnoreCase("docx")){doc2pdf(officeToPDFInfo.sourceUrl,officeToPDFInfo.destUrl);}else if(extentionName.equalsIgnoreCase("xls")||extentionName.equalsIgnoreCase("xlsx")){excel2PDF(officeToPDFInfo.sourceUrl,officeToPDFInfo.destUrl);}    return 0;}protected static boolean doc2pdf(String srcFilePath, String pdfFilePath) {  ActiveXComponent app = null;  Dispatch doc = null;  try {  ComThread.InitSTA();  app = new ActiveXComponent("Word.Application");  app.setProperty("Visible", false);  Dispatch docs = app.getProperty("Documents").toDispatch();  doc = Dispatch.invoke(docs, "Open", Dispatch.Method,  new Object[] { srcFilePath,   new Variant(false),   new Variant(true),//是否只读  new Variant(false),   new Variant("pwd") },  new int[1]).toDispatch();  // Dispatch.put(doc, "Compatibility", false);  //兼容性检查,为特定值false不正确  Dispatch.put(doc, "RemovePersonalInformation", false);  Dispatch.call(doc, "ExportAsFixedFormat", pdfFilePath,wdFormatPDF); // word保存为pdf格式宏,值为17return true; // set flag true;  }finally {  if (doc != null) {  Dispatch.call(doc, "Close", false);  }  if (app != null) {  app.invoke("Quit", 0);  }  ComThread.Release();  }  }protected static boolean ppt2pdf(String srcFilePath, String pdfFilePath) {  ActiveXComponent app = null;  Dispatch ppt = null;  try {  ComThread.InitSTA();  app = new ActiveXComponent("PowerPoint.Application");  Dispatch ppts = app.getProperty("Presentations").toDispatch();  // 因POWER.EXE的发布规则为同步,所以设置为同步发布  ppt = Dispatch.call(ppts, "Open", srcFilePath, true,// ReadOnly  true,// Untitled指定文件是否有标题  false// WithWindow指定文件是否可见  ).toDispatch();  Dispatch.call(ppt, "SaveAs", pdfFilePath, ppSaveAsPDF); //ppSaveAsPDF为特定值32    return true; // set flag true;  }finally {  if (ppt != null) {  Dispatch.call(ppt, "Close");  }  if (app != null) {  app.invoke("Quit");  }  ComThread.Release();  }} public static boolean excel2PDF(String inputFile,String pdfFile){ActiveXComponent app=null;Dispatch excel =null;try{ComThread.InitSTA();  app = new ActiveXComponent("Excel.Application");app.setProperty("Visible", false);Dispatch excels = app.getProperty("Workbooks").toDispatch();excel = Dispatch.call(excels,"Open",inputFile, false,true).toDispatch();Dispatch.call(excel,"ExportAsFixedFormat", xlTypePDF,pdfFile);return true;}finally{if (excel!= null) {Dispatch.call(excel, "Close");  }  if (app != null) {  app.invoke("Quit");  }ComThread.Release();  }            }

2.Wps 2015

核心代码如下:

public final static String WORDSERVER_STRING="KWPS.Application";public final static String PPTSERVER_STRING="KWPP.Application";public final static String EXECLSERVER_STRING="KET.Application";private static final int wdFormatPDF = 17;private static final int xlTypePDF = 0;private static final int ppSaveAsPDF = 32;/*** @return 操作成功与否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置错误; 如果返回 0,*         则表示操作成功; 返回1, 则表示转换失败*/@Overridepublic int officeToPdf(OfficeToPDFInfo officeToPDFInfo) {String sourceFile=officeToPDFInfo.sourceUrl;String destFile=officeToPDFInfo.destUrl;File inputFile = new File(sourceFile);if (!inputFile.exists()) {return -1;// 找不到源文件, 则返回-1}// 如果目标路径不存在, 则新建该路径File outputFile = new File(destFile);if (!outputFile.getParentFile().exists()) {outputFile.getParentFile().mkdirs();}String extentionName=FileUtils.getFileExtension(sourceFile);if(extentionName.equalsIgnoreCase("ppt")||extentionName.equalsIgnoreCase("pptx")||extentionName.equalsIgnoreCase("wpt")){ppt2pdf(officeToPDFInfo.sourceUrl,officeToPDFInfo.destUrl);}else if(extentionName.equalsIgnoreCase("doc")||extentionName.equalsIgnoreCase("docx")||extentionName.equalsIgnoreCase("wps")){doc2pdf(officeToPDFInfo.sourceUrl,officeToPDFInfo.destUrl);}else if(extentionName.equalsIgnoreCase("xls")||extentionName.equalsIgnoreCase("xlsx")||extentionName.equalsIgnoreCase("et")){excel2PDF(officeToPDFInfo.sourceUrl,officeToPDFInfo.destUrl);}    return 0;}protected static boolean doc2pdf(String srcFilePath, String pdfFilePath) {  ActiveXComponent pptActiveXComponent=null; ActiveXComponent workbook = null; try {ComThread.InitSTA();//初始化COM线程  pptActiveXComponent = new ActiveXComponent(WORDSERVER_STRING);//初始化exe程序  Variant[] openParams=new Variant[]{new Variant(srcFilePath),//filePathnew Variant(true),new Variant(true)//readOnley};workbook = pptActiveXComponent.invokeGetComponent("Documents").invokeGetComponent("Open",openParams);workbook.invoke("SaveAs",new Variant(pdfFilePath),new Variant(wdFormatPDF));                return true;}finally{if(workbook!=null){workbook.invoke("Close"); workbook.safeRelease(); }if(pptActiveXComponent!=null){       pptActiveXComponent.invoke("Quit"); pptActiveXComponent.safeRelease();}ComThread.Release();  }}protected static boolean ppt2pdf(String srcFilePath, String pdfFilePath) {ActiveXComponent pptActiveXComponent=null; ActiveXComponent workbook = null;  boolean readonly = true;try {ComThread.InitSTA();//初始化COM线程  pptActiveXComponent = new ActiveXComponent(PPTSERVER_STRING);//初始化exe程序  workbook = pptActiveXComponent.invokeGetComponent("Presentations").invokeGetComponent("Open",new Variant(srcFilePath),new Variant(readonly));workbook.invoke("SaveAs",new Variant(pdfFilePath),new Variant(ppSaveAsPDF));                return true;}finally{if(workbook!=null){workbook.invoke("Close"); workbook.safeRelease(); }if(pptActiveXComponent!=null){        pptActiveXComponent.invoke("Quit"); pptActiveXComponent.safeRelease();}ComThread.Release();  }} public static boolean excel2PDF(String srcFilePath,String pdfFilePath){ActiveXComponent et = null; Dispatch workbooks = null;  Dispatch workbook = null;  ComThread.InitSTA();//初始化COM线程  //ComThread.InitSTA(true);  try {  et = new ActiveXComponent(EXECLSERVER_STRING);//初始化et.exe程序  et.setProperty("Visible", new Variant(false));  workbooks = et.getProperty("Workbooks").toDispatch();  //workbook = Dispatch.call(workbooks, "Open", filename).toDispatch();//这一句也可以的  workbook = Dispatch.invoke(workbooks,"Open",Dispatch.Method,new Object[]{srcFilePath,0,true},new int[1]).toDispatch();   //Dispatch.invoke(workbook,"SaveAs",Dispatch.Method,new Object[]{pdfFilePath,xlTypePDF},new int[1]);Dispatch.call(workbook,"ExportAsFixedFormat",new Object[]{xlTypePDF,pdfFilePath});return true;}finally{if(workbook!=null){Dispatch.call(workbook,"Close");workbook.safeRelease(); }if(et!=null){         et.invoke("Quit"); et.safeRelease();}ComThread.Release();  }}

3.OpenOffice4.X

public int officeToPdf(OfficeToPDFInfo officeToPDFInfo) throws IOException {String sourceFile=officeToPDFInfo.sourceUrl;String destFile=officeToPDFInfo.destUrl;String OpenOffice_HOME=officeToPDFInfo.openOfficeHOME;File inputFile = new File(sourceFile);if (!inputFile.exists()) {return -1;// 找不到源文件, 则返回-1}// 如果目标路径不存在, 则新建该路径File outputFile = new File(destFile);if (!outputFile.getParentFile().exists()) {outputFile.getParentFile().mkdirs();}//= "D:\\Program Files\\OpenOffice.org 3";//这里是OpenOffice的安装目录, 在我的项目中,为了便于拓展接口,没有直接写成这个样子,但是这样是绝对没问题的// 如果从文件中读取的URL地址最后一个字符不是 '\',则添加'\'if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') {OpenOffice_HOME += "\\";}// 启动OpenOffice的服务String command = OpenOffice_HOME+ "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;StarOffice.ServiceManager\" -nofirststartwizard";Process pro = Runtime.getRuntime().exec(command);// connect to an OpenOffice.org instance running on port 8100OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);connection.connect();// convertDocumentConverter converter = new OpenOfficeDocumentConverter(connection);//DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection); converter.convert(inputFile, outputFile);// close the connectionconnection.disconnect();// 关闭OpenOffice服务的进程pro.destroy();return 0;}

(java office转pdf) MS Office2010、WPS2015、OpenOffice4用Java将Office文档转换为PDF,WIN7 64位系统相关推荐

  1. java openoffice 打印_java调用openoffice将office系列文档转换为PDF的示例方法

    前导: 发过程中经常会使用java将office系列文档转换为PDF, 一般都使用微软提供的openoffice+jodconverter 实现转换文档. openoffice既有windows版本也 ...

  2. 仿百度文库方案[openoffice.org 3+swftools+flexpaper](三) 之 使用JODConverter将office文档转换为pdf...

    第三步,使用JODConverter将office文档转换为pdf JODConverter是一个java的OpenDucument文件转换器,可以进行许多文件格式的转换,它利用 OpenOffice ...

  3. windows/linux服务器上java使用openoffice将word文档转换为PDF(亲测可用)

    一. 前言 1. 开发过程中经常会使用java将office系列文档转换为PDF, 一般都使用微软提供的openoffice+jodconverter 实现转换文档. 2. openoffice既有w ...

  4. Java 将 Word 文档转换为 PDF 的完美工具

    点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 今日推荐:为什么魂斗罗只有 128 KB却可以实现那么长的剧情?个人原创+1博客:点击前往,查看更多 来源:https:/ ...

  5. azw3转换为pdf_干货:如何Java 将 Word 文档转换为 PDF

    在日常工作中,PDF格式良好的视觉阅读性和稳定性使其使用越来越广泛.因此我们常会遇到需要将成型的Word文档转换为PDF格式的情况.本文就将通过使用Java程序来演示如何将Word文档转换成PDF格式 ...

  6. Java将Word文档转换为PDF的完美工具

    引用至:https://mp.weixin.qq.com/s/JIgo3f98HufGJx23mgtvag Java 将 Word 文档转换为 PDF 的完美工具 在日常工作中,PDF格式良好的视觉阅 ...

  7. C#实现office文档转换为PDF格式

    C#实现office文档转换为PDF格式 1.安装组件OfficeSaveAsPDFandXPS 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFand ...

  8. 【软件操作】Office将Word文档转换为PDF格式

    一.问题描述 PDF格式(Portable Document Format)相比于Word文档(.doc/.docx格式)体积更大,但具有更好的文档一致性(减少排版问题),修改更困难(不易被外界篡改) ...

  9. Word处理控件Aspose.Words功能演示:使用 Android 库将 Word 文档转换为 PDF

    Aspose.Words 是一种高级Word文档处理API,用于执行各种文档管理和操作任务.API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word.此 ...

  10. 如何将Microsoft Word文档转换为PDF

    PDFs are handy for distributing documents so that they're seen the same way by all parties. Typicall ...

最新文章

  1. 和12岁小同志搞创客开发:如何选择合适的控制器?
  2. 解惑:学.Net还是学Java?
  3. 疫情退票引爆的潘多拉盒子,境外旅游商家濒临倒闭
  4. 阿里工程师如何叫外卖?99%的人猜不到
  5. 12c oracle 激活_Oracle 12C 安装教程
  6. C语言 strlcpy函数实现
  7. python colorbar位置大小调整_python - matplotlib相邻子图:添加colorbar更改子图的大小 - 堆栈内存溢出...
  8. HiveQL学习笔记(一):Hive安装及Hadoop,Hive原理简介
  9. HDU1234 开门人和关门人(解法二)【废除!!!】
  10. Windows程序设计_学习总结(1)
  11. html 透明玻璃效果图,CSS3教程实现模糊透明玻璃效果
  12. android高德地图热力图,热力图-自有数据图层-示例中心-JS API 示例 | 高德地图API...
  13. Android基于在线地图的轨迹跟踪服务
  14. 乐优商城遇到的坑(四)之前台门户系统之Search.html
  15. QT全局钩子监控鼠标和键盘
  16. 图片不能置于底层怎么办_ps怎么把图片置于底层
  17. GRU和LSTM的单元结构
  18. css中字母数字自动换行
  19. 6、基带信号的发送和接收
  20. html图层标记,图层标记和合成标记

热门文章

  1. Komodo Edit快捷键
  2. 网络舆情公关必须把握的四项基本原则
  3. 基于微信小程序的多功能记事本软件开发 报告+前后台(java)项目源码+数据库
  4. 2400: Spoj 839 Optimal Marks
  5. 管道爬行机器人内部陀螺仪_油管内壁爬行机器人的机械结构设计
  6. 【软件测试】如何学好软件测试-第八章软件本地化测试篇
  7. 中国奇谭小猪妖、女C罗和领域驱动设计伪创新-罗大佑K歌集及溯源(二)(6-7)
  8. 获取GooglePlay下载来源广告数据统计接入同时analytics埋点
  9. 初步认识机器学习(Machine Leaning)
  10. org.hibernate.HibernateException: No CurrentSessionContext configured! at org.hibernate.internal.Se