How To Print Part of the page in C#

In this article we will see how to print a portion of a page in C#.

In this article we will see how to print a portion of a page.
 
We are often required to print a particular portion of a page at times that we can not create several pages then redirect them and then print.
 
See the page below to see how it takes effect.
  1. <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"  
  2.     CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>  
  3.   
  4. <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">  
  5.     <script language="javascript">  
  6.         function printPartOfPage(elementId) {  
  7.             var printContent = document.getElementById(elementId);  
  8.             var windowUrl = 'about:blank';  
  9.             var uniqueName = new Date();  
  10.             var windowName = 'Print' + uniqueName.getTime();  
  11.             var printWindow = window.open(windowUrl, windowName, 'left=50000,top=50000,width=0,height=0');  
  12.   
  13.             printWindow.document.write(printContent.innerHTML);  
  14.             printWindow.document.close();  
  15.             printWindow.focus();  
  16.             printWindow.print();  
  17.             printWindow.close();  
  18.         }  
  19.     </script>  
  20. </asp:Content>  
  21. <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">  
  22.  <div id="MyFirstDiv" style="padding: 10 10 10 10; background-color: LightCyan; width: 300px;  
  23.             float: left;" onclick="javascript:printPartOfPage('MyFirstDiv')">  
  24.             C# Corner's mission is to become the world's top online developer community.   
  25.             Each day, we empower millions of developers worldwide by providing the latest unbiased news, advice,   
  26.             and tools for career growth. We’re passionate about nurturing the next young generation of developers  
  27.             so that they can become not only great programmers, but also exceptional human beings.</div>  
  28.         <div id="Div1" onclick="javascript:printPartOfPage('Div1')">  
  29.             <div style="padding: 10 10 10 10; background-color: LightCyan; width: 300px; float: left;  
  30.                 margin-left: 50px;">  
  31.                 C# Corner, headquartered in Philadelphia, PA, is an online global community of 3 million software developers.   
  32.                 We serves 5+ million visitors with 9 million page views each month. We publish the latest news and articles   
  33.                 on cutting-edge software development topics. Developers share their knowledge and connect via content, forums,   
  34.                 and chapters. Thousands of members benefit from our monthly events, webinars, and conferences.   
  35.                 We also provide tools for career growth such as career advice, resume writing, training, certifications,   
  36.                 books and whitepapers, and videos. We also connect developers with their potential employers via out Job board.</div>  
  37.         </div>  
  38. </asp:Content>  
The output for above code is
 
Print in C# 
 
On click, we are calling the JavaScript function to print the div content.
 
Print in C#