Logar314159
Logar314159

Reputation: 503

set default name to PDF document with itextsharp

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?

Upvotes: 1

Views: 2827

Answers (2)

gon250
gon250

Reputation: 3545

try with this code:

Response.ContentType = "application/pdf"
Response.AppendHeader("Content-Disposition", "inline; filename="filename".pdf")
Response.TransmitFile("filename")
Response.End()

Upvotes: 1

Marcus Cunningham
Marcus Cunningham

Reputation: 350

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.

Upvotes: 0

Related Questions