0

Currently, I'm printing the contents of a WPF WebBrowser like so:

mshtml.IHTMLDocument2 doc = WebBrowser.Document as mshtml.IHTMLDocument2;
doc.execCommand("Print", true, null);

My HTML content has tables with background colors. Currently, when I print the content, the background colors do not print -- everything is solid white. Is there a way to tell the WebBrowser to print the background colors as well?

Also, this still causes a print dialog to pop up. Does anyone know what the command is to print the contents dialog-less?

Thanks a lot!

JToland
  • 3,630
  • 12
  • 49
  • 70

1 Answers1

1

Assuming you're using 'SHDocVw.WebBrowser', you can use the ExecWB command. To print without the dialog, use the OLECMDEXECOPT_PROMPTUSER (1) constant. You can also pass an IE print template (just an HTML file) for more control over how the page is displayed.

It's something like this (taken from this MSDN question)

browser.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT,
               SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, 
               "print_template.html", ref nullObject);

As for the background, it appears to be one of the options you can specify in the print template's LayoutRect. All print dialog settings are stored in the registry, but a print template is preferable because it won't change system-wide settings.

Courtney Christensen
  • 9,165
  • 5
  • 47
  • 56
  • 1
    I wasn't. I was using "System.Windows.Controls.WebBrowser" in which using ExecWB will not work. It appears, after much searching, that printing without a dialog from a _WPF_ WebBrowser is danged near impossible mostly due to it's being basically a poor wrapper around the WinForms WebBrowser. – JToland Jun 13 '11 at 20:08
  • @JToland, thanks for the update, as unfortunate as that may be. [How?] were you able to resolve your issues? – Courtney Christensen Jun 13 '11 at 21:16
  • Not truly. I simply switched to using a WinForms WebBrowser control and then used a bit of a work-around hack to enable putting dialog's on "top of it" (since a WinForms WebBrowser in a Windows Forms Host control doesn't obey z-order). Actually, I just not take an 'image' of the current screen, hide the WebBrowser, show the image in it's place, then put the dialog over that to make it appear like the dialog just opened on top of the current screen. – JToland Jun 14 '11 at 12:50