Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I save an edited pdf?

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();
}
like image 382
MrM Avatar asked Jun 02 '11 19:06

MrM


1 Answers

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();
}
like image 144
yms Avatar answered Oct 15 '22 18:10

yms