温故而知新

.net相关

导航

关于用js打印网页

一、基本打印功能

 <script language="javascript">
  function printsetup(){
  // 打印页面设置
  wb.execwb(8,1);
  }
  function printpreview(){
  // 打印页面预览     
  wb.execwb(7,1);           
  }
  function printit()
  {
  if (confirm('确定打印吗?')) {
  wb.execwb(60,60)
  }
  }
</script>

<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();"/>

 

二、在打印的页面中加入分页符

1、先在页面中加入样式表

<style>
.PageNext{page-break-after: always;}<!--控制分页-->
.style8 {font-size: medium; color: #000066; }
</style>

2、在body中可以这样写

<TABLE>
    <Tr>
        <TD><span class="style8">第一页</span></TD>
</Tr></TABLE>

<!-- 下面这个网页元素就是用于打印时分隔页面-->
<div id="pageNext1" style="visibility:hidden;"><p class="PageNext"></p></div>
<TABLE>
    <Tr>
        <TD align="center"><span class="style8">第二页</span></TD>
</Tr></TABLE>

 

三、在打印页面中去除不需要的元素

1、先在页面中加入这个样式

<style media=print> 
.Noprint{display:none;}<!--用本样式在打印时隐藏非打印项目--> 
</style>

2、页面中不需要的元素可以这么写

<input id="abc" class="Noprint" type=button name=print value="打印本页面" onClick="javascript:window.print()">

<font class="Noprint">隐藏的文字</font>

 

四、设计打印页眉和页脚

1、在页面中加入js代码

<script language="JavaScript">
var hkey_root,hkey_path,hkey_key
hkey_root="HKEY_CURRENT_USER"
hkey_path="http://www.cnblogs.com/zhaoxinxin/admin/file://software//Microsoft//Internet Explorer\\PageSetup\\"
//设置网页打印的页眉页脚为空
function pagesetup_null(){
try{
var RegWsh = new ActiveXObject("WScript.Shell")
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 pagesetup_default(){
try{
var RegWsh = new ActiveXObject("WScript.Shell")
hkey_key="header"
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"&w&b页码,&p/&P")
hkey_key="footer"
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"&u&b&d")
}catch(e){}
}
</script>

2、在body中这么写

<input type="button" value="清空页码" onclick=pagesetup_null()>
<input type="button" value="恢复页码" onclick=pagesetup_default()><br/>

 

五、用样式表调节页面边距

<style media="print" type="text/css">
  body   {font-size:9pt;   margin:-70px   0px   0px   0px  }
</style>

posted on 2008-11-13 22:38  业余程序猿  阅读(3761)  评论(0编辑  收藏  举报