在使用 iTextSharp 将网页保存为 PDF 时,有没有办法打开另存为窗口?现在我只是将它保存到一个集合文件中。
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();
您需要一个 ActionResult
方法来返回一个文件并设置适当的请求 header 。
public ActionResult Pdf()
{
Response.AddHeader("Content-Disposition", "attachment;filename=YourFile.pdf");
return File(yourPdfFile, "application/octet-stream");
}
File()
有一些重载,它们采用 byte[]
、System.IO.Stream
或指向磁盘上的实际文件。我对 iTextSharp 不太熟悉,所以无法进一步帮助您,但这应该会让您走上正轨。
如果我没记错的话,特定浏览器(我想是 IE)需要 header 才能弹出另存为对话框。
我是一名优秀的程序员,十分优秀!