Printing in ASP.NET

One of the most common functionality in any ASP.NET application is to print forms and controls. In this article I am going to show how we can achieve this print functionality in our asp.net application.

One of the most common functionality in any ASP.NET application is to print forms and controls.

In this article, I am going to show how can we achieve this print functionality in our asp.net application. For this, I created a PrintHelper class. In this class, I made a method 'PrintWebControl'. With this method we can print any server control alike GridView, DataGrid, Panel, TextBox etc.

This is my PrintHelper.cs

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.IO;  
  11. using System.Text;  
  12. using System.Web.SessionState;  
  13. /// <summary>  
  14. /// Summary description for PrintHelper  
  15. /// </summary>  
  16. public class PrintHelper  
  17. {  
  18.     public PrintHelper()  
  19.     {  
  20.         //  
  21.         // TODO: Add constructor logic here  
  22.         //  
  23.     }  
  24.     public static void PrintWebControl(Control ctrl)  
  25.     {  
  26.         PrintWebControl(ctrl, string.Empty);  
  27.     }  
  28.     public static void PrintWebControl(Control ctrl, string Script)  
  29.     {  
  30.         StringWriter stringWrite = new StringWriter();  
  31.         System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);  
  32.         if (ctrl is WebControl)  
  33.         {  
  34.             Unit w = new Unit(100, UnitType.Percentage); ((WebControl)ctrl).Width = w;  
  35.         }  
  36.         Page pg = new Page();  
  37.         pg.EnableEventValidation = false;  
  38.         if (Script != string.Empty)  
  39.         {  
  40.             pg.ClientScript.RegisterStartupScript(pg.GetType(), "PrintJavaScript", Script);  
  41.         }  
  42.         HtmlForm frm = new HtmlForm();  
  43.         pg.Controls.Add(frm);  
  44.         frm.Attributes.Add("runat""server");  
  45.         frm.Controls.Add(ctrl);  
  46.         pg.DesignerInitialize();  
  47.         pg.RenderControl(htmlWrite);  
  48.         string strHTML = stringWrite.ToString();  
  49.         HttpContext.Current.Response.Clear();  
  50.         HttpContext.Current.Response.Write(strHTML);  
  51.         HttpContext.Current.Response.Write("<script>window.print();</script>");  
  52.         HttpContext.Current.Response.End();  
  53.     }  
  54. }  
Here, in this application, I created 2 pages. First is the default page of the content that I want to print.On the same page, I set a Print button also. When it will be clicked, it will redirect the user to the second page, named as 'Print.aspx'.

Default.aspx 

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title>Pring in ASP.NET</title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.         <div>  
  10.             <asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="btnPrint_Click" /><br />  
  11.             <asp:Panel ID="pnl1" runat="server">  
  12.                 <table cellpadding="4" cellspacing="4" width="50%" align="center">  
  13.                     <tr>  
  14.                         <td align="center">  
  15.                             Fill all information  
  16.                         </td>  
  17.                     </tr>  
  18.                     <tr>  
  19.                         <td>  
  20.                             <asp:Label ID="Label1" runat="server" Text="Email" Width="130px"></asp:Label>  
  21.                             <asp:TextBox runat="server"></asp:TextBox>  
  22.                         </td>  
  23.                     </tr>  
  24.                     <tr>  
  25.                         <td>  
  26.                             <asp:Label ID="Label2" runat="server" Text="Name" Width="130px"></asp:Label>  
  27.                             <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  28.                         </td>  
  29.                     </tr>  
  30.                     <tr>  
  31.                         <td>  
  32.                             <asp:Label ID="Label6" runat="server" Text="Country" Width="130px"></asp:Label>  
  33.                             <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>  
  34.                         </td>  
  35.                     </tr>  
  36.                     <tr>  
  37.                         <td>  
  38.                             <asp:Label ID="Label3" runat="server" Text="Mobile" Width="130px"></asp:Label>  
  39.                             <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>  
  40.                         </td>  
  41.                     </tr>  
  42.                 </table>  
  43.             </asp:Panel>  
  44.         </div>  
  45.     </form>  
  46. </body>  
  47. </html>

PrintImage1.JPG

Image 1.

 

Click event of 'print' button is

  1. protected void btnPrint_Click(object sender, EventArgs e)  
  2. {  
  3.         Session["ctrl"] = pnl1;  
  4.         ClientScript.RegisterStartupScript(this.GetType(), "onclick""<script language=javascript>window.open('Print.aspx','PrintMe','height=300px,width=300px,scrollbars=1');</script>");  
  5. }  

When, the 'Print' button is clicked,screen will be looking as under in the Image 2

PrintImage2.JPG 

Image 2.

 

Following is the load event of this page, 'Print.aspx'

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     Control ctrl = (Control)Session["ctrl"];  
  4.      PrintHelper.PrintWebControl(ctrl);  
  5. }