1

I want to print the background-images through my web browser control:

body {
    background-image: url("image url");
}

To do so, I'm generating the html content and finally try to print the document using:

 webBrowserTest.Print();

While the background image is showing in the run-time in browser, but when printing it does't print. How can I Keep background images during printing?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392

1 Answers1

2

There is a Print Background Colors and Images setting in Page Setup dialog which is shared between Web Browser Control and Internet Explorer.

Page setup settings for Microsoft Internet Explorer are stored in the following registry key:

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup

Print Background Colors and Images value is stored in Print_Background key which can be yes or no. You can change the setting using code, but just keep in mind, these values are system-wide settings and will affect all instances of the WebBrowser control and Internet Explorer for the current user:

using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
    @"Software\Microsoft\Internet Explorer\PageSetup", true))
{
    key.SetValue("Print_Background", "yes", Microsoft.Win32.RegistryValueKind.String);
}

Here is the test html that I used:

<html>
  <head>
    <style>
     body { background-image: url("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"); } 
    </style>
  </head>
  <body>
  </body>
</html>
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Thanks a lot but It didn't work because I do a silent printing, You had helped me a lot to print the HTML silently without dialogs https://stackoverflow.com/a/57233157/418343 , Could U help me please to keep background images based on these circumstances. – Anyname Donotcare Nov 21 '19 at 09:29
  • No problem, The first option (setting the registry key) should work. You just need to make sure you are setting it correctly. – Reza Aghaei Nov 21 '19 at 09:31
  • I need to do it Programmatically, so I copy and paste your block of code in my `PageSetup` method, but it didn't work, I edit my question Could U take a look please – Anyname Donotcare Nov 21 '19 at 09:45
  • 1
    I'll reset the edit which you made on question because it makes the answer nonsense. I'll take a look at the code which is still accessible in edit history of your question. – Reza Aghaei Nov 21 '19 at 09:50
  • Did U find the problem – Anyname Donotcare Nov 21 '19 at 11:15
  • @AnynameDonotcare I tried your code by minimizing the button click event handler to these lines `webBrowser1.Visible = false; SetupPage(); webBrowser1.Print();` and every thing worked as expected. – Reza Aghaei Nov 21 '19 at 17:06
  • Did U try it without the dialog box (silent printing) using your answer https://stackoverflow.com/a/57380227/418343 because I have tried it but still the same problem – Anyname Donotcare Nov 22 '19 at 20:43
  • Yes I tried and it works well. Use `webBrowser1.Visible = false; SetupPage(); webBrowser1.Print();`. – Reza Aghaei Nov 22 '19 at 22:11
  • I use `Windows 10` how to make sure that these registry values are set, because I take the HTML Doc run it in Microsoft Edge and `print` it after running the block of code which set these values, but the document is printed without the watermark! – Anyname Donotcare Nov 24 '19 at 14:37
  • The settings are IE settings not the edge. As a test html document just browse https://www.bing.com and print using exactly with this commmands `webBrowser1.Visible = false; SetupPage(); webBrowser1.Print();` after made sure the document loaded completely. – Reza Aghaei Nov 24 '19 at 14:40
  • Try to test the answer in a clean environment and using a minimal complete verifiable code. I can confirms it's a working as expected. – Reza Aghaei Nov 24 '19 at 14:42
  • I have created a fresh console application and set the configuration method in the Main() method, Run the application, then try to open the HTML in Internet Explorer and try to print it but no watermarks! – Anyname Donotcare Nov 24 '19 at 14:51
  • So check the registry to see if the setting has been applied and make sure you are using exactly the code which is shared in answer and you are browsing https://www.bing.com for test. Also as a result the check box of print background in page setup dialog should be checked. – Reza Aghaei Nov 24 '19 at 14:59
  • I have checked this answer https://stackoverflow.com/a/37565013/418343, and encoded my SVG and I edited my question,so now the watermar has appeared only in the browser (IE)view, but doesn't appear in the printing. (Print background colors and images) option is checked. So I think the registry has changed but this fails to reflect in the printing. – Anyname Donotcare Nov 24 '19 at 15:15
  • I would say the post answrs printing background image but I haven't checked it with encoded svg data url. – Reza Aghaei Nov 24 '19 at 16:12
  • If printing doesn't work in IE it wont work in browser control. If printing works in IE it will work in browser control as well. – Reza Aghaei Nov 24 '19 at 16:17
  • @AnynameDonotcare I checked this answer again and I can confirm if you use a `png`, `jpg` or `bmp` background, everything works fine. But for `svg`, it doesn't work , because it doesn't work in IE. I suggest keep this question specific to background css and images and keep the new post for `svg`, because `svg` may need a totally different solution. – Reza Aghaei Nov 28 '19 at 18:37
  • The problem here is I wanna to make a water mark without changing the layout by adding new divs or whatever so i depended on svg as a background image for the body. – Anyname Donotcare Nov 28 '19 at 18:41
  • I guess you didn't get my point. Try this: `background-image: url("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png");` It doesn't have anything to do with layout. It will not change the layout. – Reza Aghaei Nov 28 '19 at 18:44
  • Why not, just create a png watermark and use it the same way that I showed in above comment. – Reza Aghaei Nov 28 '19 at 18:53
  • Okay i will try a transparent image, but are You sure that u test it as a background-image in css not a src image – Anyname Donotcare Nov 28 '19 at 18:56
  • Yes, this is my test: ` ` – Reza Aghaei Nov 28 '19 at 18:59
  • It's the same thing that I told you one week ago! Anyway, I'm happy that you finally get it worked ;) – Reza Aghaei Nov 28 '19 at 19:05
  • Sorry for confusion I thought it doesn’t print because of the css rule doesn’t work on IE – Anyname Donotcare Nov 28 '19 at 19:07
  • No worries, now you have it working. I may post an answer for the new post, just telling you use png instead of svg, because of limitation in IE. – Reza Aghaei Nov 28 '19 at 19:08