简体   繁体   English

Internet Explorer如何准备“打印预览”窗口

[英]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. 我想知道Internet Explorer,Mozilla Firefox或任何其他浏览器如何生成加载到浏览器中的网页的打印预览窗口。

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. 我们想使用C#WebBrowser控件实现类似的打印预览窗口,并且我不想使用默认的浏览器打印预览功能,例如ExecWB命令或任何其他功能。

Please give us some light on this. 请给我们一些启示。

Thanks, 谢谢,

Ramanand Bhat. Ramanand Bhat。

You could try to alter the styles by accessing and modifying the HTMLDocument LINK elements. 您可以尝试通过访问和修改HTMLDocument LINK元素来更改样式。

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) 这将更改您的打印样式以在屏幕视图中显示(即作为常规样式表,而不必使用打印预览窗口),并且将不显示您的屏幕样式(因为它们不再具有Media类型的屏幕) )

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. 要打印屏幕,您需要在javascript中设置对window.print()的调用。

<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 然后它将使用您在页面中分配为“打印”的任何CSS来将页面呈现为预览

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 . print介质时,CSS决定外观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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