C# 如何在vb.net windows应用程序中打印html页面

C# 如何在vb.net windows应用程序中打印html页面,c#,.net,html,vb.net,winforms,C#,.net,Html,Vb.net,Winforms,我正在用vb.net开发一个windows应用程序,我有一个带有占位符的简单html页面,我将页面加载到流阅读器中,替换占位符,然后我需要打印html内容,任何人都知道如何将html内容打印为html而不是源代码。 vb.bet或c#中的P.S.代码可以。 谢谢您可以使用WebBrowser控件来执行此操作。它将允许您在WinForms中显示HTML DocumentText属性允许您设置表示要显示的HTML的字符串 例如: webBrowser.DocumentText = "<html

我正在用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>";

您还应该考虑使用方法<代码> PrimTrime以确保问题不是您的打印配置。

以下是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
子printHTMLFile(文件名为字符串,纵向为布尔值,副本为整数)
Const PAGESET_KEY As String=“Software\Microsoft\internetexplorer\PageSetup”
如果拷贝数<1,则退出Sub
Dim MyKey As RegistryKey=Registry.CurrentUser.OpenSubKey(页面集\键,真)
Dim TempFooter As String=MyKey.GetValue(“页脚”).ToString()
Dim TempHeader As String=MyKey.GetValue(“header”).ToString()
Dim TempBottom As String=MyKey.GetValue(“margin_-bottom”).ToString()
Dim TempLeft As String=MyKey.GetValue(“左边距”).ToString()
Dim TempRight As String=MyKey.GetValue(“右边距”).ToString()
Dim TENTOP As String=MyKey.GetValue(“margin_top”).ToString()
MyKey.SetValue(“页脚”,String.Empty)
MyKey.SetValue(“标题”,String.Empty)
MyKey.SetValue(“边距\底部”,“0.40000”)
MyKey.SetValue(“左边距”,“0.40000”)
MyKey.SetValue(“右边距”、“0.40000”)
MyKey.SetValue(“边距顶部”、“0.40000”)
MyKey.Close()
页面集(纵向)
作为WebBrowser的尺寸WB=新WebBrowser()
WB.Navigate(文件名)
而WB.ReadyState WebBrowserReadyState.Complete
'Thread.Sleep(100)
Application.DoEvents()
结束时
对于i,作为整数=1到个副本
WB.Print()
下一个
MyKey=Registry.CurrentUser.OpenSubKey(页集\项,真)
MyKey.SetValue(“页脚”,TempFooter)
MyKey.SetValue(“header”,TempHeader)
MyKey.SetValue(“边距底部”,临时底部)
MyKey.SetValue(“左边距”,TempLeft)
MyKey.SetValue(“右边距”,TempRight)
MyKey.SetValue(“边距顶部”,TENTOP)
MyKey.Close()
端接头
子页面集(纵向为布尔值)
'在默认打印机上设置页面方向
Const DEVICE\u KEY=“HKEY\U CURRENT\u 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(设备_键,“设备”,String.Empty)
Dim PrinterName As String=DevStr.Substring(0,(DevStr.IndexOf(“,”))
Dim DevMode()为Byte=Registry.GetValue(DevMode_键,PrinterName,Nothing)
如果DevMode为Nothing,则
DevMode=Registry.GetValue(默认的DevMode键和PrinterName.Replace(“\”c,“,”c),“默认的DevMode”,无)
如果结束
如果是肖像的话
DevMode(76)=1
其他的
DevMode(76)=2
如果结束
Registry.SetValue(DEVMODE_键、PrinterName、DEVMODE)
端接头
在第页,我发现了一个非常简单的解决方案:

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

希望对本页面的未来访问者有用

我尝试了这个方法,但在尝试调用webbroswer1.print时,什么都没有发生。你的意思是想实际打印你呈现的HTML吗?我编辑了我的答案以反映你的问题,强烈建议在你的问题中添加你已经尝试过的代码片段等等。另外,我发现了几个关于StAdvExcel的问题的副本,您应该考虑下一次对搜索更加彻底。@ PykaySwitter正在调查这个问题,看看我是否可以设置打印机名称而不是使用Windows默认打印机。有什么想法吗?
Dim printProcess As New Diagnostics.ProcessStartInfo()
printProcess.FileName = "YOUR_FILE_HERE.html"
printProcess.Verb = "print"
'printProcess.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(printProcess)