6

C# で PrintDocument を使用してファイルを印刷したいと思います。ファイルは単純な HTML です (ファイル内のテキストをページ内の特定の場所に配置する必要があるため、これが必要です)。

私の質問は、HTML 自体 (タグなど) ではなく、Web ブラウザーに表示される HTML を印刷するようにファイルを印刷するにはどうすればよいですか?

4

2 に答える 2

11

Webブラウザコントロールを使用して、次のようにprintメソッドを呼び出します。

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

これを行うためのMSDN記事

于 2011-10-06T15:40:19.663 に答える