日期:2014-05-16  浏览次数:20418 次

JS打印的几种实现方法
1、使用IE自带的WebBrowser控件进行打印,用户无需下载,缺点是需要设置IE浏览器的安全设置,“Internet选项”-->“安全”-->“自定义级别”-->“对未标记为可安全执行脚本的ActiveX控件初始化并执行脚本”改为“启用”或者是“提示”
<%@ page contentType="text/html;charset=utf-8" language="java"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'MyJsp.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<link href="/print.css" rel="stylesheet" />
	<link href="/print.css" rel="stylesheet" media="print" />
	<style type="text/css" media=print>
		.noprint{display : none }
	</style>
	<script language="javascript">
		// 打印页面设置 
	  function printsetup() {
		  wb.execwb(8,1);
	  }
		// 打印页面预览
	  function printpreview() {
		  wb.execwb(7,1); 
	  } 
	  function printit() {
		  if (confirm('确定打印吗?')) { 
		  		wb.execwb(6,6) 
		  }
	  }
		
		var HKEY_Root,HKEY_Path,HKEY_Key;
		HKEY_Root="HKEY_CURRENT_USER";
		HKEY_Path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
		//设置网页打印的页眉页脚为空
		function PageSetup_Null() {
			try {
				var Wsh=new ActiveXObject("WScript.Shell");
				HKEY_Key="header";
				Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");
				HKEY_Key="footer";
				Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");
			}catch(e){
			
			}
		}

		//设置网页打印的页眉页脚为默认值
		function  PageSetup_Default() {
			try {
				var Wsh=new ActiveXObject("WScript.Shell");
				HKEY_Key="header";
				Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"&w&b页码,&p/&P");
				HKEY_Key="footer";
				Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"&u&b&d");
			}catch(e){
			
			}
		}
	</script>
  </head>
  
  <body>
	<DIV align=center>
	<OBJECT id="wb" height="0" width="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" name="wb"></OBJECT>
	<input name="button_print" onclick="javascript:printit()" type="button" value="打印"  class="noprint"/>
	<input name="button_setup" onclick="javascript:printsetup();" type="button" value="打印页面设置" class="noprint"/> 
	<input name="button_show" onclick="javascript:printpreview();" type="button" value="打印预览" class="noprint" />
	<input type="button" value="清空页码"  onclick="PageSetup_Null()" class="noprint"/>
	<input type="button" value="恢复页码"  onclick="PageSetup_Default()" class="noprint"/>
	</DIV>
           这些文字就是要打印的内容!
  </body>
</html>


2、区域打印
<%@ page contentType="text/html;charset=utf-8" language="java"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
	<head></head>
	<script language="javascript">
		function preview() {
			bdhtml = window.document.body.innerHTML;
			sprnstr = "<!--startprint-->";
			eprnstr = "<!--endprint-->";
			prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
			prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
			window.document.body.innerHTML = prnhtml;
			window.print();
		}
	</script>
	<body>
		<form id="WebForm1" method="post">
			<center>
				本部分内容不被打印
			</center>
			<!--startprint-->
			<div align="center">