Friday, February 2, 2007

Removing Header and Footer on printing ASP.NET

I was looking all over the Internet for this answer until I stumbled on a solution that gave me a quick fix to this issue.

I looking for way to remove the header and footer from the web page on printing.

The solution is with in the .NET Framework Class Library called Microsoft.Win32
After playing around with the class for about 1 hour I finally figured it out.

This code will be in C#

First you must import the Namespace using the import statement with in the page or in code behind.

using Microsoft.Win32

RegistryKey pageKey = Registry.CurrentUser.OpenSubKey("software\\microsoft\\internet explorer\\pagesetup", true);
//set the page of pages
string newFooter = "&p of &P";
Object origKeyfooter = pageKey.GetValue("footer");
Object origKeyheader = pageKey.GetValue("header");
if (Convert.ToString(origKeyfooter) == "&p of &P" || Convert.ToString(origKeyheader) == "&p of &P")
{
//This has already been configured
this.lblMessage.Text = "Computer has been configured";
}
else
{
//this will set the keys back to original state
pageKey.SetValue("headerTemp", origKeyheader);
pageKey.SetValue("footerTemp", origKeyfooter);
pageKey.SetValue("header", newFooter);
pageKey.SetValue("footer", newFooter);
pageKey.Close();
this.lblMessage.Text = "Computer has been configured";
}


If you read the comments provided in code it will guide you along the way to write this quick and easy fix. If you have a better way please do not hesitate to comment.

No comments: