views:

442

answers:

3

Hi All,

I am wondering how Internet Explorer, Mozilla Firefox or any other browser generate print preview window of an web page loaded into the browser.

The preview image will have various changes such as banners and adv are removed, will have white background and black text and etc.

We would like implement similar print preview window using C# WebBrowser control and i don't want to use default browser Print preview feature such as ExecWB command or any other.

Please give us some light on this.

Thanks,

Ramanand Bhat.

A: 

As far as I know, the banners, advertisements, et cetera are not removed by the browser during a print preview. CSS governs the appearance when the media is print.

Alan Haggai Alavi
Thank you Alan,But how to invoke "Print" media for loaded web page in webbrowser.
Ramanand Bhat
A: 

To print a screen you need to set up a call to window.print() in javascript.

<a href="javascript:window.print();">Print screen</a>

It will then use whatever css you have assigned as 'print' in the page to render the page as a preview

Steerpike
Thanks, but this javascript will invoke browser default print preview and print module.We would like to construct web page print preview window manually and not using browser print window.
Ramanand Bhat
+2  A: 

You could try to alter the styles by accessing and modifying the HTMLDocument LINK elements.

HtmlDocument document = WebBrowser1.Document;

foreach (HtmlElement element in document.GetElementsByTagName("LINK"))
{
    string cssMedia = element.GetAttribute("Media");


    if (cssMedia == "print")
        element.SetAttribute("Media", "screen"); //sets print styles to display normally
    else
        element.SetAttribute("Media", "hidden"); //hides normal styles
}

This will change your print-styles to display in screen view (i.e. as a normal stylesheet without having to use the print-preview window) and your screen-styles to not be shown (as they don't have a Media type of screen anymore)

This is sample code so doesn't have any error checking. It might also have some syntax errors but it should be a start to achieve your goal.

NickGPS
Thanks Nick, we accept your answer as solution for our query.We found that this solution will be good starting point to start-up with.But as we work with found not all web site pages have 'Media = Print or screen' tag attribute so not all web pages (Google, bing, etc..) render in print preview mode. So how do we support web pages that doesn't consist print media tag to generate print preview bitmap image.Thanks.
Ramanand Bhat
IE has an advanced setting under "Tools>Internet options>Advanced>Printing" that lets you "Print background colors and images". If this is checked, the print version will display everything that is displayed on the screen. The default for this setting is unchecked, which is the behaviour I believe you're after. I would parse the CSS and remove all background color and background image references (in stylesheets and embedded in the html to be sure).
NickGPS