简体   繁体   English

打印网页而不显示“打印”对话框

[英]Print Web Page without showing Print Dialog

I want to print webpage using C# without showing Printer Dialog box. 我想使用C#打印网页而不显示“打印机”对话框。 I want to provide the url and it should print the page as displayed. 我想提供URL,它应该按显示的那样打印页面。 Currently my code is printing HTML code. 目前,我的代码正在打印HTML代码。 Please guide me to do so. 请指导我这样做。

This sounds trickier than it is (most probably). 听起来比实际要棘手(很可能)。 My understanding is that you would need some sort of an engine in order to translate all webpage's styles, layout and paintings. 我的理解是,您需要某种引擎才能翻译所有网页的样式,布局和绘画。 and just after that render whatever is the final result. 然后就渲染最终结果。 Keeping this in mind one things pops up immediately: browser automation and in particular - selenium could do the job. 请牢记这一点,马上就会弹出一件事:浏览器自动化,尤其是-硒可以完成这项工作。 Since you asked without showing Printer Dialog box, you can run in headless mode Still I think your best bet is to have a Print Preview page loaded via js.ExecuteScript("setTimeout(window.print, 5);"); 由于您没有显示打印机对话框而被询问,因此您可以在无头模式下运行。我仍然认为最好的选择是通过js.ExecuteScript("setTimeout(window.print, 5);");加载打印预览页面js.ExecuteScript("setTimeout(window.print, 5);"); ( reference) and then carefully locate the print button with waiting handlers. 参考) ,然后小心地找到带有等待处理程序的打印按钮

PS. PS。 I don't believe this is the cleanest and easiest solutions out there, however it's the only one I can currently think of. 我不认为这是最干净,最简单的解决方案,但这是我目前唯一想到的解决方案。

I successfully do this by these lines. 我通过这些行成功地做到了这一点。 But the issue is that this only prints to default printer. 但是问题在于,这只会打印到默认打印机。 I know to set the default printer but I am looking for solution where I pass printer name and it print the webpage without setting it to default printer first. 我知道要设置默认打印机,但是我正在寻找通过打印机名称传递解决方案的解决方案,它无需先将其设置为默认打印机即可打印网页。

ie.Navigate(URL, ref missing, ref missing, ref missing, ref missing);
while (!documentLoaded && ie.QueryStatusWB(OLECMDID.OLECMDID_PRINT) != OLECMDF.OLECMDF_ENABLED)
    ie.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref missing, ref missing);

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

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