【发布时间】:2012-03-05 22:03:38
【问题描述】:
我正在使用 vb.net 开发一个 Windows 应用程序,我有一个带有占位符的简单 html 页面,我将页面加载到流阅读器中,替换占位符,然后我需要打印 html 内容,任何人知道如何将 html 内容打印为 html 而不是源代码。 附言vb.bet 或 c# 中的代码是可以的。 谢谢
【问题讨论】:
标签: c# .net html vb.net winforms
我正在使用 vb.net 开发一个 Windows 应用程序,我有一个带有占位符的简单 html 页面,我将页面加载到流阅读器中,替换占位符,然后我需要打印 html 内容,任何人知道如何将 html 内容打印为 html 而不是源代码。 附言vb.bet 或 c# 中的代码是可以的。 谢谢
【问题讨论】:
标签: c# .net html vb.net winforms
您可以使用WebBrowser 控件来执行此操作。它将允许您在 WinForms 中显示 HTML。
DocumentText 属性将允许您设置一个表示您要显示的 HTML 的字符串。
例如:
webBrowser.DocumentText = "<html><body><p>I like StackOverflow</p><body></html>";
之后,如果要打印页面,则必须等到文档完成并调用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 的链接: Print with a WebBrowser control on MSDN
【讨论】:
我使用下一个代码。我在更改页面方向时遇到了大问题,唯一可行的解决方案是更改默认打印机上的页面设置,使用直接注册表破解。
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
【讨论】:
在this 页面上,我发现了一个非常易于实施的紧凑型解决方案:
Dim printProcess As New Diagnostics.ProcessStartInfo()
printProcess.FileName = "YOUR_FILE_HERE.html"
printProcess.Verb = "print"
'printProcess.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(printProcess)
希望它对这个页面的未来访问者有用!
【讨论】: