如何在vb.net windows应用程序中打印html页面
本文关键字:打印 html 页面 应用程序 windows vb net | 更新日期: 2023-09-27 18:24:39
我正在使用vb.net开发一个windows应用程序,我有一个简单的html页面,其中有占位符,我将页面加载到流读取器中,替换占位符,然后我需要打印html内容,任何人都知道如何将html内容打印为html而不是源。vb.bet或c#中的P.S.代码可以。感谢
您可以使用WebBrowser
控件来执行此操作。它将允许您在WinForms
中显示HTML。
DocumentText属性将允许您设置一个表示要显示的HTML的字符串。
例如:
webBrowser.DocumentText = "<html><body><p>I like StackOverflow</p><body></html>";
之后,如果您想打印页面,您必须等到Document完成并调用WebBrowser
的Print
方法。MSDN展示了一种简单的方法:
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();
}
您还应该考虑尝试使用方法PrintDialog
,以确保问题不是您的打印配置。
以下是MSDN的链接:使用MSDN 上的WebBrowser控件打印
可能重复:打印WebBrowser控件内容
我使用下一个代码。我在更改页面方向时遇到了一个大问题,唯一有效的解决方案是在默认打印机上更改页面设置,直接使用注册表破解。
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
在这个页面上,我发现了一个非常容易实现的非常紧凑的解决方案:
Dim printProcess As New Diagnostics.ProcessStartInfo()
printProcess.FileName = "YOUR_FILE_HERE.html"
printProcess.Verb = "print"
'printProcess.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(printProcess)
希望它对本页面的未来访问者有用!
对于那些可能觉得这个简单的VB代码很有用的人,我打印我的html模板文档如下:
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