【问题标题】:WebBrowser won't fire DocumentCompeted event or printWebBrowser 不会触发 DocumentCompeted 事件或打印
【发布时间】: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


【解决方案1】:

我终于让它按预期工作了。这不是世界上最漂亮的东西,但它可以发挥作用,我可以接受。

Public Class frmHTMLPrint

Public Shared formHandle As frmHTMLPrint

Private Sub wbPrintHtml_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles wbPrintHtml.DocumentCompleted
    Dim wbPrinter As WebBrowser = CType(sender, WebBrowser)
    If wbPrinter.ReadyState = WebBrowserReadyState.Complete AndAlso Not wbPrinter.Url.ToString() = "about:blank" Then
        wbPrinter.Print()
    End If
End Sub

Shared Function setURL(url As Reporter.ReportServer.PrintMessageType) As Boolean
    If formHandle.wbPrintHtml.InvokeRequired Then
        If url.SourcePathFileName = "about:blank" Then
            formHandle.wbPrintHtml.Invoke(CType(Sub()
                                                    formHandle.wbPrintHtml.Navigate(url.SourcePathFileName)
                                                End Sub, MethodInvoker))
        Else
            formHandle.wbPrintHtml.Invoke(CType(Sub()
                                                    formHandle.wbPrintHtml.Navigate("file:///" & url.SourcePathFileName)
                                                End Sub, MethodInvoker))
        End If
        Dim wbReady As WebBrowserReadyState
        formHandle.wbPrintHtml.Invoke(CType(Sub()
                                                wbReady = formHandle.wbPrintHtml.ReadyState
                                            End Sub, MethodInvoker))
        While ((Not wbReady = WebBrowserReadyState.Complete))
            Application.DoEvents()
            formHandle.wbPrintHtml.Invoke(CType(Sub()
                                                    wbReady = formHandle.wbPrintHtml.ReadyState
                                                End Sub, MethodInvoker))
        End While
        Return wbReady = 4
    Else
        If url.SourcePathFileName = "about:blank" Then
            formHandle.wbPrintHtml.Navigate(url.SourcePathFileName)
        Else
            formHandle.wbPrintHtml.Navigate("file:///" & url.SourcePathFileName)
        End If
        While ((Not formHandle.wbPrintHtml.ReadyState = WebBrowserReadyState.Complete))
            Application.DoEvents()
        End While
        Return formHandle.wbPrintHtml.ReadyState = 4
    End If
End Function

Private Sub frmHTMLPrint_Load() Handles Me.Load
    InitializeComponent()
    Dim wbInitializer As New Reporter.ReportServer.PrintMessageType
    formHandle = Me
    wbInitializer.SourcePathFileName = "about:blank"
    setURL(wbInitializer)
End Sub

结束类

【讨论】:

    猜你喜欢
    • 2021-05-16
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    相关资源
    最近更新 更多