1、将office首先要安装OpenOfice,傻瓜式安装就好了,之后可以使用下列代码将word转为pdf。这个需要导入jodconverter-2.2.2里的 ja r包
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.net.ConnectException;
  5. import org.junit.Test;
  6. import com.artofsolving.jodconverter.DocumentConverter;
  7. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
  8. import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
  9. import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
  10. publicclassOfficeChangeToPDF{
  11. /**
  12. * 将Office文档转换为PDF. 运行该函数需要用到OpenOffice, OpenOffice下载地址为
  13. * http://www.openoffice.org/
  14. *
  15. * <pre>
  16. *
  17. * 方法示例:
  18. * String sourcePath = "F:\\office\\source.doc";
  19. * String destFile = "F:\\pdf\\dest.pdf";
  20. * Converter.office2PDF(sourcePath, destFile);
  21. * </pre>
  22. *
  23. * @param sourceFile
  24. * 源文件, 绝对路径. 可以是Office2003-2007全部格式的文档, Office2010的没测试. 包括.doc,
  25. * .docx, .xls, .xlsx, .ppt, .pptx等. 示例: F:\\office\\source.doc
  26. * @param destFile
  27. * 目标文件. 绝对路径. 示例: F:\\pdf\\dest.pdf
  28. * @return 操作成功与否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置错误; 如果返回 0,
  29. * 则表示操作成功; 返回1, 则表示转换失败
  30. */
  31. publicint office2PDF(String sourceFile,String destFile){
  32. try{
  33. File inputFile =newFile(sourceFile);
  34. if(!inputFile.exists()){
  35. return-1;// 找不到源文件, 则返回-1
  36. }
  37. // 如果目标路径不存在, 则新建该路径
  38. File outputFile =newFile(destFile);
  39. if(!outputFile.getParentFile().exists()){
  40. outputFile.getParentFile().mkdirs();
  41. }
  42. StringOpenOffice_HOME="C:\\Program Files (x86)\\OpenOffice 4";// 这里是OpenOffice的安装目录,
  43. // 在我的项目中,为了便于拓展接口,没有直接写成这个样子,但是这样是绝对没问题的
  44. // 如果从文件中读取的URL地址最后一个字符不是 '\',则添加'\'
  45. if(OpenOffice_HOME.charAt(OpenOffice_HOME.length()-1)!='\\'){
  46. OpenOffice_HOME+="\\";
  47. }
  48. // 启动OpenOffice的服务,也可以一直开启,那就不需要运行一次开启一次,提高效率
  49. String command =OpenOffice_HOME
  50. +"program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
  51. Process pro =Runtime.getRuntime().exec(command);
  52. // connect to an OpenOffice.org instance running on port 8100
  53. OpenOfficeConnection connection =newSocketOpenOfficeConnection("127.0.0.1",8100);
  54. connection.connect();
  55. // convert
  56. DocumentConverter converter =newOpenOfficeDocumentConverter(connection);
  57. converter.convert(inputFile, outputFile);
  58. // close the connection
  59. connection.disconnect();
  60. // 关闭OpenOffice服务的进程
  61. pro.destroy();
  62. return0;
  63. }catch(FileNotFoundException e){
  64. e.printStackTrace();
  65. return-1;
  66. }catch(ConnectException e){
  67. e.printStackTrace();
  68. }catch(IOException e){
  69. e.printStackTrace();
  70. }
  71. return1;
  72. }
  73. @Test
  74. publicvoid toPDF(){
  75. int office2pdf = office2PDF("C:\\Users\\HP\\Desktop\\xx.doc","C:\\Users\\HP\\Desktop\\xx.PDF");
  76. System.out.println(office2pdf);
  77. }
  78. }
 
 

2、要实现在web端预览pdf需要pdfobject插件,然后使用如下代码

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>网页嵌入pdf浏览器</title>
  5. <scripttype="text/javascript"src="./js/pdfobject.min.js"></script>
  6. <styletype="text/css">
  7. .pdfobject-container { height:600px;}
  8. .pdfobject { border:1px solid #666; }
  9. </style>
  10. </head>
  11. <body>
  12. <divid="example1"></div>
  13. <scripttype="text/javascript">
  14. PDFObject.embed("./pdf/ELM-Chinese-Brief.pdf","#example1");
  15. </script>
  16. </body>
  17. </html>
 

附件列表

OpenOfice将offic转为pdf并且在web显示的更多相关文章

  1. 把上传过来的多张图片拼接转为PDF的实现代码

    以下是把上传过来的多张图片拼接转为PDF的实现代码,不在本地存储上传上来的图片,下面是2中做法,推荐第一种,把pdf直接存储到DB中比较安全. 如果需要在服务器上存储客户端上传的文件时,切记存储文件时 ...

  2. 将doc文件批量转为pdf文件

    需要将不少doc文件转为pdf,WPS带有这种功能,但是鼠标点击次数太多以后整个人都变得很烦躁 用了一下午去搜这方面的工具软件,找到若干.有一些免费,有一些试用的,但总归就找到一个真正能用,虽说生成的 ...

  3. [置顶] 读取pdf并且在web页面中显示

    读取pdf并且在web页面中显示 if (System.IO.File.Exists(f)) { Response.ContentType = "applicationpdf"; ...

  4. 使用OpenOffice.org将各类文档转为PDF

    http://blog.zhaojie.me/2010/05/convert-document-to-pdf-via-openoffice.html ————————————————————————— ...

  5. java转pdf(html转为pdf),解决中文乱码,标签不规范等问题

    第一步,下载jar包以及建对应的文件夹.注意pd4ml的jar要选择pro版本.然后建一个pd4fonts.properties 里面对应的字体. SimSun = simsun.ttf 前面为变量名 ...

  6. [办公自动化]如何将PPT转为PDF,免费

    同事需要把PPT格式的文档转为PDF.她没有安装adobe acrobat,安装了微软office 2007. 这个其实可以通过安装微软官方插件来解决.无需额外费用. 所需软件为: 2007 Micr ...

  7. Java 将Word转为PDF、PNG、SVG、RTF、XPS、TXT、XML

    同一文档在不同的编译或阅读环境中,需要使用特定的文档格式来打开,通常需要通过转换文档格式的方式来实现.下面将介绍在Java程序中如何来转换Word文档为其他几种常见文档格式,如PDF.图片png.sv ...

  8. Java 基于Spire.Cloud.Excel 将Excel转为PDF

    Spire.Cloud.Excel Sdk 提供GeneralApi接口和WorkbookApi接口,支持将本地Excel和云端Excel文档转换为ODS, PDF, XPS, PCL, PS等格式. ...

  9. Java 将Excel转为PDF

    本文将介绍在Java程序中如何将Excel工作簿转为PDF文档的,包括: 将整个工作簿转为PDF 将指定工作表转为PDF   使用工具:Free Spire.XLS for Java (免费版) Ja ...

随机推荐

  1. Mongodb基础用法及查询操作[转载]

    插入多条测试数据> for(i=1;i<=1000;i++){... db.blog.insert({"title":i,"content":&qu ...

  2. jquery元素是否可见(隐藏)

    var temp1=$(".view_hidden").is(":visible");//是否可见 var temp2=$(".elem_id&quo ...

  3. [leetcode-485-Max Consecutive Ones]

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

  4. easyui框架--基础篇(一)-->数据表格datagrid(php与mysql交互)

      前  言  php  easyui框架--本篇学习主要是 easyui中的datagrid(数据表格)框架. 本篇学习主要通过讲解一段代码加GIF图片学习datagrid(数据表格)中的一些常用属 ...

  5. Linux用户管理-中

    添加用户组命令groupadd 提示:groupadd命令的使用非常简单,但在生产环境中使用的不多,因此,会简单应用即可. 与groupadd命令有关的文件有:/etc/group :用户组相关文件/ ...

  6. Spring Security -SpEL表达式

    Spring Security -SpEL表达式 开启SpEL表达式 <!-- use-expressions是否开启 SpEL表达式 o.s.s.web.access.expression.W ...

  7. 高级Java程序员的技术进阶之路

      据不完全统计,截至目前(2017.07)为止,中国Java程序员的数量已经超过了100万.而且,随着IT培训业的持续发展和大量的应届毕业生进入社会,Java程序员面临的竞争压力越来越大.那么,作为 ...

  8. spring简单笔记

    1.普通注入 1)构造器注入 <constructor-arg name="id" value="100" /> #直接赋值 <constru ...

  9. Tomcat管理页面配置

    详情参考:http://www.365mini.com/page/tomcat-manager-user-configuration.htm 修改$CATALINA_BASE/conf/tomcat- ...

  10. OpenCV3.2 + VS2015环境配置

    一.准备工作: (1)   到OpenCV的官网(http://opencv.org/)下载OpenCV3.2 (2)   安装好VS2015. (3)   计算机系统:Win7(Win8, Win1 ...