简体   繁体   中英

How Internet Explorer Prepare Print Preview window

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.

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 (ie 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.

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM