2

I am developing a windows application using vb.net, I have a simple html page with place holders in it, I load the page into a stream reader, replace the place holders and then I need to print the html content, anybody have any idea how to print the html content as html and not source. P.S. code in vb.bet or c# is ok. Thanks

Alex
  • 5,971
  • 11
  • 42
  • 80

4 Answers4

6

You can use the WebBrowser control to do so. It will allow you to show HTML inside your WinForms.

The DocumentText proprety will allow you to set a String that represent the HTML you want to show.

For example:

webBrowser.DocumentText = "<html><body><p>I like StackOverflow</p><body></html>";

Afterward if you want to print the page, you'll have to wait until the Document is completed and call the Print method of the WebBrowser. MSDN shows an easy way to do it:

private void PrintHelpPage()
{
    // Create a WebBrowser instance. 
    WebBrowser webBrowserForPrinting = new WebBrowser();

    // Add an event handler that prints the document after it loads.
    webBrowserForPrinting.DocumentCompleted +=
        new WebBrowserDocumentCompletedEventHandler(PrintDocument);

    // Set the Url property to load the document.
    webBrowserForPrinting.Url = new Uri(@"\\myshare\help.html");
}

private void PrintDocument(object sender,
    WebBrowserDocumentCompletedEventArgs e)
{
    // Print the document now that it is fully loaded.
    ((WebBrowser)sender).Print();

    // Dispose the WebBrowser now that the task is complete. 
    ((WebBrowser)sender).Dispose();
}

You should also consider trying to use the method PrintDialog to make sure the issue is not your printing configuration.

Here is the link to MSDN: Print with a WebBrowser control on MSDN

Possible duplicate: Printing WebBrowser control content

Community
  • 1
  • 1
Alexandre
  • 4,382
  • 2
  • 23
  • 33
  • I tried this but when trying to call webbroswer1.print nothing is happening. – Alex Feb 14 '12 at 14:50
  • You mean you want to physically print the HTML you rendered? – Alexandre Feb 14 '12 at 15:03
  • I edited my answer to reflect your question, it is strongly recommended to add Code Snippets of what you already have tried and so on to your question. Also, I found several duplicate of your question on StackOverflow, you should consider being more thorough with your search next time. – Alexandre Feb 14 '12 at 15:16
  • @PeekaySwitch am investigating into this to see if i can set the printer name instead of using windows default printer. Any idea? – A Khudairy Feb 12 '15 at 14:41
0

I use the next code. Big problem I had with change page orientation, only working solution is to change page settings on default printer, direct registry hack is used.

Sub printHTMLFile (FileName As String, Portrait As Boolean, Copies As Integer)
  Const PAGESET_KEY As String = "Software\Microsoft\Internet Explorer\PageSetup"

  If Copies < 1 Then Exit Sub
  Dim MyKey As RegistryKey = Registry.CurrentUser.OpenSubKey (PAGESET_KEY, True)
  Dim TempFooter As String = MyKey.GetValue ("footer").ToString ()
  Dim TempHeader As String = MyKey.GetValue ("header").ToString ()
  Dim TempBottom As String = MyKey.GetValue ("margin_bottom").ToString ()
  Dim TempLeft   As String = MyKey.GetValue ("margin_left").ToString ()
  Dim TempRight  As String = MyKey.GetValue ("margin_right").ToString ()
  Dim TempTop    As String = MyKey.GetValue ("margin_top").ToString ()
  MyKey.SetValue ("footer", String.Empty)
  MyKey.SetValue ("header", String.Empty)
  MyKey.SetValue ("margin_bottom", "0.40000")
  MyKey.SetValue ("margin_left",   "0.40000")
  MyKey.SetValue ("margin_right",  "0.40000")
  MyKey.SetValue ("margin_top",    "0.40000")
  MyKey.Close ()
  pageSet (Portrait)

  Dim WB As WebBrowser = New WebBrowser ()
  WB.Navigate (FileName)
  While WB.ReadyState <> WebBrowserReadyState.Complete
    'Thread.Sleep (100)
    Application.DoEvents ()
  End While
  For i As Integer = 1 To Copies
    WB.Print ()
  Next

  MyKey = Registry.CurrentUser.OpenSubKey (PAGESET_KEY, True)
  MyKey.SetValue ("footer",        TempFooter)
  MyKey.SetValue ("header",        TempHeader)
  MyKey.SetValue ("margin_bottom", TempBottom)
  MyKey.SetValue ("margin_left",   TempLeft)
  MyKey.SetValue ("margin_right",  TempRight)
  MyKey.SetValue ("margin_top",    TempTop)
  MyKey.Close ()
End Sub

Sub pageSet (Portrait As Boolean)
' page orientation settins on default printer
  Const DEVICE_KEY = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows"
  Const DEVMODE_KEY = "HKEY_CURRENT_USER\Printers\DevModePerUser"
  Const DEFAULT_DEVMODE_KEY = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers\"

  Dim DevStr As String = Registry.GetValue (DEVICE_KEY, "Device", String.Empty)
  Dim PrinterName As String = DevStr.Substring (0, (DevStr.IndexOf (",")))

  Dim DevMode() As Byte = Registry.GetValue (DEVMODE_KEY, PrinterName, Nothing)
  If DevMode Is Nothing Then
    DevMode = Registry.GetValue (DEFAULT_DEVMODE_KEY & PrinterName.Replace ("\"c, ","c), "Default DevMode", Nothing)
  End If
  If Portrait Then
    DevMode(76) = 1
  Else
    DevMode(76) = 2
  End If
  Registry.SetValue (DEVMODE_KEY, PrinterName, DevMode)
End Sub
0

On this page, I found a great compact solution that's really easy to implement:

Dim printProcess As New Diagnostics.ProcessStartInfo()
printProcess.FileName = "YOUR_FILE_HERE.html"
printProcess.Verb = "print"
'printProcess.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(printProcess)

Hope its useful to future visitors of this page!

user3470185
  • 311
  • 2
  • 2
0

For those who may find this simple VB code helpful, I print my html template documents like this:

Dim PrintWebBrowser As New WebBrowser
AddHandler PrintWebBrowser.DocumentCompleted, AddressOf DocumentCompleted
PrintWebBrowser.DocumentText = "<!DOCTYPE html PUBLIC etc. etc. </html>"    

Private Sub DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    With DirectCast(sender, WebBrowser)
        If .ReadyState = WebBrowserReadyState.Complete Then
            .Print()
        End If
    End With
End Sub
Ray E
  • 134
  • 1
  • 9