gpt4 book ai didi

c# - 使用 iTextSharp 打开另存为窗口

转载 作者:太空宇宙 更新时间:2023-11-03 13:31:46 25 4
gpt4 key购买 nike

在使用 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 才能弹出另存为对话框。

关于c# - 使用 iTextSharp 打开另存为窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20246772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com