开发者

How to print a web page without using the WebBrowser control

开发者 https://www.devze.com 2023-01-01 17:47 出处:网络
We are using a client application to print out grade sheets from an online application. This has been working flawlessly until the systems were upgraded to IE 8. I now receive this error (Access Deni

We are using a client application to print out grade sheets from an online application.

This has been working flawlessly until the systems were upgraded to IE 8. I now receive this error (Access Denied): http://img707.imageshack.us/img707/5259/62270489.png

Apparently, this is a known issue and it cannot be solved.

How can I print HTML from a WinForms application without using the IE WebBrowser control?

Than开发者_运维问答ks,

Martin Wiboe


A possible solution to an old question:

When printing multiple documents from a WebBrowser.WebBrowser control, with only 1 instance, this can lead to access denied errors.

Use a new variable for each document to print and do not use it globally. In eventhandlers such as WebBrowserDocumentCompletedEventHandler, cast the sender parameter to the webbrowser object.

WebBrowser browser = (WebBrowser)sender;

The access denied error probably occurs when a webbrowser instance is disposed that still has some open resources for printing.

Regards, M.


This "might" work: If you are using Office Automation and the html is basic enough, you could make a request the html and print via word?

Another Idea worth exploring:

Convert the Html content to PDF and save/print that.


hmm interesting error, I've seen this before, I suggest you using Gecko.

Some References:

Embedding Gecko(Mozilla rendering engine) in a .Net application

GeckoFX is a Windows Forms control written in clean, commented C# that embeds the Mozilla Gecko browser control in any Windows Forms Application. It also contains a simple class model providing access to the HTML and CSS DOM.


I was having trouble printing from Skybound's GeckoFx - not WebBrowser - but since Michel van Engelens answer to this question helped me, I thought I might add my solution here. It may well apply to WebBrowser as well.

The most important part is separating the Print call from the DocumentCompleted event - I simply did it with a timer here.

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
    Try
        Dim wb As New Skybound.Gecko.GeckoWebBrowser
        AddHandler wb.DocumentCompleted, AddressOf PrintWebBrowser_DocumentCompleted
        AddHandler wb.HandleCreated, AddressOf PrintWebBrowser_HandleCreated
        wb.CreateControl()
    Catch ex As Exception
        MsgBox(String.Format("Error trying to create GeckoWebBrowser: {0}", ex.Message))
    End Try
End Sub

Private Sub PrintWebBrowser_HandleCreated(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim wb = DirectCast(sender, Skybound.Gecko.GeckoWebBrowser)
    wb.Navigate(GeckoWebBrowser1.Url.AbsoluteUri)
End Sub

Private Sub PrintWebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim wb = DirectCast(sender, Skybound.Gecko.GeckoWebBrowser)
    If wb.Url IsNot Nothing AndAlso wb.Url.AbsoluteUri = "about:blank" Then Exit Sub

    Dim tmr As New Timer
    tmr.Interval = 200
    AddHandler tmr.Tick, AddressOf TimerTick
    tmr.Tag = wb
    tmr.Start()
End Sub

Private Sub TimerTick(ByVal sender As Object, ByVal e As EventArgs)
    Dim tmr As Timer = DirectCast(sender, Timer)
    tmr.Stop()

    Dim wb As Skybound.Gecko.GeckoWebBrowser = Nothing
    Try
        wb = DirectCast(tmr.Tag, Skybound.Gecko.GeckoWebBrowser)
        wb.Window.Print()
    Catch comEx As Runtime.InteropServices.COMException
        Dim hresult As Integer = Runtime.InteropServices.Marshal.GetHRForException(comEx)
        If hresult = &H80004004 Then //' EX_ABORT
            MsgBox("Print cancelled")
        Else
            MsgBox(String.Format("Print failed: {0}", comEx.Message))
        End If
    Catch ex As Exception
        MsgBox(String.Format("Print failed: {0}", ex.Message))
    Finally
        If wb IsNot Nothing Then wb.Dispose()
    End Try
End Sub


I am about to explore the possibility of using a rich text editor to convert it, and then printing the contents of the RTE. I'll let you know if I have success.

0

精彩评论

暂无评论...
验证码 换一张
取 消