Monday, November 20, 2006

Removing URL from the footer of a printed document

It's kind of disappointing to know that there are some simple operations that still require the use of APIs and can't be done entirely by using encapsulated .NET framework controls.

Well so far I have tried this:

I want to setup the header and footer when printing a page (notice that I’m not talking about the header and footer of the webpage, which can be solve using a PrintStyleSheet). I’m talking about the header and footer that appears in the Page Setup Dialog (you know the annoying URL)

I'm trying with the IE5. OCX control, via interop, that allows to pass a parameter to the header and footer of the pagesetup, but I don’t want to go back using unmanage code.

Also, I found this solution requires to change the keys entries in the registry. Of course it is necessary to have permission to write in the registry. The basic code is this and needs no explanation

using Microsoft.Win32;

RegistryKey pageKey = Registry.CurrentUser.OpenSubKey("software\\microsoft\\internet explorer\\pagesetup", true);
string newFooter = "";
string newHeader = "";
object currentFooter = pageKey.GetValue("footer");
object currentHeader = pageKey.GetValue("header");
pageKey.SetValue("footerTemp", currentFooter );
pageKey.SetValue("headerTemp", currentHeader);
pageKey.SetValue("footer", newFooter);
pageKey.SetValue("header", newHeader);


Something to remember:

The header and footer entries in the registry are not created yet in the user machine. Windows create those entries the first time the user accesses the PageSetup windows. So if you receive an exception maybe because the code is trying to read entries that are not created yet. You should check first.

It was useful for me at the time I had a WebBrowser2.0 control running inside a windows form (VS2005 )so I could access the registry from my windows application. I have not use this code from a WebPage which require more code and some JavaScript to manipulate the registry but in this link is a sample showing it.

Test it!! and let me know.

No comments: