ASP.NET 调用ie打印(隐藏页眉、页脚以及打印按钮等)

来源:互联网 发布:fs工作软件 编辑:程序博客网 时间:2024/05/14 04:51

运行环境 WInXp Vs2005

问题:如果直接调用ie打印功能,打印出来的页面会有页眉页脚以及打印、打印预览等按钮。

 解决方案:

i:将与打印有关的按钮放在一个面板或者其他容器上,点击按钮的时候隐藏面板,操作完成后再显示面板,如点击打印按钮:1、隐藏按钮面板;2、打印;3、显示按钮面板。

 ii:页眉页脚的隐藏和显示用修改注册表来实现。一进入页面就调用方法隐藏掉页眉和页脚,打印完成后再恢复ie页眉和页脚的显示(bug:如果不点击打印按钮就不能恢复了。最好能在用户退出该页面时恢复)

代码:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>

<body>
<form id="form1" runat="server">
<object  id="WebBrowser" style="width:0; height:0; display:none" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" ></object>
<asp:Panel ID="btn" runat ="server" Width="445px">

<asp:Button ID="Button1" runat="server"  Text="打印" OnClick="Button1_Click" />
<input type="button" value="打印"     onclick="btn.style.display='none';document.all.WebBrowser.ExecWB(6,1);btn.style.display='list-item'" />
<input type="button" value="直接打印" onclick="btn.style.display='none';document.all.WebBrowser.ExecWB(6,6);btn.style.display='list-item'" />
<input type="button" value="页面设置" onclick="btn.style.display='list-item';document.all.WebBrowser.ExecWB(8,1);btn.style.display='list-item'" />
<input type="button" value="打印预览" onclick="btn.style.display='none';document.all.WebBrowser.ExecWB(7,1);btn.style.display='list-item'"/>
</asp:Panel>
        <br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
         &nbsp;
    </form>
</body>
</html>

前台:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Win32;

public partial class _Default : System.Web.UI.Page
{
    private ReportDocument customerReport;
    protected void Page_Load(object sender, EventArgs e)
    {
        printnull();//隐藏ie打印的页眉和页脚
        Button1.Attributes.Add("onclick","btn.style.display='none';document.all.WebBrowser.ExecWB(6,6);btn.style.display='list-item'");//添加打印属性
    }
     private void printnull()
    {
        RegistryKey pregkey;
        pregkey = Registry.CurrentUser.OpenSubKey("Software//Microsoft//Internet Explorer//PageSetup//", true);

        if (pregkey == null)
        {
            Response.Write("<script language='javascript'>alert('键值不存在');</script>");
        }

        else
        {   pregkey.SetValue("footer", "");
            pregkey.SetValue("header", "");
        }
    }
    private void printdef()
    {
        RegistryKey pregkey;
        pregkey = Registry.CurrentUser.OpenSubKey("Software//Microsoft//Internet Explorer//PageSetup//", true);

        if (pregkey == null)
        {
            Response.Write("<script language='javascript'>alert('键值不存在');</script>");
        }

        else
        {
            pregkey.SetValue("footer", "&u&b&d");
            pregkey.SetValue("header", "&w&bPage &p of &P");
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        printdef();//打印完成后恢复ie设置
    }

原创粉丝点击