把office文档转化为pdf并在线显示

 

1.下载jodconverter-2.2.2,使用这个里面lib下面的所有jar包。这些jar包放在WEB-INF的lib文件夹里面。

 

2.下载"Openoffice3.3或3.3",并进行安装。OpenOffice的下载地址是"http://sourceforge.net/projects/jodconverter/files/"

3、以cmd方式启动openoffice server

cd opeonofiice的安装路径/program
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
4.安装Adobe Reader或其他pdf阅读器

5.下面进行代码的编写。

JOD4DocToPDF这个类用来把office文档转换成pdf.需要指出的是里面的“/”最好写成File.separator,这样程序的可移植性就比较好了

 

package bao1;
import java.io.File;  
import java.net.ConnectException;  
import java.util.Date;  
 
import com.artofsolving.jodconverter.DocumentConverter;  
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;  
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;  
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;  
 

public class JOD4DocToPDF extends java.lang.Thread {  
    private File inputFile;// 需要转换的文件  
    private File outputFile;// 输出的文件  
 
    public JOD4DocToPDF(File inputFile, File outputFile) {  
        this.inputFile = inputFile;  
        this.outputFile = outputFile; 
       
    }  
 
    public void docToPdf() {  
        Date start = new Date();  
      
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);  
        try {  
            connection.connect();  
 
           
            DocumentConverter converter = new OpenOfficeDocumentConverter(  
                    connection);  
            converter.convert(inputFile, outputFile);  
        } catch (ConnectException cex) {  
            cex.printStackTrace();  
        } finally {  
            // close the connection  
            if (connection != null) {  
                connection.disconnect();  
                connection = null;  
            }  
        }   
          }  
 
    /** 
     * 由于服务是线程不安全的,所以……需要启动线程 
     */ 
    public void run() {  
        this.docToPdf();  
 
    }  
 
    public File getInputFile() {  
        return inputFile;  
    }  
 
    public void setInputFile(File inputFile) {  
        this.inputFile = inputFile;  
    }  
 
    public File getOutputFile() {  
        return outputFile;  
    }  
 
    public void setOutputFile(File outputFile) {  
        this.outputFile = outputFile;  
    }   
  
     

下面开始编写jsp来遍历站点根目录下的“wendang”文件夹,这里也使用了一些AJAX。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>遍历文件</title>
<script language="javascript">
  var xmlHttp ;
  var flag = false ;
  function createXMLHttp(){
   if(window.XMLHttpRequest){
    xmlHttp = new XMLHttpRequest() ;
   } else {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") ;
   }
  }
  function checkUserid(mc){
   createXMLHttp() ;


   xmlHttp.open("POST","chuli.jsp?mc="+mc) ;


   xmlHttp.onreadystatechange = checkUseridCallback ;
   xmlHttp.send(null) ;
   document.getElementById("msg").innerHTML ="<img src='images/loading3.gif'/>" ;
     }
  function checkUseridCallback(){
   if(xmlHttp.readyState == 4){
    if(xmlHttp.status == 200){
     var text = xmlHttp.responseText ;     
     document.getElementById("msg").innerHTML = text ;     
    }
   }
  }
</script>
<style type="text/css">
#msg
{
 margin-left:40px;

}
</style>
</head>
<body>
    <center>
      <div id="msg2"></div>
    </center>
 
 <div id="msg">
  <%
 
   java.io.File f=new java.io.File(this.getServletContext().getRealPath("/")+"wendang");
  
   String f2[]=f.list();
   for(int i=0;i<f2.length;i++)
   {
   %>
   
       <li><a href="#" onclick="checkUserid('<%=f2[i]%>')"><%=f2[i]%></a></li>
   <%
   }
   %>
 
   </div>
</body>
</html>

这个jsp负责把office文档转换成pdf,为了简单起见这里只生成一个名为“123.pdf”的临时文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.net.URLDecoder" %>
<%@ page import="bao1.JOD4DocToPDF" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>显示文档</title>

</head>
<body>
 <%
  request.setCharacterEncoding("UTF-8");
  //String s=URLDecoder.decode(request.getParameter("mc"),"ISO-8859-1");
  String s=new String(request.getParameter("mc").getBytes("ISO8859-1"),"GB2312");
 
  String path=this.getServletContext().getRealPath("/")+"wendang"+java.io.File.separator;
  String path2=this.getServletContext().getRealPath("/")+"pdf"+java.io.File.separator;
 
  JOD4DocToPDF zh=new JOD4DocToPDF(new java.io.File(path+s),new java.io.File(path2+"123.pdf"));
 
  zh.start();
 %>
 
<%
  Thread.sleep(4000);   //因为把office文档转换成pdf需要大概4秒时间,这个时间不是用来显示而是用来替换原来的pdf.之后显示才不会出错
%>
 

 <center>
  
  <object data="pdf/123.pdf" type="application/pdf" width="1000" height="700"></object>
  </center>
</body>
</html>

到这里代码就写完了,运行的效果图如图所示。如果需要我的这个项目。你们可以去csdn上下载。如果你在操作中出现了问题,可以和我联系。我的QQ是“1214273312”,加我为好友时请注明“csdn网友”。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值