Click here to Skip to main content
15,905,414 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Sir,

I am writing all contents inside a gridview to PDF.

It is generating a window with 'Open with' and 'Save' option.

Here my requirement is how to open that PDF without this window.
That means the open with event in popup window want to work in background..

Please help me.
Thanks in advance.

My code i am giving below..
This code is working perfectly with the open with window..


protected void imgPdf_Click(object sender, EventArgs e)
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
try
{
//To Export all pages
grdRequestList.AllowPaging = false;
BindGrid();

grdRequestList.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
HttpContext.Current.ApplicationInstance.CompleteRequest();
//PdfAction action = new PdfAction(PdfAction.PRINTDIALOG);
//Response.End();
}
catch (Exception Ex)
{

throw Ex;
}
}
}
}
Posted
Updated 10-Feb-14 20:53pm
v2

1 solution

It's not in your hand. How browsers handle downloaded content is complicated by several thing...
The mime type - does it known for the browser? There is an application assigned to it?
The user's preferences - how user wish to handle specific content?

You may come up with a solution (mostly using iframe) that will work on most IE browsers, but not anywhere else and not on customized IE too...
So do not waste your time...
 
Share this answer
 
Comments
kanamala subin 11-Feb-14 4:12am    
Thank you for spending time to my question..
But you are not correct.
I solved it myself by changing ,
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
To

Response.AddHeader("content-disposition", "inline;filename=GridViewExport.pdf");

onething i am facing here is it is, opening in the same window..
Kornfeld Eliyahu Peter 11-Feb-14 4:19am    
Content-disposition is only used to attach file name to your download. The behavior of not-opening save dialog if the header is presented can be easily changed by user don't wish to enable that behavior...
kanamala subin 11-Feb-14 6:03am    
The same I need..Can you please suggest any way to open it in a new window or tab..?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900