Я использую следующий код.Большая проблема у меня была с изменением ориентации страницы, единственное рабочее решение - изменить настройки страницы на принтере по умолчанию, используется прямой взлом реестра.
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