views:

516

answers:

3

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

}

A: 

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

SLaks
A: 

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.

henchman
+1  A: 

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

}

Steve Danner
I was looking for something like this, but I think something is still missing. Do I have to set the Content/Type
Dkong
Yes, you very well might, see the edit.
Steve Danner