Print a Crystal Report from an ASP.NET Application

I’ve been working on an application over the past few weeks that has really tested my programming abilities and if I have learned anything, its that sometimes even the simpliest of tasks can take hours to finally get right. Enter stage left: Crystal Reports and ASP.NET.

The mission was to automatically print a series of Crystal report documents (with parameters) on a button click. When your at a gas station, you dont preview your receipt and then choose to print it, it just prints right out. I wanted my application to have that same functionality. Sounds easy, right? WRONG.

You see, the SDK documentation provided by SAP gives some good examples on how to view the report in a report viewer control (as pictured below) but not on how to automatically print it. Hours of Google Magic only provide you with a ton of posts on which was the best method for doing what I was trying to do, but not how to actually do it.


So, in an effort to make the world a better place by putting this info out in the open, this is how to you do it.

First, you will need to download and install the SDK from SAPs website. I ran into a problem where my application was not able to access the assemblies, so the fix was to copy and paste the assembly files from the location the SDK installed them to, directly into my app. To do this, search for the “aspnet_client” folder and copy/paste it into your app.

Once that is finished, go into your code behind for the perticular page you want to have this bad boy print from and make sure the following assemblies are being used (you might not need all of them, but it works and I am in no mood to go down each assembly to see what is actually needed right now).

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Web;
using CrystalDecisions.ReportAppServer.ClientDoc;
using CrystalDecisions.ReportAppServer.Controllers;
using CrystalDecisions.ReportAppServer.CommonControls;
using CrystalDecisions.ReportAppServer.CommLayer;
using CrystalDecisions.ReportAppServer.CommonObjectModel;
using CrystalDecisions.ReportAppServer.ObjectFactory;
using CrystalDecisions.ReportAppServer.DataSetConversion;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Data.AdoDotNetInterop;
using CrystalPrintControlLib;
using CrystalReportViewerWebReportSourceLib;
using CrystalActiveXReportViewerLib13;

Once that is done, add in the following code to whatever action you want the report to print on. In my case, it is on a button click, but you can do it on page load or whatever else.

//print sales report
ReportDocument slbl = new ReportDocument();
System.Drawing.Printing.PrintDocument pDoc = new System.Drawing.Printing.PrintDocument();
CrystalDecisions.Shared.PrintLayoutSettings PrintLayout = new CrystalDecisions.Shared.PrintLayoutSettings();
System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();

//set printer for report
printerSettings.PrinterName = "My Printer Name";
System.Drawing.Printing.PageSettings pSettings = new System.Drawing.Printing.PageSettings(printerSettings);
slbl.PrintOptions.DissociatePageSizeAndPrinterPaperSize = true;
slbl.PrintOptions.PrinterDuplex = PrinterDuplex.Simplex;
slbl.PrintToPrinter(printerSettings, pSettings, false, PrintLayout);

Now, there is limitation with this method. The printer must be directly attached to the client machine in order for the app to recognize the printer name. A work around is to install the printer manually by going to Control Panel > Devices and Printers > Add a Printer > Add a Local Printer> Then add the printer by IP address.

Another thing to remember is that your Crystal Report must be using Intergrated Security, or in otherwords, it cannot require a password to run the report.

Now your cooking with gasoline! When the action occurs to print your report, you should see the printed results. Be warned, however, that the process takes FOREVER to complete. On the plus side, its pretty reliable.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s