Home
    Shop
    Advertise
    Write For Us
    Affiliate
    Newsletter
    Contact

Printing in ASP.NET

Very common tasks, needed in almost any kind of application are reporting and printing. Before or later you will need to provide a print option to your users. In Windows Forms applications print issues are better defined, but when using ASP.NET, printing of web form is not that easy.

 

There is a few questions, first, do you want printing on server side or on client side. If you want to print on server side, then you can use System.Drawing.Printing classes just like when you work with .Net Windows Forms application. But, if you try to enable printing facility on client side, you need to consider more constraints and different possible solutions, depending of your specific needs.

Print button with javascript - fast and simple solution

The simplest way to print a web form is to use client side javascript. To print a web page, you can use code like this:

<input type="button" value="Print" onclick="window.print();">

This HTML code will return a Print button like bellow. You can click it to print this page.

This example use window.print() method. This method will print your complete web page, including a print button too and everything else, like navigation menu, advertisement, search box etc. This probably not looks professional, but it could be useful in some scenarios.

To get better results, you can make two versions of web page, one for showing on screen and another adopted for printing. Then your print button will open print version in another window and call window.print() method when page loads.

How to make web page to be printer friendly

As you can see, window.print() method will print everything in your page, including navigation or Print button. Because of that, possible solution is to have two versions of page, one for displaying on screen and second version for printing. Version for printing will not contain buttons or navigation, but only the report formated for printing. In this case, your print button will have HTML code like this:

<input type="button" value="Print" onclick="window.open('YourPrintPage.aspx')" />

Print button on first page only opens a new window and navigate to print version of the page. At the end of YourPrintPage.aspx, you can add javascript code for printing, like this:

<script language="javascript">
window.print();
</script>

This javascript will execute when page loads and you don't need any button on your printing version.

It is not just about hiding navigations, buttons and other unwanted elements. You need to use different metric units. For screen version it is best to use pixels to define size of fonts, tables, images etc., as you probably already do. But, for print version use points instead of pixels. For example:

body {
   font: 12pt;
}

Avoid "printer friendly" version of the same content

It is unnecessary to make another page just for printing. Instead of two versions of the same content, it is often better to solve print format problems with css styles and have only one page for both printing and showing on screen. Take a look at bellow html code snippet, you can place it inside page's <head> tag:

<LINK rel="stylesheet" href="style-for-screen.css" type="text/css" media="screen">
<LINK rel="stylesheet" href="style-for-print.css" type="text/css" media="print">

As you can see, there is media attribute inside the LINK tag. Web browser will use style-for-screen.css to show page on screen or style-for-print.css when print the page. Now you can simply hide elements like navigation, buttons etc. For example, let say your navigation menu is inside <div> tag with id = "navigation". To hide it, inside style-for-print.css file code should be:

#navigation { display : none }

Or, if you don't want to have two css files, you can do it all in one file, with code like this:

@media print 
{
    /*****   Styles for print   ******/
      h1, h2, h3 {color:black;}
      #navigation {display:none}
}
 
@media screen {
      /*****   Styles for screen  ******/
}

Printing with PDF document

Printing of HTML page can be very complicated. You can experience problems with styles, but also with different client operating systems, different web browsers and even different versions of web browser. To make your page compatible to all this possible cases requires a lot of work and testing. Because of that, you can consider using of PDF format for printing, since this format is platform independent. In that case, your print button can be simple download of PDF document. You can even make some kind of Print Preview facility if you display PDF document on web page, inside ASP.NET server control. More about that you can read on How to Display PDF documents with ASP.NET tutorial.

To create PDF files, you can use Crystal Reports engine. See Export Crystal Reports to PDF file tutorial for more informations.

Print with custom Windows Control

.NET Windows Forms applications can print by using System.Drawing.Printing classes. With these classes you can access to local printer directly. Because of security reasons, you can't connect direct to local printer from web form. But, it is possible to place custom .NET Windows Forms Control or even Win32 ActiveX Control to Web Form. Custom Control can access directly to local printer and you can print your documents or reports like in any other Windows application. More about using of Windows Controls you can read on How to Place .NET Windows Forms Control To ASP.NET Web Form tutorial.

The problem with using custom controls is that your web application's user must install control before use. Of course, control will not work if client don't have Windows operating system. Also, .NET Control will not work if client have not .NET Framework installed. So, these additional demands could be difficult and annoying for your users or customers. Still, using of custom .NET or Win32 control can be acceptable solution if your application will be used inside your organization only or in your intranet etc. In that case sometimes is better to print with server side ASP.NET code to printer visible in local area network.

Print reports from database

I mentioned Crystal Reports as a tool you can use to print reports from database. Since this subject is very large, you can read Crystal Reports in ASP.NET tutorial to find out how to use this reporting tool which comes with Visual Studio. Depending of your needs, you can export these reports to PDF, but also to DOC, XLS, RTF and HTML format.

If your web site is driven by SQL Server database, you should consider ApexSQL Report as more optimized and more productive solution, specialized for MS SQL Server.


Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback  |   Google