Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Automatically Printing an RDLC file in ASP.NET MVC 3

0.00/5 (No votes)
3 Apr 2013 1  
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

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";   
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

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";
  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 .

$(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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here