Reputation: 485
Is there a way to open the save as window when saving webpage to PDF using iTextSharp? right now I'm just saving it to a set file.
var pdfDoc = new Document();
const string folderPath = "C:\\SG-ListingPDFs\\";
bool isExists = Directory.Exists(folderPath);
if (!isExists)
Directory.CreateDirectory(folderPath);
var writer = PdfWriter.GetInstance(pdfDoc, new FileStream("C:\\SG-ListingPDFs\\" + detailsViewModel.ListingNumber + ".pdf", FileMode.Create));
pdfDoc.Open();
....Code Here
pdfDoc.Add(table);
TempData["SavedPdF"] = true;
pdfDoc.Close();
Upvotes: 2
Views: 1348
Reputation: 33306
I would create the stream as a referenced variable i.e.
var aFileName = string.Format("{0}.{1}", detailsViewModel.ListingNumber + "pdf";
var aStream = new FileStream("C:\\SG-ListingPDFs\\" + aFileName);
Then pass it into the get instance like this:
var writer = PdfWriter.GetInstance(pdfDoc, aStream , FileMode.Create));
Then from your controller return the stream like this as a FileResult
.
public ActionResult SaveFile()
{
// processing to get the file etc....
return File(filePath, aStream , aFileName);
}
The important part in the above is the aFileName
paramater, this is the fileNameDownload
parameter which makes it open as a downloadable file.
Upvotes: 0
Reputation: 6832
You'll need to have an ActionResult
method that returns a file and set the appropriate request headers.
public ActionResult Pdf()
{
Response.AddHeader("Content-Disposition", "attachment;filename=YourFile.pdf");
return File(yourPdfFile, "application/octet-stream");
}
File()
has a few overloads that take either a byte[]
, a System.IO.Stream
or the path to an actual file on disk. I'm not too familiar with iTextSharp so I can't help you much further, but this should send you on the right track.
If my memory serves me right, the header is needed in a specific browser (I'm thinking IE) to get the save as dialog to pop.
Upvotes: 1