Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
New Feature: Community Sites:
Create your own .NET community website and start earning from Google AdSense !
It's Free !
|
Printing Invoices in C#
|
Printing invoices in a web application.
INTRODUCTION With the revolutionization of the web, web based applications have become the buzzword these days. Almost all vendors, small and big are supporting their business with web based applications.
THE PROBLEM This is a common scenario that I ran into while developing one such application. As far as the application screens are concerned, alls well that looks well, but when it comes to printing out anything other than reports, say invoices, it took me a real long time trying to figure out how I should get rid of all the extra fields to the top and bottom of the page, namely the form name, the page 1 of 1, the URL and the date that is printed by default in IE 6.0.
THE SOLUTION One of the solution is to use VC++ to change the default printtemplate that IE picks up, but that seems to be a rather confusing and difficult option which involves a lot of pointer handling that can be hazardous to the memory too. The other method is a little risky but a quick and easy solution. This is the solution we discuss here today. Modifying the registry to delete the header and footer text for internet explorer window. The values to be printed in the page header and footer are stored at the following location in the registry:
For the Header and Footer, the values are picked up from the following: HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup
The Microsoft.Win32 namespace provides classes for editing the registry. The most widely used classes for registry editing are The “Registry” class and the “RegistryKey” class. The “Registry” Class has 5 fields each identifying the following roots: HKEY_CLASSSES_ROOT identified by the field ClassesRoot HKEY_CURRENT_CONFIG identified by the field CurrentConfig HKEY_CURRENT_USER identified by the field CurrentUser HKEY_LOCAL_MACHINE identified by the field LocalMachine and HKEY_USERS identified by the field Users.
The field of our interest as per the location mentioned above is ‘CurrentUser’ for the root HKEY_CURRENT_USER. RegistryKey regkey = Registry.CurrentUser;
The class ‘RegistryKey’ is used to open the sub keys to the root registry key. When an OpenSubKey is fired on a registry object, the value returned is that of type ‘RegistryKey’. regkey = regkey.OpenSubKey(@"Software\Microsoft\Internet Explorer\PageSetup",true);
Once the required subkey is obtained, it is just a matter of a simple GetValue() and SetValue() to read and write values to the registry respectively.
Following is a simple C# example that puts all the pieces together.
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using Microsoft.Win32;
namespace webPrint { /// /// Summary description for WebForm1. /// public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Button btnPrint; protected object IEHeader; protected object IEFooter;
private void Page_Load(object sender, System.EventArgs e) { Button1.Attributes.Add("onclick","window.print();"); RegistryKey regkey = Registry.CurrentUser; regkey = regkey.OpenSubKey(@"Software\Microsoft\Internet Explorer\PageSetup",true); // Store the existing values. IEHeader = regkey.GetValue("header"); IEFooter = regkey.GetValue("footer");
// Set the header and footer to blank regkey.SetValue("header",""); regkey.SetValue("footer",""); }
#region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.Unload += new System.EventHandler(this.WebForm1_Unload); this.Load += new System.EventHandler(this.Page_Load);
} #endregion
private void WebForm1_Unload(object sender, System.EventArgs e) { // Restore the header and footer values. regkey.SetValue("header",IEHeader.ToString()); regkey.SetValue("footer",IEFooter.ToString()); } } }
The above code first stores the current values for header and footer into object variables, resets them to blank values and finally restores the values on unload to the earlier values, just to ensure that outside the application, everything works normal and as expected.
SUMMARY In this article I have made a very raw effort to put together a solution of a problem that kept me in a trance for more than a month. Hope this proves to be an immediate solution for some and a good idea for the others.
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|