有關頁面顯示pdf心得

先將自己的代碼貼上來!

<html>
  <head>
    
    <title>My JSP 'pdfTest.jsp' starting page</title>
    
       <script type="text/javascript">
      window.οnlοad=function(){
        document.all[document.getElementById("PDFNotKnown") ? "IfNoAcrobat" : "showdiv"].style.display = "block";
        //當能顯示pdf的時候,設置工具欄無用
        if(document.getElementById("showdiv").style.display=="block"){
            pdf.SetShowToolbar(false);
            //document.getElementById("pdf").EnableCopy=false;
            //document.getElementById("pdf").EnablePrint=false;
        }
      }
      
    
    
  /*   // 禁用右鍵功能
    function stop(){
     return false;
     }
    document.οncοntextmenu=stop; */
    
    //禁止F8按鈕
    function keypressed() {
        if(event.keyCode == 119) {
           event.keyCode = 0;
           return false;
        }
    }
   </script>
  </head>
  
  <body>
   
<div class="right" id="abc" style="margin-top:-20px;">
 <div class="cont-boxwrap2" id="c1">
    <div class="con2" id="c2">   
       <h2 class="title"><span>意外險查詢</span></h2>
 <div class="boxwrap2" style="width:660px;overflow:auto;" id="cb2">
 <div class="text-tabel1" style="overflow:auto;">
                <fieldset class="auto_height" style="border:1px solid #ccc; background-color:#FFFFFF;">
                <legend  class="theme-color f16">條款預覽</legend>
                <div style="overflow:hidden;height:660px;">
                        <DIV id="IfNoAcrobat" >
                              <a href="http://get.adobe.com/cn/reader/">你需要先安裝Adobe Reader才能正常瀏覽文件,請點擊這裏下載Adobe Reader.</a>   
                        </DIV>
                        <OBJECT type="application/pdf" width=0 height=0 style="display:none">
                           <DIV id="PDFNotKnown" > </DIV>
                        </OBJECT>
                        <DIV id="showdiv" style="HEIGHT: 10px; margin-top: 10px;">  
                            <object classid="clsid:CA8A9780-280D-11CF-A24D-444553540000"   width="100%" height="660" border="0" top="-10" name="pdf" id="pdf" >   
                            <param name="toolbar" value="false">  
                            <param name="pagemode" value="none">
                            <param name="_Version" value="65539">  
                              
                            <param name="_ExtentX" value="20108">  
                              
                            <param name="_ExtentY" value="10866">  
                              
                            <param name="_StockProps" value="0">  
                        
                            <param name="SRC" value="../jpg.pdf">
                            
                            <embed src="../jpg.pdf"   width="100%" height="660">
                            </object>
                        </DIV>  
                </div>
                </fieldset>
            </div>
             <div class="btn-area" style="margin-top:10px;width:200px; margin-right:30%!important;margin-right:17%">
                   <div class="btn-themecolor" ><a href="###" οnclick=" javascript:window.history.go(-1);">返回</a></div>
            </div>
          </div>
          
  </div>
  </div>
  </div>
  </body>
</html>
這裏要考慮到多瀏覽器兼容的問題,需要用到object和embed標籤。


Object、param標籤及頁面顯示PDF文件的方法:http://blog.csdn.net/tanguang_honesty/article/details/8530731

兼容火狐ie 在html中給.swf傳遞參數(轉):http://blog.sina.com.cn/s/blog_6283c54a0100i2b0.html

在html上面顯示pdf代碼(禁用工具欄):http://blog.csdn.net/tanguang_honesty/article/details/8530500

html標籤之Object標籤詳解:http://blog.csdn.net/soliy/article/details/5404183

另外一種方法是直接在jsp頁面上輸出:

代碼如下。

    out.clear();
    out = pageContext.pushBody();
    response.setContentType("application/pdf");
	String strPdfPath = "D://jpg1.pdf";
    try {
       
        //判斷該路徑下的文件是否存在
        File file = new File(strPdfPath);
        if (file.exists()) {
            DataOutputStream temps = new DataOutputStream(response
                    .getOutputStream());
            DataInputStream in = new DataInputStream(
                    new FileInputStream(strPdfPath));

            byte[] b = new byte[2048];
            while ((in.read(b)) != -1) {
                temps.write(b);
                temps.flush();
            }

            in.close();
            temps.close();
        } else {
            out.print(strPdfPath + " 文件不存在!");
        }

    } catch (Exception e) {
        out.println(e.getMessage());
    }





發佈了25 篇原創文章 · 獲贊 4 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章