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.