打印网页--设置打印格式(真实例子)

本文介绍了如何使用JavaScript和CSS控制网页打印时的格式,包括隐藏不需要打印的内容、设置页眉页脚为空、调整表格高度和字体大小,以及利用ActiveXObject进行打印设置和预览。示例代码展示了如何实现打印按钮触发打印、页面设置和预览功能。

网页打印--设置打印格式

   

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) 关闭窗体无提示 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值