【问题标题】:opening PDF document from memory从内存中打开 PDF 文档
【发布时间】: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


【解决方案1】:

您可以像这样将 PDF 保存到内存流并将其写入浏览器。

protected void Page_Load(object sender, EventArgs e)
{
    MemoryStream ms;

    using (ms = new MemoryStream())
    {
       PdfWriter writer = PdfWriter.GetInstance(myPdfDoc, ms);

       Response.ContentType = "application/pdf";
       Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
       Response.OutputStream.Flush();
       Response.OutputStream.Close();

    }
}

【讨论】:

  • 我一直在寻找这样的东西,但我认为仍然缺少一些东西。我是否必须设置内容/类型
【解决方案2】:

您需要将其保存到一个临时文件夹,然后在该文件上调用Process.Start

【讨论】:

    【解决方案3】:

    要打开/显示 PDF,您可以在将文件保存到临时文件夹后使用 acrobat activex 组件。在早期的研究中,我找不到显示 PDF 的免费控件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-21
      • 2018-11-15
      • 2023-04-01
      • 2021-07-07
      • 2013-02-18
      • 1970-01-01
      相关资源
      最近更新 更多