使用方式:
1.在html中的body标签中插入:
<body onload="body_onload();">
<form id="form1" runat="server">
<object id="WebBrowser" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height="0" width="0">
</object>
<table id="tblbtn" class="notDisplay" runat="server" style="margin-left:200px;"><!--不打印的内容-->
<tr height="24" align="right">
<td align="left">
<input type="button" id="btnprint2" value="打印" onclick="document.all.WebBrowser.ExecWB(6, 1)" />
<input type="button" id="btnprint" value="直接打印" onclick="document.all.WebBrowser.ExecWB(6, 6)" />
<input type="button" id="btnSetprinter" value="页面设置" onclick="document.all.WebBrowser.ExecWB(8, 1)" />
<input type="button" id="btnpreview" value="打印预览" onclick="document.all.WebBrowser.ExecWB(7, 1)" />
<input type="button" id="exit" value="退出" onclick="javascript: window.close();" />
</td>
</tr>
</table>
<div style="width:100%" id="divNeiRong" runat="server"></div><!--打印的内容-->
</form>
</body>
2.css打印专用样式:
<style media="print" type="text/css">
.notDisplay{
display:none;
}
.PageNext{
page-break-after: always;
}
</style>
3.执行js函数:
//修改注册表上下左右边距,清空页眉,清空页脚
function PageSetup_Null(mtop,mbottom,mleft,mright)
{
//注意注册表里的单位是英寸,打印设置中是毫米,1英寸=25.4毫米
mtop=""+parseFloat(mtop)/25.4;
mbottom=""+parseFloat(mbottom)/25.4;
mleft=""+parseFloat(mleft)/25.4;
mright=""+parseFloat(mright)/25.4;
var HKEY_Root,HKEY_Path,HKEY_Key;
HKEY_Root="HKEY_CURRENT_USER";
HKEY_Path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
try
{
var RegWsh=new ActiveXObject("WScript.Shell");
HKEY_Key="margin_left" ;
RegWsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,mleft); //左边边界
HKEY_Key="margin_right" ;
RegWsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,mright); //右边边界
HKEY_Key="margin_top" ;
RegWsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,mtop);//上边边界
HKEY_Key="margin_bottom" ;
RegWsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,mbottom); //下边边界
HKEY_Key="header";
RegWsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,""); //清空页眉
HKEY_Key="footer";
RegWsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");//清空页脚
}
catch(e){}
}
function body_onload()
{
PageSetup_Null(8,0,8,8);//毫米
}