I want to open PDF File directly without prompting Open/Save in browser else I wish to show PDF in print preview and then print. The following is the code used to generate PDF file and works fine.
Code:
string strReportName = string.Empty;
if (Request.QueryString["ReportName"] != null)
strReportName = Request.QueryString["ReportName"].ToString();
HttpContext context = HttpContext.Current;
if (!context.Response.Buffer)
return;
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
//ReportServerURL returns me the URL of ReportServer
string strReportUrl = ConfigurationManager.AppSettings["ReportServerURL"].ToString();
//ReportFolder returns the path of Report Folder Name
string strReportFolder = ConfigurationManager.AppSettings["ReportFolder"].ToString();
ReportViewer rptViewer = new ReportViewer();
rptViewer.ServerReport.ReportServerUrl = new System.Uri(strReportUrl);
rptViewer.ServerReport.ReportPath = "/" + strReportFolder + strReportName;
rptViewer.ServerReport.SetParameters(GetObjParameter(Request.QueryString["Parameters"], Request.QueryString["ParamValues"]));
byte[] pdfContent = rptViewer.ServerReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);
context.Response.Buffer = true;
context.Response.Clear();
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("Content-disposition", string.Format("attachment; filename={0}.{1}", strReportName, extension));
context.Response.BinaryWrite(pdfContent);
context.Response.Flush();
context.Response.End();
A Generic Method for creating Dynamic Report Parameters is as follows:
private ReportParameter[] GetObjParameter(string Parameters, string Values)
{
ReportParameter[] rptParams = null;
string[] sParam = Parameters.Split(',');
string[] sVal = Values.Split(',');
if (sParam.Count() == sVal.Count())
{
rptParams = new ReportParameter[sParam.Count()];
for (int i = 0; i < sParam.Count(); i++)
{
if (sParam[i] == "FromDate" && sVal[i] == " 1/1/0001 12:00:00 AM")
sVal[i] = null;
if (sParam[i] == "ToDate" && sVal[i] == "1/1/0001 12:00:00 AM")
sVal[i] = null;
if (sParam[i] == "ETADate" && sVal[i] == "1/1/0001 12:00:00 AM")
sVal[i] = null;
rptParams[i] = new ReportParameter(sParam[i].ToString(), sVal[i]);
}
}
return rptParams;
}
Any Help would be appreciatable.
Thanks in Advance