Procedura: stampare con un controllo WebBrowser

Aggiornamento: novembre 2007

Nell'esempio di codice riportato di seguito viene illustrato come utilizzare il controllo WebBrowser per stampare una pagina Web senza visualizzarla.

Esempio

Private Sub PrintHelpPage()

    ' Create a WebBrowser instance. 
    Dim webBrowserForPrinting As New WebBrowser()

    ' Add an event handler that prints the document after it loads.
    AddHandler webBrowserForPrinting.DocumentCompleted, New _
        WebBrowserDocumentCompletedEventHandler(AddressOf PrintDocument)

    ' Set the Url property to load the document.
    webBrowserForPrinting.Url = New Uri("\\myshare\help.html")

End Sub

Private Sub PrintDocument(ByVal sender As Object, _
    ByVal e As WebBrowserDocumentCompletedEventArgs)

    Dim webBrowserForPrinting As WebBrowser = CType(sender, WebBrowser)

    ' Print the document now that it is fully loaded.
    webBrowserForPrinting.Print()
    MessageBox.Show("print")

    ' Dispose the WebBrowser now that the task is complete. 
    webBrowserForPrinting.Dispose()

End Sub
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();
}

Compilazione del codice

L'esempio presenta i seguenti requisiti:

  • Riferimenti agli assembly System e System.Windows.Forms.

Vedere anche

Attività

Procedura: passare a un URL con il controllo WebBrowser

Procedura: aggiungere funzionalità del browser Web a un'applicazione Windows Form

Procedura: creare un visualizzatore di documenti HTML in un'applicazione Windows Form

Concetti

Protezione dei controlli WebBrowser

Riferimenti

WebBrowser

Print

Url

Cenni preliminari sul controllo WebBrowser