本文在原文的基础上有所修改,原文请参考:

http://titanseason.iteye.com/blog/1471606 由于此blog不支持附件附件请到此处下载

http://my.oschina.NET/bigyuan/blog/165464

1. 需要用的软件

    OpenOffice 下载地址http://www.openoffice.org/

    JodConverter 下载地址http://sourceforge.net/projects/jodconverter/files/JODConverter/,也可以直接从附件里面下载

  

2.启动OpenOffice的服务

 

安装完openoffice,安装服务

cd C:\Program Files (x86)\OpenOffice 4\program

执行

soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard

查看是否安装成功,查看端口对应的pid

    netstat -ano|findstr “8100”

 查看pid对应的服务程序名

    tasklist|findstr “pid值”

 

 

3.将JodConverter相关的jar包添加到项目中   

  

4. 下面是实现代码

附件里面有现成的可以用的项目示例,直接导入eclipse就可以运行

 

  1. 1 本文在原文的基础上有所修改,原文请参考:
  2. 2
  3. 3 http://titanseason.iteye.com/blog/1471606 由于此blog不支持附件附件请到此处下载
  4. 4 http://my.oschina.NET/bigyuan/blog/165464
  5. 5 1. 需要用的软件
  6. 6
  7. 7 OpenOffice 下载地址http://www.openoffice.org/
  8. 8
  9. 9 JodConverter 下载地址http://sourceforge.net/projects/jodconverter/files/JODConverter/,也可以直接从附件里面下载
  10. 10
  11. 11
  12. 12 2.启动OpenOffice的服务
  13. 13
  14. 14
  15. 15 安装完openoffice,安装服务
  16. 16
  17. 17 cd C:\Program Files (x86)\OpenOffice 4\program
  18. 18
  19. 19 执行
  20. 20
  21. 21 soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
  22. 22
  23. 23 查看是否安装成功,查看端口对应的pid
  24. 24
  25. 25 netstat -ano|findstr "8100"
  26. 26
  27. 27 查看pid对应的服务程序名
  28. 28
  29. 29 tasklist|findstr "pid值"
  30. 30
  31. 31
  32. 32 3.JodConverter相关的jar包添加到项目中
  33. 33
  34. 34 4. 下面是实现代码
  35. 35 附件里面有现成的可以用的项目示例,直接导入eclipse就可以运行
  36. 36
  37. 37 [java] view plain copy
  38. 38 /**
  39. 39 * 将Office文档转换为PDF. 运行该函数需要用到OpenOffice, OpenOffice下载地址为
  40. 40 * http://www.openoffice.org/
  41. 41 *
  42. 42 * <pre>
  43. 43 * 方法示例:
  44. 44 * String sourcePath = "F:\\office\\source.doc";
  45. 45 * String destFile = "F:\\pdf\\dest.pdf";
  46. 46 * Converter.office2PDF(sourcePath, destFile);
  47. 47 * </pre>
  48. 48 *
  49. 49 * @param sourceFile
  50. 50 * 源文件, 绝对路径. 可以是Office2003-2007全部格式的文档, Office2010的没测试. 包括.doc,
  51. 51 * .docx, .xls, .xlsx, .ppt, .pptx等. 示例: F:\\office\\source.doc
  52. 52 * @param destFile
  53. 53 * 目标文件. 绝对路径. 示例: F:\\pdf\\dest.pdf
  54. 54 * @return 操作成功与否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置错误; 如果返回 0,
  55. 55 * 则表示操作成功; 返回1, 则表示转换失败
  56. 56 */
  57. 57 public static int office2PDF(String sourceFile, String destFile) {
  58. 58 try {
  59. 59 File inputFile = new File(sourceFile);
  60. 60 if (!inputFile.exists()) {
  61. 61 return -1;// 找不到源文件, 则返回-1
  62. 62 }
  63. 63
  64. 64 // 如果目标路径不存在, 则新建该路径
  65. 65 File outputFile = new File(destFile);
  66. 66 if (!outputFile.getParentFile().exists()) {
  67. 67 outputFile.getParentFile().mkdirs();
  68. 68 }
  69. 69
  70. 70 // connect to an OpenOffice.org instance running on port 8100
  71. 71 OpenOfficeConnection connection = new SocketOpenOfficeConnection(
  72. 72 "127.0.0.1", 8100);
  73. 73 connection.connect();
  74. 74
  75. 75 // convert
  76. 76 DocumentConverter converter = new OpenOfficeDocumentConverter(
  77. 77 connection);
  78. 78 converter.convert(inputFile, outputFile);
  79. 79
  80. 80 // close the connection
  81. 81 connection.disconnect();
  82. 82
  83. 83 return 0;
  84. 84 } catch (FileNotFoundException e) {
  85. 85 e.printStackTrace();
  86. 86 return -1;
  87. 87 } catch (ConnectException e) {
  88. 88 e.printStackTrace();
  89. 89 } catch (IOException e) {
  90. 90 e.printStackTrace();
  91. 91 }
  92. 92
  93. 93 return 1;
  94. 94 }

    http://www.xwcms.net/xwcms/download.jsp?fileAnnexId=c96201c7-f42d-4e28-8560-a389365ba906&articleContentId=20140629170446171

 

版权声明:本文为cxxjohnson原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/cxxjohnson/p/6880545.html