2010-03-03 85 views
5

使用iTextSharp可以在内存中创建一个PDF文档,使用户可以选择“打开”或“保存”,如果它打开,则会在浏览器窗口中打开。从内存中打开PDF文档

目前我唯一将它保存到磁盘。

编辑:

好,我知道了sussed。我最终不得不将文件写入一个文件夹,但它只是每次都被覆盖的临时文件。下面是它的价值的解决方案:

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(); 

}

+0

考虑发布您的解决方案作为一个答案,将其标记为接受。 – dckuehn 2016-11-23 18:18:32

回答

0

你需要将其保存到临时文件夹,然后调用该文件Process.Start

0

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

3

您可以将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(); 

    } 
} 
+0

我一直在寻找这样的东西,但我认为还有一些东西缺失。我是否必须设置内容/类型 – Dkong 2010-03-03 04:47:33

+0

是的,你可能会看到编辑。 – 2010-03-03 13:32:27