title: How can I print the contents of a WebBrowser control?
body: <p>I found code <a href="http://social.msdn.microsoft.com/Forums/vstudio/en-US/e115cf3b-a6ee-48f3-9cab-f6ad8f6ff3b5/how-to-print-contents-of-webbrowser?forum=wpf" rel="nofollow noreferrer">here</a> that I used like so:</p>
  
  <pre><code>printDialog1.PrintDocument
  (((IDocumentPaginatorSource)webBrowserMap.Document).DocumentPaginator, "Alchemy Map");
  </code></pre>
  
  <p>...but with that I get two err msgs, namely:</p>
  
  <pre><code>0)'System.Windows.Forms.PrintDialog' does not contain a definition for 'PrintDocument' and no extension method 'PrintDocument' accepting a first argument of type 'System.Windows.Forms.PrintDialog' could be found (are you missing a using directive or an assembly reference?)
  
  1) The type or namespace name 'IDocumentPaginatorSource' could not be found (are you missing a using directive or an assembly reference?)
  </code></pre>
  
  <p>I then tried to adapt the code from <a href="http://www.codeproject.com/Articles/60179/Web-Browser-in-C" rel="nofollow noreferrer">here</a> this way:</p>
  
  <pre><code>private void buttonPrint_Click(object sender, EventArgs e)
  {
      WebBrowser wb = getCurrentBrowser();
      wb.ShowPrintDialog();
  }
  
  private WebBrowser getCurrentBrowser()
  {
      return (WebBrowser)tabControlAlchemyMaps.SelectedTab.Controls[0];
  }
  </code></pre>
  
  <p>...but got, "<em>System.InvalidCastException was unhandled
    HResult=-2147467262
    Message=Unable to cast object of type 'System.Windows.Forms.Button' to type 'System.Windows.Forms.WebBrowser'.</em></p>
  
  <p>I then tried to derive from what I found <a href="https://stackoverflow.com/questions/4630391/get-all-controls-of-a-specific-type">here</a>, with this code:</p>
  
  <pre><code>private void buttonPrint_Click(object sender, EventArgs e)
  {
      WebBrowser web = getCurrentBrowser();
      web.ShowPrintDialog();
  }
  
  private WebBrowser getCurrentBrowser()
  {
      // I know there's a better way, because there is only one WebBrowser control on the tab
      foreach (var wb in tabControlAlchemyMaps.SelectedTab.Controls.OfType&lt;WebBrowser&gt;())
      {
          return wb;
      }
      return null;
  }
  </code></pre>
  
  <p>...and, although I can step through it, and it seems to work (I get to the "web.ShowPrintDialog()" line with no err msg), I see no print dialog. So how <em>can</em> I go about printing the contents of a WebBrowser control?</p>