jsp页面实现打印功能

23 篇文章 0 订阅
14 篇文章 0 订阅

 

直接在jsp页面实现打印,需要启用IE的ActiveX控件。

 

实现打印其实不是jsp的功能,而是浏览器的功能,我们只是通过js直接调用了浏览器的打印功能了:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">


<!-- 需要启用IE的 自定义级别中的  没有标记为安全的ActiveX控件 -->

<!-- 
好用,使用的时候注意要创建浏览器对象<OBJECTid="WebBrowser" ></OBJECTid>,而且要注意html页面的dtd是否正确:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 -->


<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>JSP中实现打印功能</title>
<style type="text/css">
<!--
.STYLE5 {color: #993399; font-size: xx-large; }
.STYLE6 {font-size: xx-large}
-->
</style>
<!--  将不用打印出来的地方隐藏了-->
<style type="text/css" media="print">
<!--
 .dis{display:none;}
-->
</style>
</head>


<body>
	<table width="616" height="350" border="1" align="center">
		  <tr>
			<td colspan="2"> <span class="STYLE6">bbbbb </span></td>
		  </tr>
		  <tr>
			<td><span class="STYLE5">aaa</span></td>
			<td bgcolor="#99CCCC"><span class="STYLE6">bbbb</span></td>
		  </tr>
		  <tr>
			<td><span class="STYLE5">aaa</span></td>
			<td bgcolor="#99CCCC"><span class="STYLE6">bbbb</span></td>
		  </tr>
		  <tr>
			<td><span class="STYLE5">aaa</span></td>
			<td bgcolor="#99CCCC"><span class="STYLE6">bbbbb</span></td>
		  </tr>
	</table>
	<p class="dis" align="center">
		<OBJECT   classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2   height=0   id="WebBrowser"   width=0> </OBJECT>   
		<input   name=Button   onClick=document.all.WebBrowser.ExecWB(1,1)   type=button   value=打开> 
		<input   name=Button   onClick=document.all.WebBrowser.ExecWB(2,1)   type=button   value=关闭所有> 
		<input   name=Button   onClick=document.all.WebBrowser.ExecWB(4,1)   type=button   value=另存为>   
		<input   name=Button   onClick=document.all.WebBrowser.ExecWB(6,1)   type=button   value=打印> 
		<input   name=Button   onClick=document.all.WebBrowser.ExecWB(6,6)   type=button   value=直接打印> 
		<input   name=Button   onClick=document.all.WebBrowser.ExecWB(7,1)   type=button   value=打印预览> 
		<input   name=Button   onClick=document.all.WebBrowser.ExecWB(8,1)   type=button   value=页面设置> 
		<input   name=Button   onClick=document.all.WebBrowser.ExecWB(10,1)   type=button   value=属性> 
		<input   name=Button   onClick=document.all.WebBrowser.ExecWB(17,1)   type=button   value=全选> 
		<input   name=Button   onClick=document.all.WebBrowser.ExecWB(22,1)   type=button   value=刷新> 
		<input   name=Button   onClick=document.all.WebBrowser.ExecWB(45,1)   type=button   value=关闭>
	</p>
</body>
</html>

 

首先引入一个WebBrowser在需要打印的页面,可以直接添加: 复制代码 代码如下: <object id="WebBrowser" classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height="0" width="0"> </object> 2 .页面设置打印预览 如下所示,直接调用即可 复制代码 代码如下: document.all.WebBrowser.ExecWB(6,6) 直接打印 document.all.WebBrowser.ExecWB(8,1) 页面设置 document.all.WebBrowser.ExecWB(7,1) 打印预览 3 隐藏不打印的页面元素和分页 CSS 有个Media 属性,可以分开设置打印和显示的格式。 如 <style media="print" type="text/css"> …</style> 中间的格式将只在打印时起作用,不会影响显示界面。 所以可以设定 <style media="print" type="text/css"> .Noprint{display:none;} .PageNext{page-break-after: always;} </style> 然后给不想打印的页面元素添加: class="Noprint" ,那就不会出现在打印打印预览中了。 想分页的地方添加: <div class="PageNext"></div> 就可以了。 4.打印页面的特定部分 通过将需要打印的特定部分另建一个页面,然后装入主页面的一个IFrame中,再调用IFrame的打印方法,只打印IFrame中的内容实现的。 如: <iframe visible" name="FrameId" width="100%" height="30%" src="NeedPrintedPage.asp"></iframe> 下面的pringFrame js函数将只打印Iframe中的内容,可以直接引用使用,如printFrame(FrameId); 复制代码 代码如下: window.print = printFrame; // main stuff function printFrame(frame, onfinish) { if ( !frame ) frame = window; function execOnFinish() { switch ( typeof(onfinish) ) { case "string": execScript(onfinish); break;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值