I want to print webpage using C# without showing Printer Dialog box. I want to provide the url and it should print the page as displayed. Currently my code is printing HTML code. Please guide me to do so.
-
Simply stated: If you want a html page to be displayed (or printed with the same look as displayed), you need a web browser, not C#. Or you need to call a browser from C#. – Al Kepp Jan 24 '19 at 20:51
-
Thank you for your response. – Adeel Kamran Jan 24 '19 at 20:54
-
Right, I need guidance to call browser from C# to print the web page as it is displayed. – Adeel Kamran Jan 24 '19 at 20:54
-
Any luck to help ? – Adeel Kamran Jan 24 '19 at 21:30
-
@AdeelKamran what progress have you made so far? – noobed Feb 15 '19 at 10:56
2 Answers
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);");
(reference)
and then carefully locate the print button with waiting handlers.
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.

- 1,329
- 11
- 24
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);

- 138,499
- 22
- 57
- 95

- 103
- 7