网页打印--设置打印格式
1.设置打印不显示的内容
<style type="text/css" media="print">
.noprint{display : none;}
</style>
<div class="noprint">
<table class="tableinfotb">
<tr>
<td align="center">
<input type="button"
class="formbutton" name="Submit"
value="<fmt:message key="public.button.back"/>"
onclick="javascript:history.go(-1)">
<input type="button"
class="formbutton" name="Submit"
value="打印"
onclick="printit();">
</td>
</tr>
</table>
</div>
2.打印网页
<script language="javascript">
var HKEY_RootPath="HKEY_CURRENT_USER//Software//Microsoft//Internet Explorer//PageSetup//";
// 设置页眉页脚为空
function PageSetup_del(){
alert("ddd");
try{
var WSc=new ActiveXObject("WScript.Shell");
HKEY_Key="header";
WSc.RegWrite(HKEY_RootPath+HKEY_Key,"");
HKEY_Key="footer";
WSc.RegWrite(HKEY_RootPath+HKEY_Key,"");
}catch(e){}
}
//恢复页眉页脚
function PageSetup_set(){
try{
var WSc=new ActiveXObject("WScript.Shell");
HKEY_Key="header";
WSc.RegWrite(HKEY_RootPath+HKEY_Key,"&w&b页码,&p/&P");
HKEY_Key="footer";
WSc.RegWrite(HKEY_RootPath+HKEY_Key,"&u&b&d");
}catch(e){}
}
//打印
function printit()
{
document.getElementById("table1").style.height=800 + "px";
document.getElementById("table2").style.height=400 + "px";
fontZoom(16);
PageSetup_del();
window.print();
document.getElementById("table1").style.height="auto";
document.getElementById("table2").style.height="auto";
fontZoom(12);
}
//设置打印字体
function fontZoom(size)
{
var l = document.getElementsByTagName("td");
for( i=0;i<l.length;i++) {
l[i].style.fontSize=size+'px';
}
}
</script>
-----------------------------------------------------------------------------------------------------------
补充:
1.JavaScript打印
<input id="btnPrint" type="button" value="打印" onclick="javascript:window.print();" />
可以用样式控制,你想让那块打印就打印啊,样式如下:
<style type="text/css" media=print>
.noprint{display : none }
</style>
然后使用样式就可以:
<p class="noprint">不需要打印的地方</p>
=============================================================
2.IE打印
<script language="javascript">
function printsetup(){
// by http://www.cidu.net/ 打印页面设置
wb.execwb(8,1);
}
function printpreview(){
//by http://www.cidu.net/ 打印页面预览
wb.execwb(7,1);
}
function printit()
{
if (confirm('确定打印吗?')) {
wb.execwb(6,6)
}
}
</script>
<OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height=0 id=wb name=wb width=0></OBJECT>
<input type=button name=button_print value="打印本单据" onclick="javascript:printit()">
<input type=button name=button_setup value="打印页面设置" onclick="javascript:printsetup();">
<input type=button name=button_show value="打印预览" onclick="javascript:printpreview();">
<input type=button name=button_fh value="关闭" onclick="javascript:window.close();">
------------------------------------------------
关于这个组件还有其他的用法,列举如下:
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) 关闭窗体无提示
本文介绍了如何使用JavaScript和CSS控制网页打印时的格式,包括隐藏不需要打印的内容、设置页眉页脚为空、调整表格高度和字体大小,以及利用ActiveXObject进行打印设置和预览。示例代码展示了如何实现打印按钮触发打印、页面设置和预览功能。
5万+

被折叠的 条评论
为什么被折叠?



