问题
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