【发布时间】:2010-03-03 02:46:16
【问题描述】:
可以使用 iTextSharp 在内存中创建 PDF 文档,让用户可以选择“打开”或“保存”?如果它打开,则它会在浏览器窗口中打开。
目前只有我将它保存到磁盘。
编辑:
好的,我已经猜到了。我最终不得不将文件写入文件夹,但它只是临时的,因为每次都会被覆盖。这是值得的解决方案:
private void GeneratePDF() {
var doc1 = new Document();
string path = Server.MapPath("~/pdfs/");
string filepath = path + "Doc1.pdf";
PdfWriter.GetInstance(doc1, new FileStream(filepath, FileMode.Create));
doc1.Open();
doc1.Add(new Paragraph("A new Document"));
doc1.Add(new Paragraph(DateTime.Now.ToString()));
doc1.Close();
Response.Buffer = false; //transmitfile self buffers
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=myPDF.pdf");
Response.TransmitFile(filepath);
Response.End();
}
【问题讨论】:
-
考虑将您的解决方案发布为答案并将其标记为已接受。
标签: c# asp.net itextsharp