Customize Print Header/Footer ASP.Net

Reference:- Click here to view Rajasekhar Chowdary's online profile.

Introduction

I have seen many developers looking for a solution to remove the url that will be printed whenever we print a webpage. Due to some security reasons some applications doesnt want to display the url in the printed documents. Even i faced this problem and i could able to overcome it with vb.net code.

I have a window which displays a photo along with a print icon. whenever i click the print icon i am printing the page with customized footer for the printed page. Once the printing is done i am changing back the Registry Settings to the original settings. As i said above the photo being displayed is sensitive i have disabled right click, drag and select and displaying the image toolbar(provided by IE 6.0 with save and other buttons) when we hover mouse over the image. The photo window i am displaying doesnt have any toolbar, title bar, status bar and help icon.

Steps i have implemented.

1) When we load the photo page change the registry settings to our own settings

2)Once the printing is done or the photo window is closed change back the settings to default values.

I have a small photo and whenver i click on it i am opening a big photo in different window with a print icon in the screen. The code to open a window without the toolbar, title bar etc is below.

<javascript ...>function DisplayBigPhoto(webpage, width, height){

//If the photo window is already opened and not closed then bring it to focus if the user click on the small photo second time, dont open one more window

if(myWin && (!myWin.closed)) {

myWin.focus();

} else{ //Display the window in the center of the screen with supplied height and width

var str = "height=" + height + ",innerHeight=" + height;

str += ",width=" + width + ",innerWidth=" + width;

if (window.screen) {

var ah = screen.availHeight - 30;

var aw = screen.availWidth - 10;

var xc = (aw - width) / 2;

var yc = (ah - height) / 2;

str += ",left=" + xc + ",screenX=" + xc;

str += ",top=" + yc + ",screenY=" + yc;

}

var specs = "'"+ str + ",menubar=no,scrollbars=no,toolbar=no,location=center,

directories=no,resizable=no,status=no,help=0"+ "'";

myWin = window.open(webpage, '', eval(specs))

}}</javascript ...>

Now the window with big photo opened. See the photo page code below.

<html ><head> //below meta tag hides the image toolbar when we hover the mouse

over the image

<meta http-equiv="imagetoolbar" content="no" />

<title>Photo</title> <script language="JavaScript" type="text/javascript">

//This is to hide the <div> tag which contains the print icon, so that it will not be printed

function hide(showpage,id){

if (!showpage ){

document.all[id].style.visibility = 'hidden';}

else {

document.all[id].style.visibility = 'visible';}

}

function PrintPage(){

window.print(); }

//Bringup another temporary window to change the registry settings back to the original values

function ShowWindow(){

var specs = "'" + "left=0,top=0,height=5,screenX=0,screenY=0,width=5,menubar=no,scrollbars=no,

toolbar=no,location=center,directories=no,resizable=no,status=no,help=0"+ "'";

window.open('changeregistry.aspx', '', eval(specs));

}</script></head>

//attribtes of body tag to disable selection ,context menu etc

<body id="body" onselectstart="return false" ondragstart="return false" oncontextmenu="return false" onunload="ShowWindow()">

<form id="form1"><div>

<table width="100%"> <tr>

<td colspan="3"><div id="printImage">

<img style= "cursor:hand;" id="DhsPrint" src="images/icon_print.gif" onclick="hide(false,'printImage');PrintPage();hide(true,'printImage')" />

</div> </td> </tr> <tr>

<td align="left" colspan="3" > <div > <asp:Image ID="DhsImgPhoto" runat="server" />

</div></td></tr> </table></div>

</form></body></html>

Since ASP.net pages are compiled on the webserver and rendered to the client, the UnLoad event will be fired whenever the page is unloaded from the server once its rendered fully. We do not have any mechanism to trap the close event of the form(by pressing the X button) in ASP.net. This is the reason i am opening another aspx page to set back the origianal footer in registry.

The code that is used to change the registry settings:

Imports

Microsoft.Win32

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim pageKey As RegistryKey = Registry.CurrentUser.OpenSubKey("software\microsoft\internet explorer\pagesetup", True)

Dim newFooter As String = "&b&D&b&T"

Dim curFooter As Object = pageKey.GetValue("footer")

pageKey.SetValue("footerTemp", curFooter)

pageKey.SetValue("footer", newFooter)

pageKey.Close()

End Sub

Open the registry key, retrieve the current footer, store it in a temporary string, then set the modified footer constants and finally, the Close() method will commit the changes to the registry. Similarly you can even change the header too. Use below registry constants for different options.

You can use any of IE's header and footer codes in your custom code as well:

  • &w Window title
  • &u Page address (URL)
  • &d Date in short format specified by Regional Settings in Control Panel
  • &D Date in long format specified by Regional Settings in Control Panel
  • &t Time in the format specified by Regional Settings in Control Panel
  • &T Time in 24-hour format
  • &p Current page number
  • &P Total number of pages
  • && A single ampersand (&)
  • &b The text immediately following these characters as centered
  • &b&b The text immediately following the first "&b" as centered, and the text following the second "&b" as right-justified

By now we are done with changing the registry and setting the customized footer. As i said above since we can not trap the window close event in ASP.net, i am handling the onunload html event in the body tag. During unload i am loading another aspx page(see ShowWindow() function above) to set back the registry constants to their original values.

The temporary aspx page will be opened with its minimal size and sets back the registry values and closes itself.

//temporary aspx page load method

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim pageKey As RegistryKey = Registry.CurrentUser.OpenSubKey("software\microsoft\internet explorer\pagesetup", True)

Dim oriFooter As Object = pageKey.GetValue("footerTemp")

pageKey.SetValue("footer", oriFooter)

pageKey.DeleteValue("footerTemp")

pageKey.Close()

ClientScript.RegisterStartupScript(Me.GetType, "error", "<script language='Javascript'>window.close();</script>")

End Sub

Open the registry key, read the original footer values from the temporary string, update the footer, delete the temporary string, commit changes to registry and close the window.

There is a nice article in code project about modifying the registry. Read it for more understanding. Remember, you can do anything on the registry if you have permissions only and playing with registry is dangerous, as registry is heart of the windows OS.

Hope the article is good and it solves your problem.

Print | posted on Monday, May 14, 2007 8:57 PM

Comments on this post

# re: Customize Print Header/Footer ASP.Net

Requesting Gravatar...
Hai ,
i doing small project in asp.net2.0 i need to change homepage of Internet Explorer by code if you know that can you please guide me in this matter
(Explorers Registry editing by code)
Left by ramu on Jun 27, 2007 12:21 AM

# re: Customize Print Header/Footer ASP.Net

Requesting Gravatar...
Good, very good
Left by Marcos Aguiar Jr on Jul 04, 2007 1:52 AM

# re: Customize Print Header/Footer ASP.Net

Requesting Gravatar...
Nice article.

Last one week i have faced the same problem but today i got the solution of this article

Thanks too much...........
Left by Sunil Kumar Jena on Jul 24, 2007 11:24 AM

# re: Customize Print Header/Footer ASP.Net

Requesting Gravatar...
Hi

Thanx and it worked fine in the development pc,

but when i published on the server and tried printing the webpage from my local pc, i am getting an error 'Object reference not set to an instance of an object'

an ideas..?

thanks in advance

Saheen
Left by Saheen on Aug 22, 2007 12:37 PM

# re: Customize Print Header/Footer ASP.Net

Requesting Gravatar...
Page_Load is running on server side, I think, so change the registry means: change the IIS server's registry, not the client's IE setting(or you will change the client's IE as you wish).
Left by Tom LUKE on Aug 22, 2007 9:07 PM

# re: Customize Print Header/Footer ASP.Net

Requesting Gravatar...
hi

any way to change client side page header and footer with asp.net ?

Saheen
Left by Saheen on Aug 29, 2007 11:15 AM

# re: Customize Print Header/Footer ASP.Net

Requesting Gravatar...
Hi There,
Actually this article provides a workaround for printing Header/Footer not the solution, I'm also struggling with same issue which u addressed :(...., I've tried to resolve the issue through CSS but could not resolve yet....
Please update me If u get the solution.

Thanks
Left by Naren on Aug 30, 2007 9:36 AM

# re: Customize Print Header/Footer ASP.Net

Requesting Gravatar...
Hi

I don't know what this article for.. (printing from webserver ? ), who is going to print from a webserver..., if required to print , web admin can easily change settings manually

Saheen

Left by Saheen on Sep 02, 2007 3:33 PM

# re: Customize Print Header/Footer ASP.Net

Requesting Gravatar...
very bad blog
Left by Sheikh on Jun 09, 2008 10:28 AM

# re: Customize Print Header/Footer ASP.Net

Requesting Gravatar...
Hi,
It is working in my local iis fine. But when i upload this in to my server there not working and generationg error like "System.NullReferenceException: Object reference not set to an instance of an object."

Please resolve this ASAP.

Thanks,
Rajaram
Left by mohanrao.p on Jul 31, 2008 12:11 PM

# re: Customize Print Header/Footer ASP.Net

Requesting Gravatar...
Hi,

I developed my program with your sample program to change Print/Header and footer. but after i run the following line, i got nothing.
Dim pageKey As RegistryKey = Registry.CurrentUser.OpenSubKey("software\microsoft\internet explorer\pagesetup", True)

Note: project is configure in IIS.

Could you please help me?

Thanks

Left by NaingWin on Aug 07, 2008 12:39 PM

# re: Customize Print Header/Footer ASP.Net

Requesting Gravatar...
This is not the real solution. This code will change the servers registry not the clients.
Left by Hiran Das on Nov 17, 2008 5:47 PM

# re: Customize Print Header/Footer ASP.Net

Requesting Gravatar...
true, this is only a server side solution and it is changing the server's registry, which is dangerous.
The 'Object reference Error' is due to permissions. trying locally the server might be using an admin account, but when trying remotelly, as it should be, IIS is using IUSR_(MACHINENAME) or the ASPNET user which are limited accounts.
Left by cesar on Feb 25, 2009 5:59 PM

Your comment:

 (will show your gravatar)