opening PDF document from memory

不羁的心 提交于 2019-12-07 04:03:51

问题


It is possible to create a PDF document in memory with iTextSharp that gives the user a choice to "open" or "save"?, and if it opens then it opens in a browser window.

At the moment the only I have save it to disk.

EDIT:

ok I've got it sussed. I did end up having to write the file to a folder, but it is only temporary as gets overwritten every time. Here is the solution for what it's worth:

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

}


回答1:


You could save the PDF to a memorystream and write it out to the browser like this.

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:


You'll need to save it to a temporary folder, then call Process.Start on the file.




回答3:


For opening/showing the PDF you could use the acrobat activex component after saving the file to a temporary folder. I could not find a free control to show PDFs in an earlier research.



来源:https://stackoverflow.com/questions/2368423/opening-pdf-document-from-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!