【发布时间】:2020-02-03 17:51:28
【问题描述】:
所以一周多来,我一直在寻找解决方案,但无济于事。我有一个程序需要能够打印 htm(l) 文件,但我很难让它遵守。
这是我目前正在使用的代码:
Private Sub HtmlPrinterLaunch(i As Integer)
'Dim htmlWBPrinter As New WebBrowser()
'AddHandler htmlWBPrinter.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf HtmlPrinter)
'htmlWBPrinter.Visible = True
'htmlWBPrinter.ScriptErrorsSuppressed = False
'htmlWBPrinter.Show()
''frmHTMLPrint.wbPrintHtml.AllowNavigation = True
''AddHandler frmHTMLPrint.wbPrintHtml.DocumentCompleted, AddressOf HtmlPrinter
''frmHTMLPrint.wbPrintHtml.Visible = False
''frmHTMLPrint.wbPrintHtml.Navigate("file:///" & IO.Path.GetFullPath(_prints(i).SourcePathFileName))
''Application.Run(frmHTMLPrint)
''Dim appPath As String = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)
''Dim reportPath As String = Path.Combine(appPath, Path.GetFileName(_prints(i).SourcePathFileName))
''htmlWBPrinter.Url = New Uri(reportPath) 'New Uri(Path.Combine("file:///", reportPath)) 'New Uri("file://" & IO.Path.GetFullPath(_prints(i).SourcePathFileName))
'htmlWBPrinter.Url = (New Uri(Path.Combine("file:///" & IO.Path.GetFullPath(_prints(i).SourcePathFileName))))
'While ((htmlWBPrinter.DocumentText = ""))
' Thread.Sleep(10000)
'End While
'htmlWBPrinter.ShowPrintDialog()
'' htmlWBPrinter.Dispose()
Dim wb As New WebBrowser
AddHandler wb.DocumentCompleted, Sub() If wb.ReadyState = WebBrowserReadyState.Complete Then wb.Print()
wb.ScriptErrorsSuppressed = True
Dim url As New Uri(Path.Combine("file:///" & IO.Path.GetFullPath(_prints(i).SourcePathFileName)))
wb.Navigate(url)
End Sub
Private Sub HtmlPrinter(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
state = 4
Dim wbPrinter As WebBrowser = CType(sender, WebBrowser)
wbPrinter.Print()
wbPrinter.Dispose()
'frmHTMLPrint.BeginInvoke(New Action(Sub() frmHTMLPrint.Close()))
End Sub
如您所见,我在那里进行了几次尝试(保留了一些可以工作的旧代码,但我宁愿不使用它,因为我不断收到弹出窗口)
一些可能相关的问题:
-WebBrowser 状态停留在加载(1)
-即使我直接传递,Url 也不会随 file:///path 更新
简而言之,我的代码中的 WebBrowser 控件不会触发 DocumentCompleted 事件,也不会打印出文件。我需要此代码来打印文档而无需用户输入。我在这里错过了什么?
编辑:
所以我又搞砸了一些。我在自己的表单上有 webbrowser 控件,当从主线程调用时,我可以让它加载/打印,但我没有运气调用它。我当前的调用代码:
If wbPrintHtml.InvokeRequired Then
If url.SourcePathFileName = "about:blank" Then
wbPrintHtml.Invoke(CType(Sub()
wbPrintHtml.Navigate(url.SourcePathFileName)
End Sub, MethodInvoker))
Else
wbPrintHtml.Invoke(CType(Sub()
wbPrintHtml.Navigate("file:///" & url.SourcePathFileName)
End Sub, MethodInvoker))
End If
Else
If url.SourcePathFileName = "about:blank" Then
wbPrintHtml.Navigate(url.SourcePathFileName)
Else
wbPrintHtml.Navigate("file:///" & url.SourcePathFileName)
End If
End If
【问题讨论】:
-
不清楚是否要:1) 静默打印文档,2) 显示 Html 页面的预览,然后显示“打印”对话框 3) 仅显示“打印”没有预览的对话框。删除
While ... End While块。同时,将WebBrowserAdvancedFetures类添加到您的项目中,您可以看到here 并在启动时调用ActivateWBAdvancedFeatures方法。 -
@Jimi 抱歉,我没想到要将此添加到问题中。我需要在没有用户提示的情况下打印这些文档(这是一个自动报告创建功能)。我目前在那里的循环只是为了测试一些东西,它实际上并没有在那里停留任何时间,因为 documentText 在循环之前被改变了。
-
好的。删除所有内容并尝试以下操作:
Dim wb As New WebBrowser() AddHandler wb.DocumentCompleted, Sub() If wb.ReadyState = WebBrowserReadyState.Complete Then wb.Print() wb.Navigate([Some URI])。DocumentCompleted处理程序可能需要比这更复杂,因为您可能需要处理多个 IFrame 内容。请参阅注释here。 -
@Jimi 这似乎有同样的问题。我忘记添加到问题中的一件事(将暂时做) wb.url 不会随 file:///path 更新,即使我将它直接传递给它并且页面的 readystate 似乎停留在 Loading (1)
-
我认为除非将 WebBrowser 控件添加到可见容器中,否则事件不会触发。
标签: vb.net