title: Printing from Windows Control embedded in ASP.NET page
body: <p>I am working on an application for which requires the following functionality: The user clicks a button on the web page and this causes a document to be printed without any further user intervention.</p>
  
  <p>The approach I am using to deliver this functionality is to embed a Windows Control (using a Windows Control Library) in the web-page. (A tutorial on doing this is available here.) I worked through a similar tutorial and the Windows Control was properly created and displayed.</p>
  
  <p>The next step was to try actually printing. I first tested the printing code is a stand-alone application to make sure that it functions properly. It does. I have provided a simplified version of that code (see below) for those who are interested.</p>
  
  <p>The specific manner in which the code gets activated is this: This line of code, .Attributes.Add("OnClick", "myPrintFunction();") appears in Page_Load to associate a JavaScript function with the button. This JavaScript function is quite simple:</p>
  
  <pre><code>    function myPrintFunction() {
          var winCtrl = document.getElementById("MyWinControl");
          winCtrl.doMyPrintFunction();
      }
  </code></pre>
  
  <p>The problem is this: The code throws the following error:</p>
  
  <p>mscorlib: Request for the permission of type 'System.Drawing.Printing.PrintingPermission, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' failed.</p>
  
  <p>What permissions do I need to change to make this work? One web-page I found seemed to suggest that setting Trust=Full might help, but it was not clear a) if this would help, or b) how to do this.</p>
  
  <p>Thanks for your help!</p>
  
  <p>Printing code:</p>
  
  <pre><code>Public Class MyWinControl
      Public Sub doMyPrintFunction()
          Dim prtMyDoc As PrintDocument = printerSetup()
          If (Not prtMyDoc Is Nothing) Then prtMyDoc.Print()
      End Sub
  
      Private Function printerSetup() As PrintDocument
          Dim prtMyDoc As New PrintDocument
          AddHandler prtMyDoc.PrintPage, AddressOf prtMyDoc_PrintPage
          prtMyDoc.PrinterSettings.PrinterName = "&lt;my default printer&gt;"
          Return prtMyDoc
      End Function
  
      Private Sub prtMyDoc_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
          ' use graphics native call to build up page to be printed
          ' it is accessed via e.Graphics
      End Sub
  End Class
  </code></pre>