none
Problem Printing from WinForms Webbrowser Control

    Question

  • Hi

    I am having the weirdest problem with the WebBrowser control.

    If I use print preview (invoking ShowPrintPreviewDialog()), everything looks fine, but when I hit print (invoking ShowPrintDialog()), the resultant print is cropped by approximately 20-25% on the right hand and bottom sides.

    At first I thought this was something due to a complex html page, but I have tried with a very simple page with almost no formatting instructions and I get the same result.

    What is particularly irritating, is that when I open the page in Edge or Internet Explorer and print from there, the page prints fine.  

    Does anyone have an idea, what I'm doing wrong?

    Thanks

    Jonathan

    Wednesday, May 3, 2017 10:59 AM

All replies

  • Hi Jonathan,

    Thank you for posting here.

    According to your question is more related to winform, I will move it to Windows Forms General forum for suitable support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy                        


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, May 4, 2017 7:25 AM
  • Hi Jonathan,

    >>the resultant print is cropped by approximately 20-25% on the right hand and bottom sides.

    Does the webBrowser show normally when you run your project, I guess not only printing it has issue, but also display with the webBrowser when run the app.

    The Web Browser Control is - by default - perpetually stuck in IE 7 rendering mode. Even though we're now up to IE 11 and a reasonably HTML5 compatible browser, the Web Browser Control always uses the IE 7 rendering engine by default. This is because the original versions of the ActiveX control used this mode and for backwards compatibility the Control continues this outdated and very HTML5 unfriendly default.

    This applies whether you’re using the Web Browser control in a WPF application, a WinForms app, or FoxPro application using the ActiveX control. Behind the scenes all these UI platforms use the same COM interfaces and so you’re stuck with those same rules.

    The good news is there are a couple of ways to override the default rendering behavior:

    1, Using the IE X-UA-Compatible Meta header

    2, Using Application specific FEATURE_BROWSER_EMULATION Registry Keys

    And please refer to Web Browser Control & Specifying the IE Version for more details.

    So you can use IE11 to render the page.

    Or I suggest you can provide some code and screenshot so that I can reproduce it.

    Hope it helps!

    Best Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 4, 2017 9:56 AM
    Moderator
  • Hi Stanly

    Thanks for your reply.

    The quick answer is that the WebBrowser does display the full page with no problem.  Print Preview also shows the full page.  It is just Print that crops.  Screen shots are therefore less than helpful!  I could scan the resultant print if you like??

    I did try using the <meta http-equiv="X-UA-Compatible" content="IE=edge" /> header from the article you linked to.  Whilst this did effect the way the page appeared within the control, it had no effect on the print :-( 

    I was wondering whether it might be DPI related, as I am developing on a high resolution machine.  I am planning to try a test app, with just a single form containing just one WebBrowser control, on an old machine, to see whether that prints normally, but will only be able to do that tonight.  

    It is always possible that it is printer related, but given that both IE and Edge print correctly from the same printer, this is well down the list of possible suspects!

    Normally I would have posted my code in the original question, but since it is so basic, I did not think it would help!  If I can replicate on an old machine, with a simple application, I will though post everything.

    I 'll post again when I have something to report.

    Thanks again

    Jonathan

    Thursday, May 4, 2017 3:13 PM
  • Hi Stanly

    My suspicions were correct: it is a DPI issue. 

    I created a plain one form WinForms app, containing just the WebBrowser, pointing at a simple html page on an old machine.  It worked - print was fine.

    I copied the solution to my new machine, without any changes.  It worked again.

    I added a manifest to set dpiAware to true, and immediately hit my original problem.

    My main application has to be dpiAware.  However, just for the purposes of viewing an html document, I am happy to have the viewer form not dpiAware.  Is there any easy way I can disable dpiAwareness (good word!) for just this form?

    Thanks for your help.

    Jonathan

     

     

    Friday, May 5, 2017 8:06 AM
  • Hi Jonathan,

    Sorry for my late reply.

    >>My suspicions were correct: it is a DPI issue. 

    I may agree it, as we all know, the computer display some picture or another elements based on the pixel but the printer is based on dpi to print them, so this may cause the issue.

    >>Is there any easy way I can disable dpiAwareness (good word!) for just this form?

    What way do you use to add the .manifest file? I guess you add it to your whole application, so it works for all forms.

    I suggest you can add a folder to store the form within webbrowser, means adding an extra namespace to this form.

    Hope it helps!

    Best Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 12, 2017 3:15 AM
    Moderator
  • Hi Stanly

    As you should know, the manifest file affects all files/forms in the application.  It does not even help to move the form into a separate library (with its own namespace and its own manifest marked as dpi aware false); as soon as the library is reference by the application, it inherits the application's dpi settings (unless you know a way to avoid this???).

    Have you replicated the problem - given the information I have given that should not be hard? If so, have you found a workaround?  If not, please do not propose your own answer as the answer.  So far the only "answer" that should be proposed is mine, confirming that the problem is dpi.  Unless you know a workaround, then this is a bug in the WebBrowser control, since the problem goes away, if you open the html page with a "real" browser.

    Monday, May 15, 2017 8:52 AM
  • Hi Jonathan,

    I'm not sure if you have resolved this issue yet or not. However, I just recently ran across it myself in our application. I found the issue to be due to DPI awareness or lack of in the WebBrowser control. To resolve this I ended up doing two things. Computing the zoom factor based off of the current DPI divided by 96 (standard) and then setting the zoom on the page using the factor. Although that did not resolved the margins being cut off, it did make the font readable when printing. I then added a margin of 20px to the right and bottom margins. This seemed to do the trick. Here is my code in VB.Net:

    Private Sub WebBrowser1_DocumentCompleted(sender AsObject, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

    If FactorX <> 1 Then

        mintZoomFactor = 100 * FactorX

        WebBrowser1.Document.Body.Style =

    "zoom:"+ mintZoomFactor.ToString() + "%; margin-right: 20px; margin-bottom: 20px"

    End If

    End Sub


    • Edited by Fred Sher Wednesday, December 6, 2017 8:23 PM
    Wednesday, December 6, 2017 8:22 PM