Opening a PDF from memory

You can create a PDF document in memory with iTextSharp that gives the user the option to “open” or “save” ?, and if it opens, it opens in a browser window.

At the moment, only I saved it to disk.

EDIT:

ok I have it crazy. I still had to write the file to a folder, but it is temporary, as it is overwritten every time. Here is a solution for what it costs:

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

}

+5
source share
3 answers

You can save the pdf to memystream and write it to a 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();

    }
}
+3
source

You need to save it to a temporary folder and then call it Process.Startin a file.

0

To open / display a PDF, you can use the acrobat activex component after saving the file to a temporary folder. I could not find free control to show PDF files in earlier studies.

0
source

All Articles