In this article I will explain with an example, how to print from Server Side (Code-Behind) in ASP.Net using C# and VB.Net.
 
 

HTML Markup

The HTML Markup consists of following control:
Button – For Printing HTML content.
The Button has been assigned with an OnClick event handler.
<asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="OnPrint" />
 
 

JavaScript function to Print HTML

Inside the PrintHTML JavaScript function, the content to be printed is passed as parameter.
Note: Here the contents to be printed will be sent from Code-Behind.
In the very first line, a variable is initialized where JavaScript window.open function is called and the height and width of the popup window are set.
Then, the contents (received from the Code-Behind) are written to the window using document.write method and the document is closed.
Finally, the window is printed using JavaScript print function.
<script type="text/javascript">
    function PrintHTML(html) {
        // Creating a new window.
        var printWindow = window.open('', '', 'height=400,width=800');
        // Writting the contents of HTML in window.
        printWindow.document.write(html);
        // Closing the document.
        printWindow.document.close();
        // Printing the window.
        printWindow.print();
    }
</script>
 
 

Namespaces

You will need to import the following namespace.
C#
using System.Text;
 
VB.Net
Imports System.Text
 
 

Printing HTML from Code Behind

When the Print Button is clicked, StringBuilder class object is created and using Append method the HTML is generated.
Now, using the string.Format function, a statement to call the PrintHTML JavaScript function is built along with the HTML contents as parameter.
Finally, the statement is registered using the RegisterStartupScript method.
C#
protected void OnPrint(object sender, EventArgs e)
{
    StringBuilder sb = new StringBuilder();
    sb.Append("<div>");
    sb.Append("<span style=' font-weight: bold; font-family: Arial'>Hello,");
    sb.Append("<br />");
    sb.Append("This is <span style='color: #4A0089'>Mudassar Khan</span>.<br />");
    sb.Append("Hoping that you are enjoying my articles!</span>");
    sb.Append("</div>");
    ClientScript.RegisterStartupScript(this.GetType(), "print", string.Format("PrintHTML(\"{0}\");", sb.ToString()), true);
}
 
VB.Net
Protected Sub OnPrint(ByVal sender As Object, ByVal e As EventArgs)
    Dim sb As StringBuilder = New StringBuilder()
    sb.Append("<div>")
    sb.Append("<span style=' font-weight: bold; font-family: Arial'>Hello,")
    sb.Append("<br />")
    sb.Append("This is <span style='color: #4A0089'>Mudassar Khan</span>.<br />")
    sb.Append("Hoping that you are enjoying my articles!</span>")
    sb.Append("</div>")
    ClientScript.RegisterStartupScript(Me.GetType(), "print", String.Format("PrintHTML(""{0}"");", sb.ToString()), True)
End Sub
 
 

Screenshot

Print from Server Side in ASP.Net using C# and VB.Net
 
 

Browser Compatibility

The above code has been tested in the following browsers only in versions that support HTML5.
Microsoft Edge  FireFox  Chrome  Safari  Opera
* All browser logos displayed above are property of their respective owners.
 
 

Demo

 
 

Downloads