how to print PDF automatically from asp.net [Resolved]

Posted by Asifhussain under Sql Server on 4/3/2012 | Points: 10 | Views : 88092 | Status : [Member] | Replies : 10
The following is the piece of code, which runs fine to create PDF from ReportViewer. It prompts me with a dialogue for Opening/Saving File. Instead i want to Open PDF File directly without prompting dialogue box else Show PDF in PrintPreview mode and then print.

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




Responses

Posted by: Asifhussain on: 4/9/2012 [Member] Starter | Points: 25

Up
0
Down

Resolved
The answer to my question is to use

rptViewer.ExportContentDisposition = ContentDisposition.AlwaysInline;


Hence by adding this piece of code, PDF file will open automatically.

Thanks All for giving your valuable feedback.

Asifhussain, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajni.Shekhar on: 4/3/2012 [Member] Bronze | Points: 25

Up
0
Down
check below link:

http://www.velocityreviews.com/forums/t83315-print-pdf-directly-without-preview-from-client-side-using-asp-net.html


Thanks,
Rajni Shekhar

Asifhussain, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 4/3/2012 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
This is a Browser Security Feature. Everything that is open from the Browser , it must always ask the user if they want to save the file. So if you don't want that , you need to provide a window and redirect to it



Thank you for posting at Dotnetfunda
[Administrator]

Asifhussain, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Asifhussain on: 4/3/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Rajni Shekhar,

The link which you gave shows how to print the saved pdf file, but in my case i dont want to save pdf file rather print directly

Anyways Thanks for posting your response.

Asifhussain, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 4/3/2012 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Did you read what i said above ?

Thank you for posting at Dotnetfunda
[Administrator]

Asifhussain, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Asifhussain on: 4/3/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Vuyiswamb,

I have read your reply and i know this is how it works, looking for alternative way for print PDF

Anyways thanks again

Asifhussain, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 4/3/2012 [Member] Silver | Points: 25

Up
0
Down
Hi,
Write the pdf file to one of the directory of your project , if success , then redirect to a page and open the pdf file or call a popup page to open the saved pdf.
Hope this will help you.


Thanks,
Sanjay

Asifhussain, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 4/9/2012 [Member] Bronze | Points: 25

Up
0
Down
Asifhussain

Useful to me

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Asifhussain, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 4/9/2012 [Member] Starter | Points: 25

Up
0
Down
Hi refer below link.


http://forums.databasejournal.com/showthread.php?t=53168

Thanks & Regards
Hari

Asifhussain, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 4/9/2012 [Member] Silver | Points: 25

Up
0
Down
Thank you Asifhussain

Regards,
Singaravel M

Asifhussain, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response