Automaticaly Printing RDLC in Asp.Net MVC 3

29 Mar

Download (Code Project)

Here i am explaining a small method which i am adopted for automatically printing an rdlc file in my Asp.Net MVC3 application.

In my Asp.Net MVC 3 application I had suffered with the report section. We are using the client definition (rdlc) of Reporting Service for report creation. But the things were stuck on one scenario where our client needs to automatically print the reports by specifying a particular printer name which is presented.

After a lot of googling I realized that it is a hard scenario. Finally we decided to achieve it with acrobat reader and java script to pdf. Requirement is that you must install the acrobat reader plugin in your browser.
The requirement is that there is a printer setting where we are setting a particular printer for a particular report by providing the IP address of the system. According to these settings a particular report must print through a printer which is provided in the settings table.

Using the code
First we designed the report using rdlc. The next step is converting the rdlc to pdf from the action method in the controller. Also we need to inject the javscript for automatically printing the report in acrobat viewer. For that with the help of  iTextSharp dll we converted the rdlc file into pdf at the same time we injected some javscript for automatic printing to the pdf.
You need to include the following namespaces which is in iTextSharp DLL
using iTextSharp.text.pdf;

using iTextSharp.text;

The action method is as follows

public void RecieptPrint(long inv_ReceiptID)


           LocalReport localReport = new LocalReport();

           localReport.ReportPath = @”Reprt1.rdlc”;

          DataTable dt = DataSelect();

            The following code is for dynamically setting the data source to rdlc. I already wrote about this here

ReportDataSource reportDataSource = new ReportDataSource();

reportDataSource.Value = dt;

reportDataSource.Name = “DataSet1”;



string reportType = “PDF”;

string mimeType;                  string encoding;

string fileNameExtension = “pdf”;

//The DeviceInfo settings should be changed based on the reportType

string deviceInfo =@”<DeviceInfo>








Warning[] warnings;
string[] streams;
byte[] renderedBytes;

Now we are going to converting the rdlc to byte array using the above device info for pdf . To know more about it click here

           renderedBytes= localReport.Render(
deviceInfo,out mimeType, out   encoding,               out   fileNameExtension,out streams,out warnings);

Next comes the important section. We are adding this byte array to our pdf file. Also we are injecting the javascript which did the automatic printing into the pdf.

var doc = new Document();

var reader = new PdfReader(renderedBytes);

using (FileStream fs = new FileStream(Server.MapPath(“~/Summary”+ Convert.ToString(Session[“CurrentUserName“]) + “.pdf“),   FileMode.Create))


      PdfStamper stamper = new PdfStamper(reader, fs);

      string Printer =     PrinterName(Convert.ToInt32(Session[“localOutletID“]));

// This is the script for automatically printing the pdf in acrobat viewer

stamper.JavaScript= “var pp = getPrintParams();pp.interactive =  pp.constants.interactionLevel.automatic;pp.printerName = ” + Printer + “;print(pp);\r”;






          In view using an iframe we can render this pdf and it will be printed automatically. Requirement is that you must install the acrobat reader plugin in your browser.

Points of Interest

If we saved that pdf instead of printing while opening it will be printed automaticlay.


One Response to “Automaticaly Printing RDLC in Asp.Net MVC 3”

  1. Arun M March 29, 2013 at 3:25 pm #

    Execelent nithesh

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

%d bloggers like this: