Hide header and footer while printing a webcontrol using ASP.NET
-
05-07-2019 - |
Question
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?
Solution
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
OTHER TIPS
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" />
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.
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)