1

My goal is to load up a file (htm file) in a WebBrowser control on my form. Once I have the htm file loaded I want to print it with the default printer settings.

enter image description here

Currently I can do everything above. The one thing that I can NOT do is give the print job a name. It just uses the default "file:///C:/Users/blah/blah/myfilename.htm" for the title of the print job. This is what I want to change.

Is this possible via the WebBrowser control or via the print spooler (changing the job title after the fact)?

Arvo Bowen
  • 4,524
  • 6
  • 51
  • 109

2 Answers2

0

According to the MSDN on WebBrowser.Print()...

Prints the document currently displayed in the WebBrowser control using the current print and page settings.

So, it is a matter of changing the WebBrowser print settings. According to Microsoft, that is not allowed without changing the registry on the fly.

As a potential starting point for a workaround, there is a way to get the default PrinterSettings and set the PrintFileName like so:

PrinterSettings settings = new PrinterSettings();
settings.PrintFileName = "YourFileNameHere";

If there were a way to then assign those printer settings to your WebBrowser object, that'd be swell! Sadly, that cannot be done. But, you may be able to use the above code to engineer another solution, likely outside the scope of WebBrowser.

Community
  • 1
  • 1
Chase
  • 934
  • 6
  • 18
  • I have been going back and forth with this over and over again. I'm having no luck with any of it to accomplish changing the name of the print job. Using the PrinterSetting object to set PrintFileName is nothing that would help because that method is used to print to a file (not a printer). Thinking it might still affect the name of the print job I tried it anyway with no success. As far as the registry goes, the only settings I could find were font, footer, header, margins, print background, and shrink to fit. Nothing to do with naming the document. :/ – Arvo Bowen Feb 02 '16 at 15:25
0

This is the best way i fount to print an 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();
}