Media CSS For Print The Web Page in Customized Format

There are two kinds of CSS used to customize the print and screen look and feel of a web page.

The JavaScript function is used to print the current web page without any server code. The window.print() method is used to print the web page as it is. But if you want to print a portion of the web page then server-side code is needed to do the printing. But the media CSS is used to customize the printing of a page without any server-side logic.
 
<link href="Print.css" type="text/css" rel="Stylesheet" media="print" />

The media has several types and print is the property that applies the CSS for printing the web page. You can write the CSS to customize the web page like font style, background color, foreground color, display and hide the content.

It will not affect the current web page display in the browser and when you take the printout and view the page in the print view then you can look at the media CSS effect.

Let's create the aspx page to display the list of names in the Grid.
  1. <%@Page Language="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="MediaCSS_Default"%>  
  2.   
  3. <!DOCTYPEhtml PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <htmlxmlnshtmlxmlns="http://www.w3.org/1999/xhtml">  
  5. <headidheadid="Head1" runat="server">  
  6.     <title>Untitled Page</title>  
  7.     <linkhreflinkhref="Print.css"type="text/css"rel="Stylesheet"media="print"/>  
  8. </head>  
  9. <body>  
  10.     <formidformid="form1"runat="server">  
  11.     <div>  
  12.         <table width="100%">  
  13.             <trclasstrclass="TrNotDisplay">  
  14.                 <td>  
  15.                     <h3>  
  16.                         CSS for Print out - Example Page</h3>  
  17.                 </td>  
  18.             </tr>  
  19.             <trclasstrclass="TrNotDisplay">  
  20.                 <td>  
  21.                     <astyleastyle="color: blue;" onclick="window.print();">Print</a>  
  22.                 </td>  
  23.             </tr>  
  24.             <tr>  
  25.                 <td>  
  26.                     <asp:GridViewCssClassasp:GridViewCssClass="GridStyle"ID="GridView1"runat="server">  
  27.                     </asp:GridView>  
  28.                 </td>  
  29.             </tr>  
  30.             <trclasstrclass="TrNotDisplay">  
  31.                 <tdstyletdstyle="padding-top: 25px;">  
  32.   
  33.                     Note:  
  34.                     <br/>  
  35.                     Take the print out unless until it is important<br/>  
  36.                     Save the trees  
  37.                 </td>  
  38.             </tr>  
  39.         </table>  
  40.     </div>  
  41.     </form>  
  42. </body>  
  43. </html> 
Let's write the server-side logic to load the data to the grid control.
  1. protected void Page_Load(object sender, EventArgs e)    
  2. {    
  3.     if (!Page.IsPostBack)    
  4.     {    
  5.         BindGrid();    
  6.     }    
  7. }    
  8.     
  9. private void BindGrid()    
  10.     
  11. {    
  12.     GridView1.DataSource = GetDataTable();    
  13.     GridView1.DataBind();    
  14. }    
  15.     
  16. private DataTable GetDataTable()    
  17. {    
  18.     DataTable dTable = new DataTable();    
  19.     DataColumn dColumn = null;    
  20.     DataRow dRow = null;    
  21.     
  22.     dColumn = new DataColumn();    
  23.     dColumn.ColumnName = "SNo";    
  24.     dColumn.AutoIncrement = true;    
  25.     dColumn.AutoIncrementSeed = 1;    
  26.     dColumn.AutoIncrementStep = 1;    
  27.     dTable.Columns.Add(dColumn);    
  28.     
  29.     dColumn = new DataColumn();    
  30.     dColumn.ColumnName = "FirstName";    
  31.     dTable.Columns.Add(dColumn);    
  32.     
  33.     dColumn = new DataColumn();    
  34.     dColumn.ColumnName = "LastName";    
  35.     dTable.Columns.Add(dColumn);     
  36.     
  37.     dColumn = new DataColumn();    
  38.     dColumn.ColumnName = "Address";    
  39.     dTable.Columns.Add(dColumn);    
  40.     
  41.     dColumn = new DataColumn();    
  42.     dColumn.ColumnName = "City";    
  43.     dTable.Columns.Add(dColumn);    
  44.     
  45.     dRow = dTable.NewRow();    
  46.     dRow["FirstName"] ="Senthil";    
  47.     dRow["LastName"] ="kumar";    
  48.     dRow["Address"] ="Bangalore";    
  49.     dRow["City"] ="Bangalore";    
  50.     dTable.Rows.Add(dRow);    
  51.     
  52.     dRow = dTable.NewRow();    
  53.     dRow["FirstName"] ="Vinay";    
  54.     dRow["LastName"] ="kumar";    
  55.     dRow["Address"] ="Adyar";    
  56.     dRow["City"] ="Chennai";    
  57.     dTable.Rows.Add(dRow);    
  58.     
  59.     dRow = dTable.NewRow();    
  60.     dRow["FirstName"] ="Ravi";    
  61.     dRow["LastName"] ="kulkarni";    
  62.     dRow["Address"] ="GST";    
  63.     dRow["City"] ="Mumbai";    
  64.     dTable.Rows.Add(dRow);    
  65.     
  66.     dRow = dTable.NewRow();    
  67.     dRow["FirstName"] ="Sanjay";    
  68.     dRow["LastName"] ="Prasad";    
  69.     dRow["Address"] ="Mettupalayam";    
  70.     dRow["City"] ="Coimbatore";    
  71.     dTable.Rows.Add(dRow);    
  72.     
  73.     dRow = dTable.NewRow();    
  74.     dRow["FirstName"] ="Ravi";    
  75.     dRow["LastName"] ="Ashwin";    
  76.     dRow["Address"] ="Numgambakkam";    
  77.     dRow["City"] ="Chennai";    
  78.     dTable.Rows.Add(dRow);    
  79.     
  80.     return dTable;    
  81. }   
On the design page, we need to mention the CSS type as print. It will apply the style in the style sheet when the only print is applied.
 
Let's create the style sheet for the media - print CSS.
  1. body {  
  2.      background-color: Silver;  
  3.      font-familyTahoma;  
  4.      font-sizemedium;  
  5.      color: Olive;  
  6.      vertical-alignmiddle;  
  7.      text-aligncenter;  
  8. }  
  9.   
  10. .TrNotDisplay {  
  11.      displaynone;  
  12. }  
  13.   
  14. .GridStyle {  
  15.      width100%;  
  16. }  
  17.   
  18. .GridStyle tr th {  
  19.      color: Orange;  
  20.      font-weightbold;  
  21. }  
  22.   
  23. .GridStyle tr td {  
  24.      width70px;  
  25.      background-color: Blue;  

Here I have set the background color and font style properties. The grid view control has the font style and background color.
 
When you run the above web page it will display as designed.
 
Print1.jpg

Here, you can see the output of the web page as designed in the .aspx page.
 
Now, if you apply the print or print view then you will see the output.
 
Print2.jpg
 
Hope this will help you to customize the printout of your web page on the client-side.