Печать содержимого элемента управления WebBrowser

StackOverflow https://stackoverflow.com/questions/610183

  •  03-07-2019
  •  | 
  •  

Вопрос

Я абсолютно новичок в печати в .NET. Я хотел бы напечатать страницу, которая отображается в элементе управления WebBrowser. Как мне это сделать?

Это было полезно?

Решение

В MSDN есть статья на эту тему, однако пример кода демонстрирует, как использовать элемент управления WebBrowser для печати веб-страницы без ее отображения.

Как печатать с помощью элемента управления WebBrowser

Код C #:

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();
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top