I would like to save a edited pdf template. How do I do that?
using (MemoryStream ms = new MemoryStream())
{
PdfReader reader = new PdfReader("~/Content/Documents/Agreement.pdf");
PdfStamper formFiller = new PdfStamper(reader, ms);
AcroFields formFields = formFiller.AcroFields;
formFields.SetField("Name", formData.Name);
formFields.SetField("Location", formData.Address);
formFields.SetField("Date", DateTime.Today.ToShortDateString());
formFields.SetField("Email", formData.Email);
formFiller.FormFlattening = true;
formFiller.Close();
}
As in this example, you just need to use a FileStream instead of a MemoryStream.
The code should look like this:
string newFile = @".\FormDocument_out.pdf";
FileStream fs = new FileStream(newFile, FileMode.Create);
try
{
PdfReader reader = new PdfReader("~/Content/Documents/Agreement.pdf");
PdfStamper formFiller = new PdfStamper(reader, fs);
AcroFields formFields = formFiller.AcroFields;
formFields.SetField("Name", formData.Name);
formFields.SetField("Location", formData.Address);
formFields.SetField("Date", DateTime.Today.ToShortDateString());
formFields.SetField("Email", formData.Email);
formFiller.FormFlattening = true;
formFiller.Close();
}
catch(Exception)
{
throw;
}
finally
{
fs.Close();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With