Click here to Skip to main content
13,398,338 members (38,972 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 29 Mar 2013

Automatically Printing an RDLC file in ASP.NET MVC 3

, 3 Apr 2013
Rate this:
Please Sign up or sign in to vote.
Printing an RDLC file in ASP.NET MVC3 automatically by converting into PDF and using Acrobat Reader.


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 JavaScript 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 JavaScript 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 ActionResult RecieptPrint() 
  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(       
             reportType,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";
FileStream fss = new FileStream(Server.MapPath("~/Report/Summary.pdf"), FileMode.Open);
            byte[] bytes = new byte[fss.Length];
            fss.Read(bytes, 0, Convert.ToInt32(fss.Length));
//Here we returns the file result for view(PDF)
            return File(bytes, "application/pdf");

In view we can call this action method as follows .

$(document).ready(function () {
            $('#Print').click(function () {
<input type="button" id="Print"  value="Print"/>

This will print the PDF automatically.Requirement is that you must install the acrobat reader plug-in in your browser.

Points of Interest

If we are saved that PDF instead of printing while opening it will be printed automatically.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Nithesh AN
Software Developer (Senior) RM Eduction Solutions India
India India
Having experience in Microsoft technologies. Worked in ASP.NET MVC 3, ASP.NET,C#.NET and SQL Server

You may also be interested in...


Comments and Discussions

GeneralPrinting an RDLC file in ASP.NET MVC 3 Pin
Member 1348927927-Oct-17 6:39
memberMember 1348927927-Oct-17 6:39 
Questiondisable print popup on pdf generation Pin
priom221-Sep-16 3:24
memberpriom221-Sep-16 3:24 
AnswerRe: disable print popup on pdf generation Pin
Nithesh AN6-Sep-16 1:10
memberNithesh AN6-Sep-16 1:10 
QuestionNavigate Back to previous page Pin
Ruben lOuw5-Jan-15 21:58
memberRuben lOuw5-Jan-15 21:58 
Questionwhere is getPrintParams? Pin
Member 1030538430-Sep-13 14:49
memberMember 1030538430-Sep-13 14:49 
AnswerRe: where is getPrintParams? Pin
Nithesh AN6-Oct-13 20:24
professionalNithesh AN6-Oct-13 20:24 
GeneralMy vote of 5 Pin
vyasdev29-Mar-13 20:15
membervyasdev29-Mar-13 20:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180216.1 | Last Updated 3 Apr 2013
Article Copyright 2013 by Nithesh AN
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid