1

i am sending a PDF to my page and i want to set a default name when the user tries to save the PDF document.

i am using ItextSharp and VB.Net

Using s As MemoryStream = New MemoryStream()
  Dim Pdf_Writer As PdfWriter = PdfWriter.GetInstance(DocumentPDF, s)
  DocumentPDF.Open()

  DocumentPDF.SetMargins(10.0F, 10.0F, 10.0F, 10.0F)

  DocumentPDF.Add(Table)

  DocumentPDF.Close()

  contentX= s.ToArray()
  HttpContext.Current.Response.Buffer = False
  HttpContext.Current.Response.Clear()
  HttpContext.Current.Response.ClearContent()
  HttpContext.Current.Response.ClearHeaders()
  HttpContext.Current.Response.ContentType = "Application/pdf"
  HttpContext.Current.Response.BinaryWrite(contentX)
  HttpContext.Current.Response.Flush()
  HttpContext.Current.Response.End()
End Using

.

Response.AddHeader("content-disposition", @"attachment;filename=""MyFile.pdf""");

this way download the file(yea, it sets a default name), but i just want to show the file and if the user wants to save it, well... save it(with a default name)

how can i set a default name to my PDF document?

Logar314159
  • 503
  • 4
  • 16
  • 1
    possible duplicate of [Specifying filename for dynamic PDF in asp.net](http://stackoverflow.com/questions/74019/specifying-filename-for-dynamic-pdf-in-asp-net) – Chris Haas Apr 18 '13 at 21:51

2 Answers2

1

try with this code:

Response.ContentType = "application/pdf"
Response.AppendHeader("Content-Disposition", "inline; filename="filename".pdf")
Response.TransmitFile("filename")
Response.End()
gon250
  • 3,405
  • 6
  • 44
  • 75
0

I had a similar problem delivering a PDF via a handler page (.ashx). No matter what I set in the HTTP headers, saving from the browser PDF reader would always set the filename to "getpdf.pdf" when I used this url.

http://www.thepdfchef.com/handlers/getpdf.ashx?id=5188p

So what I did was add an escaped string after the handler path then the querystring at the end of that, like so:

http://www.thepdfchef.com/handlers/getpdf.ashx/Wellbeing%20And%20Domestic%20Assistance%20From%20John%20Paul?id=5188p

You should check for invalid characters and strip out any that could cause the name to be dangerous.