Click here to Skip to main content
15,860,972 members
Articles / Web Development / ASP.NET / ASP.NET4.0
Tip/Trick

Automatically Printing an RDLC file in ASP.NET MVC 3

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
3 Apr 2013CPOL2 min read 82.1K   4.5K   21   10
Printing an RDLC file in ASP.NET MVC3 automatically by converting into PDF and using Acrobat Reader.

Introduction

Here I am explaining a small method which I am adopted for automatically printing an RDLC file in my ASP.NET MVC3 application.

Background

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

C++
using iTextSharp.text.pdf;
using iTextSharp.text;

The action method is as follows

C++
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

C++
ReportDataSource reportDataSource = new ReportDataSource();    
reportDataSource.Value = dt;      
reportDataSource.Name = "DataSet1";   
localReport.SetParameters(param);
localReport.DataSources.Add(reportDataSource); 
string reportType = "PDF";  
string mimeType;  
string encoding;
string fileNameExtension = "pdf"; 
//The DeviceInfo settings should be changed based on the reportType 
string deviceInfo =@"<DeviceInfo>              
 <OutputFormat>PDF</OutputFormat>              
 <PageWidth>9.2in</PageWidth>              
 <PageHeight>12in</PageHeight>          
 <MarginTop>0.25in</MarginTop>          
 <MarginLeft>0.45in</MarginLeft>            
 <MarginRight>0.45in</MarginRight>       
 <MarginBottom>0.25in</MarginBottom></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

C++
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.

C++
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";
  stamper.Close();
}           
reader.Close();
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));
            fss.Close();
            System.IO.File.Delete(Server.MapPath("~/Report/Summary.pdf"));
//Here we returns the file result for view(PDF)
            return File(bytes, "application/pdf");
}

In view we can call this action method as follows .

C++
$(document).ready(function () {
            $('#Print').click(function () {
            window.open("../Report/RecieptPrint");
        });
});
<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 was originally posted at http://wp.me/pX9l5-27

License

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


Written By
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

Comments and Discussions

 
QuestionAutomatically Printing an RDLC file in ASP.NET MVC 3 Pin
javed kamal7-Apr-21 23:01
javed kamal7-Apr-21 23:01 
QuestionNeed help to remove preview box poping up for printing ..need to do print without preview Pin
vrani8-Oct-18 15:44
vrani8-Oct-18 15:44 
QuestionNot priting as per heading automatic print Pin
MayurPadhiyar2-Oct-18 2:42
MayurPadhiyar2-Oct-18 2:42 
GeneralPrinting an RDLC file in ASP.NET MVC 3 Pin
Member 1348927927-Oct-17 5:39
Member 1348927927-Oct-17 5:39 
Questiondisable print popup on pdf generation Pin
priom221-Sep-16 2:24
priom221-Sep-16 2:24 
AnswerRe: disable print popup on pdf generation Pin
Nithesh AN6-Sep-16 0:10
Nithesh AN6-Sep-16 0:10 
QuestionNavigate Back to previous page Pin
Ruben lOuw5-Jan-15 20:58
Ruben lOuw5-Jan-15 20:58 
Questionwhere is getPrintParams? Pin
Member 1030538430-Sep-13 13:49
Member 1030538430-Sep-13 13:49 
AnswerRe: where is getPrintParams? Pin
Nithesh AN6-Oct-13 19:24
Nithesh AN6-Oct-13 19:24 
GeneralMy vote of 5 Pin
vyasdev29-Mar-13 19:15
vyasdev29-Mar-13 19: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.