java 仿百度文库源码_java开发_模仿百度文库_OpenOffice2PDF_源码下载

这几天在研究模仿着做类似于百度文库的东西,在这里给大家分享一下我自己做的东西。

由于需要做这样的项目,我查阅了很多资料,最后选定一下方案去做:

Txt/Word/Excel/PPT=>PDF(OpenOffice+JodConverter)=>SWF(pdf2swf)=>FlexPaper浏览

今天就完成第一步:

Txt/Word/Excel/PPT=>PDF(OpenOffice+JodConverter)

做之前,我们要先做一些准备:

1.下载:Apache_OpenOffice_incubating_3.4.1_Win_x86_install_zh-CN.exe

94c7f4848beb5e8458cb6d1dff43347b.png

下载后得到:Apache_OpenOffice_incubating_3.4.1_Win_x86_install_zh-CN.exe

2.安装Apache_OpenOffice

双击Apache_OpenOffice_incubating_3.4.1_Win_x86_install_zh-CN.exe进行安装操作

3258418bc455696f102ee6d12e108c4b.png

注意:这里的安装位置,要在项目中用到....我安装在:C:/Program Files (x86)/OpenOffice.org 3目录下面

cdfce6169ba722cd74b7b351c1926c83.png

eedb72d1e81ebcdf9de57007a416eb69.png

e05fba225561e064caa4b6374299b6e4.png

254bcaef8034d920c262af1cb62e10dc.png

c79ea7139448abc8691f1dbf1ae19661.png

ede7fbce74143565403f4793b8df9648.png

921cff32a8848d0cde67827c21905ebf.png

到这里,OpenOffice就算是安装完成了。

3.创建一个项目

3b0d46325c3dd12e9826eb51d73574ac.png

/Office2PDF/src/com/b510/office2pdf/Office2PDF.java

1 /**

2 *3 */

4 packagecom.b510.office2pdf;5

6 importjava.io.File;7 importjava.util.Date;8 importjava.util.regex.Pattern;9

10 importorg.artofsolving.jodconverter.OfficeDocumentConverter;11 importorg.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;12 importorg.artofsolving.jodconverter.office.OfficeManager;13

14 /**

15 * 这是一个工具类,主要是为了使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx)16 * 转化为pdf文件
17 * Office2010的没测试
18 *19 * @date 2012-11-520 *@authorxhw21 *22 */

23 public classOffice2PDF {24

25 /**

26 * office中.doc格式27 */

28 public static final String OFFICE_DOC = "doc";29 /**

30 * office中.docx格式31 */

32 public static final String OFFICE_DOCX = "docx";33 /**

34 * office中.xls格式35 */

36 public static final String OFFICE_XLS = "xls";37 /**

38 * office中.xlsx格式39 */

40 public static final String OFFICE_XLSX = "xlsx";41 /**

42 * office中.ppt格式43 */

44 public static final String OFFICE_PPT = "ppt";45 /**

46 * office中.pptx格式47 */

48 public static final String OFFICE_PPTX = "pptx";49 /**

50 * pdf格式51 */

52 public static final String OFFICE_TO_PDF = "pdf";53

54 public static voidmain(String[] args) {55 Office2PDF office2pdf = newOffice2PDF();56 office2pdf.openOfficeToPDF("e:/test." + OFFICE_DOCX, "e:/test_" + new Date().getTime() + "." +OFFICE_TO_PDF);57 office2pdf.openOfficeToPDF("e:/test." + OFFICE_PPTX, null);58 }59

60 /**

61 * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件
62 *63 *@paraminputFilePath64 * 源文件路径,如:"e:/test.docx"65 *@paramoutputFilePath66 * 目标文件路径,如:"e:/test_docx.pdf"67 *@return

68 */

69 public booleanopenOfficeToPDF(String inputFilePath, String outputFilePath) {70 returnoffice2pdf(inputFilePath, outputFilePath);71 }72

73 /**

74 * 根据操作系统的名称,获取OpenOffice.org 3的安装目录
75 * 如我的OpenOffice.org 3安装在:C:/Program Files (x86)/OpenOffice.org 3
76 *77 *@returnOpenOffice.org 3的安装目录78 */

79 publicString getOfficeHome() {80 String osName = System.getProperty("os.name");81 if (Pattern.matches("Linux.*", osName)) {82 return "/opt/openoffice.org3";83 } else if (Pattern.matches("Windows.*", osName)) {84 return "C:/Program Files (x86)/OpenOffice.org 3";85 } else if (Pattern.matches("Mac.*", osName)) {86 return "/Application/OpenOffice.org.app/Contents";87 }88 return null;89 }90

91 /**

92 * 连接OpenOffice.org 并且启动OpenOffice.org93 *94 *@return

95 */

96 publicOfficeManager getOfficeManager() {97 DefaultOfficeManagerConfiguration config = newDefaultOfficeManagerConfiguration();98 //获取OpenOffice.org 3的安装目录

99 String officeHome =getOfficeHome();100 config.setOfficeHome(officeHome);101 //启动OpenOffice的服务

102 OfficeManager officeManager =config.buildOfficeManager();103 officeManager.start();104 returnofficeManager;105 }106

107 /**

108 * 转换文件109 *110 *@paraminputFile111 *@paramoutputFilePath_end112 *@paraminputFilePath113 *@paramoutputFilePath114 *@paramconverter115 */

116 public voidconverterFile(File inputFile, String outputFilePath_end, String inputFilePath, String outputFilePath, OfficeDocumentConverter converter) {117 File outputFile = newFile(outputFilePath_end);118 //假如目标路径不存在,则新建该路径

119 if (!outputFile.getParentFile().exists()) {120 outputFile.getParentFile().mkdirs();121 }122 converter.convert(inputFile, outputFile);123 System.out.println("文件:" + inputFilePath + "\n转换为\n目标文件:" + outputFile + "\n成功!");124 }125

126 /**

127 * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件
128 *129 *@paraminputFilePath130 * 源文件路径,如:"e:/test.docx"131 *@paramoutputFilePath132 * 目标文件路径,如:"e:/test_docx.pdf"133 *@return

134 */

135 public booleanoffice2pdf(String inputFilePath, String outputFilePath) {136 boolean flag = false;137 OfficeManager officeManager =getOfficeManager();138 //连接OpenOffice

139 OfficeDocumentConverter converter = newOfficeDocumentConverter(officeManager);140 long begin_time = newDate().getTime();141 if (null !=inputFilePath) {142 File inputFile = newFile(inputFilePath);143 //判断目标文件路径是否为空

144 if (null ==outputFilePath) {145 //转换后的文件路径

146 String outputFilePath_end =getOutputFilePath(inputFilePath);147 if (inputFile.exists()) {//找不到源文件, 则返回

148 converterFile(inputFile, outputFilePath_end, inputFilePath, outputFilePath, converter);149 flag = true;150 }151 } else{152 if (inputFile.exists()) {//找不到源文件, 则返回

153 converterFile(inputFile, outputFilePath, inputFilePath, outputFilePath, converter);154 flag = true;155 }156 }157 officeManager.stop();158 } else{159 System.out.println("con't find the resource");160 }161 long end_time = newDate().getTime();162 System.out.println("文件转换耗时:[" + (end_time - begin_time) + "]ms");163 returnflag;164 }165

166 /**

167 * 获取输出文件168 *169 *@paraminputFilePath170 *@return

171 */

172 publicString getOutputFilePath(String inputFilePath) {173 String outputFilePath = inputFilePath.replaceAll("." + getPostfix(inputFilePath), ".pdf");174 returnoutputFilePath;175 }176

177 /**

178 * 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx"
179 *180 *@paraminputFilePath181 *@return

182 */

183 publicString getPostfix(String inputFilePath) {184 return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1);185 }186

187 }

4.效果

860977742d656222dee3ad4e18ebd164.png

5.控制台效果

1 十一月 05, 2012 5:19:15 下午 org.artofsolving.jodconverter.office.ProcessPoolOfficeManager

2 INFO: ProcessManager implementation is PureJavaProcessManager3 十一月 05, 2012 5:19:15下午 org.artofsolving.jodconverter.office.OfficeProcess start4 INFO: starting process with acceptString 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1' and profileDir 'C:\Users\ADMINI~1\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-2002'

5 十一月 05, 2012 5:19:15下午 org.artofsolving.jodconverter.office.OfficeProcess start6 INFO: started process7 十一月 05, 2012 5:19:16下午 org.artofsolving.jodconverter.office.OfficeConnection connect8 INFO: connected: 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1'

9 十一月 05, 2012 5:19:19下午 org.artofsolving.jodconverter.office.ProcessPoolOfficeManager stop10 INFO: stopping11 文件:e:/test.docx12 转换为13 目标文件:e:\test_1352107155307.pdf14 成功!15 十一月 05, 2012 5:19:19 下午 org.artofsolving.jodconverter.office.OfficeConnection$1disposing16 INFO: disconnected: 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1'

17 十一月 05, 2012 5:19:19下午 org.artofsolving.jodconverter.office.ManagedOfficeProcess doEnsureProcessExited18 INFO: process exited with code 0

19 十一月 05, 2012 5:19:19下午 org.artofsolving.jodconverter.office.ProcessPoolOfficeManager stop20 INFO: stopped21 文件转换耗时:[2838]ms22 十一月 05, 2012 5:19:19 下午 org.artofsolving.jodconverter.office.ProcessPoolOfficeManager

23 INFO: ProcessManager implementation is PureJavaProcessManager24 十一月 05, 2012 5:19:19下午 org.artofsolving.jodconverter.office.OfficeProcess start25 INFO: starting process with acceptString 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1' and profileDir 'C:\Users\ADMINI~1\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-2002'

26 十一月 05, 2012 5:19:19下午 org.artofsolving.jodconverter.office.OfficeProcess start27 INFO: started process28 十一月 05, 2012 5:19:20下午 org.artofsolving.jodconverter.office.OfficeConnection connect29 INFO: connected: 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1'

30 文件:e:/test.pptx31 转换为32 目标文件:e:\test.pdf33 成功!34 十一月 05, 2012 5:19:26下午 org.artofsolving.jodconverter.office.ProcessPoolOfficeManager stop35 INFO: stopping36 十一月 05, 2012 5:19:26 下午 org.artofsolving.jodconverter.office.OfficeConnection$1disposing37 INFO: disconnected: 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1'

38 十一月 05, 2012 5:19:26下午 org.artofsolving.jodconverter.office.ManagedOfficeProcess doEnsureProcessExited39 INFO: process exited with code 0

40 十一月 05, 2012 5:19:26下午 org.artofsolving.jodconverter.office.ProcessPoolOfficeManager stop41 INFO: stopped42 文件转换耗时:[6417]ms

是不是很简单....

希望大家多多交流,一起进步...

========================================================

More reading,and english is important.

I'm Hongten

大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。

Hongten博客排名在100名以内。粉丝过千。

Hongten出品,必是精品。

E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

========================================================

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值