0

I currently have a WebBrowser control in my VB.NET project being created below:

Private Sub SomeSubToPrintHTMLViaWebBrowser()

   ' strDocument is the giant blob of HTML text that can be seen in the jsFiddle linked later in the question.

    Dim webBrowserHidden As New WebBrowser

    AddHandler webBrowserHidden.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PrintDocument)

    webBrowserHidden.DocumentText = strDocument

End Sub

However when I use the .Print or .ShowPrintDialog methods of the WebBrowser, the page is coming out malformed even though when I load the HTML coding as a webpage in either IE, Edge, Chrome, or Firefox, it works perfectly fine. The coding was also validated as "proper" by the W3C Online Validator.

So what I would like to know is, what engine is WebBrowserusing to render pages?

Here is the HTML/CSS coding that I'm trying to run

https://jsfiddle.net/et1t2kh5/

Paul Williams
  • 1,554
  • 7
  • 40
  • 75
  • 1
    It uses the Trident engine, which powers IE. The WebBrowser control is a .NET host for the InternetExplorer COM control. – Sam Axe Jul 06 '16 at 18:55
  • I'm assuming there is no easy way for me to have the `.Print()` method rendered via another browser? Say Chrome or others? At this point I might just say screw the `
    ` tables and use regular `` instead.
    – Paul Williams Jul 06 '16 at 19:04
  • 1
    Paul, you could always spin up a new Chrome instance and pass the URL to it. Other than that, not so much. – Sam Axe Jul 07 '16 at 18:46

1 Answers1

1

This is probably because the WebBrowser control is emulating an older version of IE. Unfortunately, there's no easy fix for this and the workaround requires that you modify the registry.

Using the Registry Editor (regedit.exe) navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

Add a new DWORD entry, where the name will be the name of your application's executable and then set the value to 2af8 (hex) or 11000 (dec).

This will force the WebBrowser control to use IE11's rendering engine.

Please refer to the following link for further information: Internet Feature Controls (B..C)

xfx
  • 1,329
  • 8
  • 18
  • In hindsight this doesn't seem to be much as I can write a directive to modify the registry. When writing, do I need just the .exe name or the full path plus the exe name? – Paul Williams Jul 07 '16 at 18:57
  • 1
    Just the EXE. For example, if your application name is "MyApp", then you just need to add an entry for "MyApp.exe". If you're using VisualStudio, remember to also add the vhost version of your EXE that VS creates for debugging purposes, for example: "MyApp.vshost.exe" – xfx Jul 07 '16 at 19:00
  • And obviously when I deploy the app, its just the .exe. got it. – Paul Williams Jul 07 '16 at 19:03
  • @PaulWilliams, you don't have to use `HKLM`. Check [this](http://stackoverflow.com/a/18333982/1768303). – noseratio Jul 07 '16 at 19:51
  • Yes, you can use HKLU which would apply the changes to the current user, but HKLM ensures that the feature changes will work under any account. – xfx Jul 07 '16 at 21:31
  • So question, am I doing this at runtime or is this something I'd have to do in the app's setup functionality? Preferably, I'd like to avoid making permanent changes to a registry if possible. – Paul Williams Jul 15 '16 at 07:19
  • I'd do it at the setup process. And because the changes will only apply to your particular EXE, they won't have any adverse effects on the target machine. – xfx Jul 15 '16 at 10:03
  • So HKLM it is then. – Paul Williams Jul 16 '16 at 07:59