JSP页面打印方法

时间:2008-08-19 08:11:27  类别:JSP  作者:因为有你

JSP页面打印方法

方法一:
function PrintTable(Id){
                var mStr;
                mStr = window.document.body.innerHTML ;
                var mWindow = window;                
                window.document.body.innerHTML =Id.innerHTML;
                mWindow.print();
                window.document.body.innerHTML = mStr;
        }

在页面中要打印
<div id="dy">
.......
</div>
<input type="button" value="打    印" onclick="return PrintTable(dy)">

方法二:

with (document)
{//输出样式表表及IE打印控件
        write ("<style type=\"text/css\" media=\"print\">");
        write ("   .noPrint{visibility:hidden}");
        write ("</style>");
        write ("<OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WB width=0></object>");
}
function doPrintSetup()
{//打印设置
        WB.ExecWB(8,1);
}
function doPrintPreview()
{//打印预览
        WB.ExecWB(7,1);
}
function doPrint()
{
        window.print();
}
function showPrintBar()
{
        with (document)
        {
                write ("<div align=\"center\" class=\"noprint\">");
                write ("   <input type=\"button\" name=\"doBack\" value=\" <<返回   \" onClick=\"history.go(-1)\">");
                write ("   <input type=\"button\" name=\"doPrintPreview\" onClick=\"WB.ExecWB(8,1)\" value=\"打印设置\">");
                write ("   <input type=\"button\" name=\"doPrint\" value=\"   打印>> \" onClick=\"doPrint()\">");
                write ("</div>")
        }
}

----------------print.js end------------

页面中使用时:
<script language="JavaScript" type="text/JavaScript" src="print.js"></script>
然后再需要输出打印按钮时:
<script>showPrintBar()</script>

方法三:
打印前把按钮隐藏
<tr><td><input type="button" value="打 印" name="butt" onClick="javascript:hide()"></td></tr>

<script language="javascript">
                function hide()
                {
                   document.all.item("butt").style.display="none";
                   
                   window.print();                                
                  }
</script>

方法四:
<style>
@media print{
.noprint{display:none}
}
</style>
给不打印的区加这个CSS就可以了,在页面上能看见但打印的时候看不到!

方法五:
利用脚本控制打印,可以采用如下方法:
1、execCommand()方法
Print 打开打印对话框以便用户可以打印当前页
即document.execCommand('Print');

2、window.print()

3、   <OBJECT   id=WebBrowser   classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2   height=0   width=0>
  </OBJECT>
  <input   type=button   value=打印           onclick=document.all.WebBrowser.ExecWB(6,1)>
  <input   type=button   value=直接打印   onclick=document.all.WebBrowser.ExecWB(6,6)>
  <input   type=button   value=页面设置   onclick=document.all.WebBrowser.ExecWB(8,1)>
  <input   type=button   value=打印预览   onclick=document.all.WebBrowser.ExecWB(7,1)>

方法六:
<%@ page contentType="text/html; charset=GBK"%>
<%
String url="#"
%>

<style media="print">
    .noPrint { display: none }
  </style>
  <style media="screen">
     .print { display: none }
</style>
<html>
<title></title>

<body>

<jsp:include page="<%=url%>" />
<table width="100%" class="noPrint" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="60" align="center">
<input type=button name=button_show value="打     印" onclick="print();">
<input type=button name=button_show value="打印预览" onclick="preview();">
<input type=button name=button_setup value="打印设置" onclick="pageSetup();">
</td>
</tr>
</table>

</body>
<object id="factory" style="display:none" viewastext
classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814"
codebase="/etsc/ActiveX/ScriptX.zip#Version=6,1,430,5">
</object>
<script language="javascript" >
        function pageSetup()
        {
                factory.printing.PageSetup();
        }
        function preview()
        {
                setPageInfo();
                factory.printing.Preview();
        }
        function print()
        {
                setPageInfo();
                factory.printing.Print(true);
        }

        function setPageInfo(){
                //factory.printing.header = "&b&b第&p页/共&P页"
                //factory.printing.footer = "&b&b时间:&D&T"
                factory.printing.footer = ""
                factory.printing.leftMargin = 10
                factory.printing.topMargin = 20
                factory.printing.rightMargin = 10
                factory.printing.bottomMargin = 20
        }
</script>
</html>


特别推荐

广而告之