hongyuniu

Whatever You Do, Do Your Best !

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 首先建立一个模板页:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="PrintMasterPage.master.cs"
    Inherits
="PrintMasterPage" 
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    
<title></title>
    
<object id='WebBrowser' width='0' height='0' classid='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'>
    
</object>
    
<link rel="stylesheet" href="css/show.css" type="text/css" />

<script language="javascript" type="text/javascript">
//获取单个对象
function getObj(id)
{
    
return
 document.getElementById(id);
}

//获取对象组
function getObjs(name)
{
    
return
 document.getElementsByTagName(name);
}

//打印文档
function printDoc()
{
    
var objs=getObjs('div'
);
    
var length=
objs.length;
    
for(var i=0;i<length;i++
)
    
{
        
if(objs[i].print==1
)
        
{
            p
=
objs[i].parentNode;
            p.style.visibility
='visible'
;
            
if(p.childNodes.length>1
)
            
{
                
for(var j=0;j<p.childNodes.length;j++
)
                
{
                    
if(p.childNodes[j].print!=1&&p.childNodes[j].style!=
undefined)
                    
{
                        p.childNodes[j].style.visibility
='hidden'
;
                    }

                }

            }

            objs[i].visibility
='visible';
        }

    }

    PageSetup_Null();
    
//InitPrint();
    getObj('WebBrowser').ExecWB(7,1);
}

</script>

<script language="JavaScript" type="text/javascript">   
var
 HKEY_Root,HKEY_Path,HKEY_Key; 
HKEY_Root
="HKEY_CURRENT_USER"

HKEY_Path
="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\"

//设置网页打印的页眉页脚为空 

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"

  Wsh.RegWrite(HKEY_Root
+HKEY_Path+HKEY_Key,"2"
); 
  HKEY_Key
="margin_left"

  Wsh.RegWrite(HKEY_Root
+HKEY_Path+HKEY_Key,"2"
); 
  HKEY_Key
="margin_right"

  Wsh.RegWrite(HKEY_Root
+HKEY_Path+HKEY_Key,"2"
); 
  HKEY_Key
="margin_top"

  Wsh.RegWrite(HKEY_Root
+HKEY_Path+HKEY_Key,"2"
); 
 }
 
 
catch(e)
{
    
//alert(e);

 }
 
}
 
//设置网页打印的页眉页脚为默认值 

function  PageSetup_Default() 
{   
 
try
 
 

  
var Wsh=new ActiveXObject("WScript.Shell"
); 
  HKEY_Key
="header"

  Wsh.RegWrite(HKEY_Root
+HKEY_Path+HKEY_Key,"&w&b页码,&p/&P"
); 
  HKEY_Key
="footer"

  Wsh.RegWrite(HKEY_Root
+HKEY_Path+HKEY_Key,"&u&b&d"
); 
 }
 
 
catch(e){}
 
}
 
    
</script>

<style type="text/css" media="print">

body
{visibility:hidden;}
.print
{visibility:visible;}

</style>

<style type="text/css" media="screen">

body
{visibility:visible;
    margin-top
: 0px;
    margin-left
:0px;
}

.control
{
    margin
: 0px;
    padding
: 0px;
    width
: 590px;
}


</style>
</head>
<body>
    
<form id="form1" runat="server">
        
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        
</asp:ContentPlaceHolder>
        
<div align="center">
            
<table width="100%">
                
<tr>
                    
<td align="center">
                        
<input id="btnPrint" type="button" value=" 打 印 " onclick="javascript:printDoc();" class="button" />
                        
<input type="button" id="btnSet" class="button" value="页面设置" onclick="javascript:document.all.WebBrowser.ExecWB(8,1)" />
                        
<input type="button" id="btnClose" onclick="javascript:document.all.WebBrowser.ExecWB(45,1);" value=" 关 闭 "  class="button" />
                    
</td>
                
</tr>
            
</table>
        
</div>
    
</form>
</body>
</html>

 

然后在子页面中设置给要打印的DIV添加一个(print="1")的属性就可以了。

posted on 2008-12-04 14:11  CsharpFish  阅读(1991)  评论(0编辑  收藏  举报