I can successfully create an on-the-fly pdf using
PdfSharp.Pdf.PdfDocument document = new PdfSharp.Pdf.PdfDocument(); PdfPage page = document.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(page); --- a series of gfx.DrawStrings to build page System.IO.MemoryStream stream = new System.IO.MemoryStream(); document.Save(stream, false); stream.Position = 0; Response.Clear(); Response.ContentType = "application/pdf"; Response.AddHeader("content-length", stream.Length.ToString()); Response.BinaryWrite(stream.ToArray()); Response.Flush(); stream.Close(); Response.End();
In most cases, I display a preview of a request (as a pdfsharp pdf) and display it in an I-frame. My client approves the request by clicking a button, and I e-mail a link to all appropriate contacts. These contacts can then click the link in the e-mail to regenerate the pdfsharp pdf in a browser window.
My client base has extended beyond my Intranet and I don't want to open my application to external access. I would like to simply modify my application to replace the link in the generated e-mail with an attachment. I used the following code:
1) On the initial preview generation, I save the pdfsharp document to Session System.Web.HttpContext.Current.Session["pdf"] = document; 2) In the code section that generates the e-mails, I tried using System.IO.MemoryStream PDFstream = new System.IO.MemoryStream(); ((PdfSharp.Pdf.PdfDocument)Session["pdf"]).Save(PDFstream, false); PDFstream.Position = 0; Attachment PDFFile = new Attachment(PDFstream, new System.Net.Mime.ContentType("application/pdf")); PDFFile.ContentDisposition.FileName = "myAttachedFile.pdf"; message.Attachments.Add(PDFFile);
The attachment is created and added to the e-mail, but is blank when clicked. If I open the file in a text editor, it appears to be a pdfsharp generated pdf file (the text is very similar to a pdfsharp pdf file that is originally built in the browser...it's just a bit larger). It seems like I'm missing something simple, like a format or something. Can you assist ?
PS - I'm trying to avoid creating an actual file on a disk that I would need to delete later.
Thanks
|