Wednesday, December 3, 2008

Invoking "Save as PDF" method of Report Viewer

UserEntryFormEntity oEnt= new UserEntryFormEntity();
List oEntity = new List();

oEnt = GetReportData(JoinersId); // Method Which returns a List of Data
oEntity.Add(oEnt);

Microsoft.Reporting.WebForms.LocalReport localReport = new Microsoft.Reporting.WebForms.LocalReport();
localReport.ReportPath = Server.MapPath("~/DocumentTemplates/UserEntryForm.rdlc");

Microsoft.Reporting.WebForms.ReportDataSource oDataSource = new Microsoft.Reporting.WebForms.ReportDataSource("UserEntryFormEntity", oEntity);
localReport.DataSources.Add(oDataSource);
string reportType = "PDF";
string mimeType;
string encoding;
string fileNameExtension;
string deviceInfo = "" + " PDF" + " 8.5in" + " 11in" + " 0.5in" + " 1in" + " 1in" + " 0.5in" + "";
Microsoft.Reporting.WebForms.Warning[] warnings;
//Warning[] warnings;
string[] streams;
byte[] renderedBytes;
renderedBytes = localReport.Render(reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
//Clear the response stream and write the bytes to the outputstream
//Set content-disposition to "attachment" so that user is prompted to take an action
//on the file (open or save)
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename=foo." + fileNameExtension);
Response.BinaryWrite(renderedBytes);
Response.End();