0

I have created a PDF using iText and storing it in a particular location (specified in the code). I would like to prompt a save dialog box for the user to choose location on his computer to save the pdf. I checked iText tutorial but it didn't help me.

Here is the code for generating the PDF file:

Document objDoc = new Document();
PdfWriter.GetInstance(objDoc, new FileStream("C:\\HelloWorld.pdf", FileMode.Create));
objDoc.Open();
objDoc.Add(new Paragraph("welcome iText Pdf"));
objDoc.Close();

I tried like this for saving:

string FileName ="HelloWorld.pdf";
String FilePath = @"C:\";
HttpResponse response = HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "application/pdf";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath + FileName);
response.Flush();
response.End();
dotnetrocks
  • 2,589
  • 12
  • 36
  • 55

1 Answers1

1

I'm assuming you're doing this from a web page since you tagged this ASP.NET. You need to add the Content-Disposition header. See the following question for details:

Force download of a file on web server - ASP .NET C#

Community
  • 1
  • 1
Richard
  • 29,854
  • 11
  • 77
  • 120