I have a process where I am creating a PDF doc from data extracted from a MS SQL database. These are done with a person's name and I create a file in App_Data first with:
string pdfTemp = System.Web.HttpContext.Current.Server.MapPath("~/Images/MTF.pdf");
string filename = "~/App_Data/" + person.FirstName + "_" + person.LastName + "_MTF" + ".pdf";
iTextSharp.text.pdf.PdfStamper stamper = new PdfStamper(reader, new FileStream(System.Web.HttpContext.Current.Server.MapPath(filename), FileMode.Create));
I then fill in the fields, close the stamper and send a stream to the view as well as save it to the database. I periodically try to delete any PDF file in the App_Data folder. If the PDF is being redone, sometimes the PDF in App_Data in still there for the person and it fails of course because is in use. How do you create a PDF without having to create a file first?
I've changed the approach according to a comment:
MTFModel mtf = new MTFModel();
mtf = GetMtfModelByPatientId(patientId);
string pdfTemp = System.Web.HttpContext.Current.Server.MapPath("~/Images/MTF.pdf");
iTextSharp.text.pdf.PdfReader reader = new PdfReader(pdfTemp);
string pwd = System.Web.Security.Membership.GeneratePassword(20, 5);
MemoryStream ms = new MemoryStream();
iTextSharp.text.pdf.PdfStamper stamper = new PdfStamper(reader, ms);
// stamp the form
stamper.Close();
return ms;
I can try to save it to the database:
AddPatientMtf(mtd.ID, ms);
And then return the stream:
return ms;
But the add patient function has to close the stream in order to save it.