Generate a Report in PDF format

It is sometimes necessary to generate reports in distributable formats. The following example uses two SQL Services Reporting Services (SSRS) Web services to execute a report and export it in the Adobe PDF format. The example is below uses a Web application to write the resulting PDF file to the response back to the client, allowing them to either view or save the report. The standard Process Overview report is used in the example.

The Web service references are made to the ReportServer2005.asmx and the ReportExecution2005.asmx Web services, which are typically located on the Report Manager Web site. For example, http://<servername>/ReportServer/ReportServer2005.asmx and http://<servername>/ReportServer/ReportExecution2005.asmx. In the example below, the name given to the first reference is rs2005 and the name given to the second service is rsExecService.

using System; 

						using System.Data; 

						using System.Configuration; 

						using System.Web; 

						using System.Web.Security; 

						using System.Web.UI; 

						using System.Web.UI.WebControls; 

						using System.Web.UI.WebControls.WebParts; 

						using System.Web.UI.HtmlControls; 

  

  

						public partial class _Default: System.Web.UI.Page 

						{ 

						protected void Page_Load(object sender,EventArgse) 

						{ 

  

						} 

						protected void Button1_Click(object sender,EventArgse) 

						{ 

						GenerateReport("/Standard Reports/Process Overview","PDF"); 

						} 

  

						private void GenerateReport(string ReportName,string ReportFormat) 

						{ 

       

						rs2005.ReportingService2005rs =new rs2005ReportingService2005(); 

						rsExecService.ReportExecutionServicersExec =new rsExecServiceReportExecutionService(); 

						rs.Credentials = System.Net.CredentialCache.DefaultCredentials; 

						rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials; 

  

						string historyID =null; 

						string deviceInfo =null; 

						string format = ReportFormat; 

						Byte[] results; 

						string encoding =String.Empty; 

						string mimeType =String.Empty; 

						string extension =String.Empty; 

						rsExecServiceWarning[] warnings =null; 

						string[] streamIDs =null; 

						string filename =string.Empty; 

  

						switch(ReportFormat) 

						{ 

						case"PDF": 

						filename =@"Report.PDF"; 

						break; 

						case"IMAGE": 

						filename =@"Report.Tiff"; 

						break; 

						default: 

						break; 

						} 

  

  

						string _reportName = ReportName; 

						string _historyID =null; 

						bool _forRendering =false; 

						rs2005ParameterValue[] _values =null; 

						rs2005DataSourceCredentials[] _credentials =null; 

						rs2005ReportParameter[] _parameters =null; 

  

						try 

						{ 

						_parameters = rs.GetReportParameters(_reportName, _historyID, _forRendering, _values, _credentials); 

  

						rsExecService.ExecutionInfoei = rsExec.LoadReport(_reportName, historyID); 

  

						rsExecServiceParameterValue[] parameters =new rsExecServiceParameterValue[1]; 

  

						// Place to include the parameter. if(_parameters.Length > 0) 

						{ 

						parameters[0] =newrsExecService.ParameterValue(); 

						parameters[0].Name ="ProcessSetID"; 

						//parameters[0].Value = null; 

						} 

  

						// if report parameters are read-only do not execute SetExecutionParameters //rsExec.SetExecutionParameters(parameters, "en-us"); 

						results = rsExec.Render(format, deviceInfo, 

						out extension,out encoding, 

						out mimeType,out warnings,outstreamIDs); 

  

  

						// Clear the content of the response 

						Response.ClearContent(); 

  

						Response.AddHeader("Content-Disposition","attachment; filename="+ filename); 

						Response.AddHeader("Content-Length", results.Length.ToString()); 

						Response.ContentType ="application/octetstream"; 

						Response.BinaryWrite(results); 

						Response.Flush(); 

						Response.End(); 

						} 

						catch (Exceptionex) 

						{ 

						Response.Write(ex.Message); 

						} 

						} 

			}