WebBrowser control will not print from C #

I have a WebBrowser control in a WinForms application that loads XML, which is converted to HTML / CSS. It looks beautiful if I just want to view it there or in a regular browser.

When the form loads, it should go to the file, and then, when the OnDocumentCompleted event fires, it should set the name of the header, footer and default printer name in the registry, and then call Print (), but for many copies the user specified.

I ran it through the debugger and called webBrowser.Print () the correct number of times in the OnDocumentCompleted event. Nothing is null, which should not be; Visible is true for the WebBrowser control, as is AllowNavigation. And yes, the printer is on and available over the network. The exact same code works elsewhere in the project, but not here.

What else could cause this infernal control to ignore my print commands?

Ideally, I would like all of this to be hidden behind the scenes, as this is designed to work with Windows Scheduler, and users never see it. I read that the control should be visible, however, until I work out this first bend that can wait.

EDIT . The last two lines of my OnDocumentCompleted event handler set DialogResult to OK and closed the form. Removing the Close () call allows you to print, so I assume that it did not get into the print spooler before the form was closed and the WebBrowser control was removed.

Except that you just need to set an arbitrary time limit to wait before closing, is there any way to tell when this is done?

+4
source share
3 answers

It turned out that the problem is with synchronization, when the form closes before it can send the document to the print spooler. I started adding a timer to the form, setting it to 30 seconds, and the Tick () event closes the form. When he closed the form earlier, now he just calls closeTimer.Start ().

+4
source

Try the following:

mshtml.IHTMLDocument2 doc; doc = oWeb.Document; doc.execCommand("Print", True, Nothing); 

FROM EDITING:

You can use SHELL commands to check the status of jobs in the print queue.

+3
source

This is the best way to print HTML from a string.

 void PrintString(string strHTMLText) { WebBrowser wbPrintString = new WebBrowser() { DocumentText = string.Empty }; wbPrintString.Document.Write(strHTMLText); wbPrintString.Document.Title = "Type The Header You Want Here"; Microsoft.Win32.RegistryKey rgkySetting = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Internet Explorer\\PageSetup", true); rgkySetting.SetValue("footer", "Type THe Footer You Want Here"); rgkySetting.Close(); wbPrintString.Parent = this; wbPrintString.ShowPrintPreviewDialog(); wbPrintString.Dispose(); 

}

-1
source

All Articles