0

How can I hide the header like 'Page 1 of 1' and footer (url) when printing a webcontol in ASP.NET?

I currently doing by opening a new page on Print button click ande in it

protected void Page_Load(object sender, EventArgs e)
{
    if( null != Session["Control"] )
    {
        Control ctrl = ( Control )Session["Control"];
        PrintManager.PrintWebControl( ctrl );
        Session["Control"] = null;
    }
}

This will print the header and footer. How to avoid it?

Sauron
  • 16,668
  • 41
  • 122
  • 174

4 Answers4

1

That setting is configured by the user in their browser. Their is no way to disable it from code. You'r best bet is to include instructions on how to configure/disable the settings.

See an example here: http://www.xheo.com/products/sps/default.aspx?print=true

Paul Alexander
  • 31,970
  • 14
  • 96
  • 151
1

You should use CSS styles and specify they apply to a media type of print. See this article for help; http://www.cantoni.org/articles/printstyle

Basically create a seperate stylesheet for print styles only. If you want to hide something on the page use { display:none } as one of that elements style attributes. Then link your stylesheet in the HEAD element;

<link href="print.css" media="print" type="text/css" rel="stylesheet" />
Dave Anderson
  • 11,836
  • 3
  • 58
  • 79
  • This definately helps when printing but still doesn't eliminate the settings that the user has chosen in their browser...at least not for IE. – Paul Alexander Jun 17 '09 at 15:37
  • Right, I slightly misunderstood the question, I see they mean the actual browsers print header and footer. I guess Firefox may have this ability somewhere but IE is too closed. – Dave Anderson Jun 17 '09 at 18:51
0

If you're using Firefox, and you can have the client install this add-on.

Otherwise, if you are using Internet Explorer: google for "MeadCo ScriptX"

(FF option is free, IE option is free for basic functionality only)

mtk
  • 13,221
  • 16
  • 72
  • 112
0

For this we are using print class like it

public static void PrintWebControl(Control ctrl)
{
    PrintWebControl(ctrl, string.Empty);
}

public static void PrintWebControl(Control ctrl, string Script)
{
    StringWriter stringWrite = new StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
    if (ctrl is WebControl)
    {
        Unit w = new Unit(100, UnitType.Percentage); ((WebControl)ctrl).Width = w;
    }
    Page pg = new Page();
    pg.EnableEventValidation = false;
    if (Script != string.Empty)
    {
        pg.ClientScript.RegisterStartupScript(pg.GetType(), "PrintJavaScript", Script);
    }
    HtmlForm frm = new HtmlForm();

    pg.Controls.Add(frm);
    // done changes on below 1 line for unassigned header URL //
    frm.ResolveUrl("");
    frm.Attributes.Add("runat", "server");
    frm.Controls.Add(ctrl);

    pg.DesignerInitialize();

    pg.RenderControl(htmlWrite);
    string strHTML = stringWrite.ToString();
    HttpContext.Current.Response.Clear();        
    HttpContext.Current.Response.Write(strHTML);
    HttpContext.Current.Response.Write("<script>window.print();</script>");
    HttpContext.Current.Response.End();
}

here just change a line in form property and set

frm.ResolveUrl("");

so URl is not visible when page is print..i use it successfuly.

Tariqulazam
  • 4,535
  • 1
  • 34
  • 42
Ram
  • 1