Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF WebBrowser: changing IE print dialog properties programmatically

I'm writing application with WPF WebBrowser control. It's source is result of xml/xslt sourse from database.

In the window that contains WebBrowser there is button for printing with handler:

mshtml.IHTMLDocument2 doc = WBrowser.Document as mshtml.IHTMLDocument2;
doc.execCommand("Print", true, 0);

but in this case there is no background in printed document. I've researched this issue, and it's trouble with property in Internet Explorer page setup dialog - Allow the printing of background colors and images.

I've tried to change this by this code:

RegistryKey regKey = Registry.CurrentUser
        .OpenSubKey("Software", true)
        .OpenSubKey("Microsoft", true)
        .OpenSubKey("Internet Explorer", true)
        .OpenSubKey("PageSetup", true);

var defaultValue = regKey.GetValue("Print_Background");
regKey.SetValue("Print_Background", "yes");

but this is bad code. I don't want to change registry values for one simple bool parameter.

So, my question is: how can I change this parameter programmatically via code-behind without registry modification?

Thank you!

like image 313
Max Zhukov Avatar asked Nov 16 '14 18:11

Max Zhukov


2 Answers

The only way to modify the print settings without modifying the registry is via Print Templates and it seems like no one has really used them from the .NET web browser control.

However, this answer has additional resources regarding print templates and their usage from C++/win32.

If you're open to using ActiveX for printing the page, you could use the SHDocVw.WebBrowser which then gives you access to the ability to specify the print template, as noted in this answer

like image 100
zastrowm Avatar answered Nov 10 '22 16:11

zastrowm


You can't do this without the registry modification.

like image 35
shfire Avatar answered Nov 10 '22 17:11

shfire