2011-10-06 13 views
3

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

私の質問は、HTMLを印刷しないようにする方法です。 (タグなど)ではなくHTMLがWebブラウザに表示されるようにしますか?

+0

あなたは/ WPF WinFormsのをやっていますか? – sll

+0

uhhhhhhhhhhhhhhhhhhhhhhhh。私のクモの感覚はうずうずしている。ユーザーのデフォルトブラウザ(またはポータブルファイアフォックス)を使用してWebページを起動するのはオプションですか?それ以外の場合は、IEを使用して強制されます。また、IEコントロールを1回使用しました。私はよく知られているウェブサイトを閲覧していましたが、その日にマルウェア広告(http://www.google.com/safebrowsing/diagnostic?site=thesite.comには13kページでは1つしかマルウェアがありません。残念なことにIE6がインストールされて以来、私はウイルスに感染してしまい、本当に迷惑になりました。 –

+0

ウィンドウフォームですが、それはメーターではありません – MoShe

答えて

9

web browser controlを使用し、そのようにそれに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 Article on doing this

+0

このイベントは必須です。 – pbies

+0

しかし、ネットワークプリンタで印刷する必要があるときはどうすればよいですか?ブラウザの印刷メソッドを呼び出すと、必要な任意のプリンタを選択することはできません。 –