Click here to Skip to main content
Click here to Skip to main content
Go to top

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.

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

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

Comments and Discussions

 
Questionwhere is getPrintParams? PinmemberMember 1030538430-Sep-13 13:49 
AnswerRe: where is getPrintParams? PinprofessionalNithesh AN6-Oct-13 19:24 
GeneralMy vote of 5 Pinmembervyasdev29-Mar-13 19:15 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 3 Apr 2013
Article Copyright 2013 by Nithesh AN
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid