OpenOfice将offic转为pdf并且在web显示

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>
 





附件列表

 

转载于:https://www.cnblogs.com/caizheng/p/6957380.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值