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

JavaScript实现网页打印,可设置页眉、页脚、页边距
WebBrowser是IE内置的浏览器控件,无需用户下载,即可实现客户端页面打印。

关于这个组件还有其他的用法,列举如下:
WebBrowser.ExecWB(1,1) 打开 
Web.ExecWB(2,1) 关闭现在所有的IE窗口,并打开一个新窗口 
Web.ExecWB(4,1) 保存网页 
Web.ExecWB(6,1) 打印 
Web.ExecWB(7,1) 打印预览 
Web.ExecWB(8,1) 打印页面设置 
Web.ExecWB(10,1) 查看页面属性 
Web.ExecWB(15,1) 好像是撤销,有待确认 
Web.ExecWB(17,1) 全选 
Web.ExecWB(22,1) 刷新 
Web.ExecWB(45,1) 关闭窗体无提示 



注意有可能执行时,会出现没有效果的错误,这时原因是可能你的浏览器限制了active对象的创建,只要取消限制就好了,取消方法如下:

打开你的ie浏览器internet选项—— 安全—— 自定义级别—— 把对没有标记为安全的activex控件进行初始化和脚本运行 设置为启用。

<SCRIPT language=javascript>
var HKEY_Root,HKEY_Path,HKEY_Key; 
HKEY_Root="HKEY_CURRENT_USER"; 
HKEY_Path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\"; 
var head,foot,top,bottom,left,right;

//取得页面打印设置的原参数数据
function PageSetup_temp() {
	try 
 { 
  var Wsh=new ActiveXObject("WScript.Shell"); 
  HKEY_Key="header"; 
//取得页眉默认值
  head = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key); 
  HKEY_Key="footer"; 
//取得页脚默认值
  foot = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key); 
  HKEY_Key="margin_bottom"; 
//取得下页边距
  bottom = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key); 
  HKEY_Key="margin_left"; 
//取得左页边距
  left = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key); 
  HKEY_Key="margin_right"; 
//取得右页边距
  right = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key); 
  HKEY_Key="margin_top"; 
//取得上页边距
  top = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key); 
 } 
 catch(e){
    alert("不允许ActiveX控件");
 } 
}

//设置网页打印的页眉页脚和页边距
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,""); 
  HKEY_Key="margin_bottom"; 
//设置下页边距(0)
  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"0"); 
  HKEY_Key="margin_left"; 
//设置左页边距(0)
  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"0"); 
  HKEY_Key="margin_right"; 
//设置右页边距(0)
  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"0"); 
  HKEY_Key="margin_top"; 
//设置上页边距(8)
  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"8"); 
 } 
 catch(e){
    alert("不允许ActiveX控件");
 } 
} 
//设置网页打印的页眉页脚和页边距为默认值 
function  PageSetup_Default() 
{   
 try 
 { 
  var Wsh=new ActiveXObject("WScript.Shell"); 
  HKEY_Key="header"; 
  HKEY_Key="header"; 
//还原页眉
  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,head); 
  HKEY_Key="footer"; 
//还原页脚
  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,foot); 
  HKEY_Key="margin_bottom"; 
//还原下页边距
  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,bottom); 
  HKEY_Key="margin_left"; 
//还原左页边距
  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,left); 
  HKEY_Key="margin_right"; 
//还原右页边距
  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,right); 
  HKEY_Key="margin_top"; 
//还原上页边距
  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,top); 
 }
 catch(e){
 	alert("不允许ActiveX控件");
 }
}

function printorder()
{
		PageSetup_temp();//取得默认值
		PageSetup_Null();//设置页面
		factory.execwb(6,6);//打印页面
		PageSetup_Default();//还原页面设置
		//factory.execwb(6,6);
		window.close();
}

</script>
<OBJECT id=factory height=0 width=0 classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2></OBJECT>