In this tutorial, I'll show you how fill your data to pdf form using ITextSharp in C# and how to solve ITextSharp FormFlattening removes filled data when using the pdf form with ITextSharp to fill data into a pdf form.

public void PdfForm()
    PdfReader pdfReader = new PdfReader(Application.StartupPath + "\\HRForm.pdf");
    //Get fields in your pdf
    List<string> fields = new List<string>();
    foreach (var de in pdfReader.AcroFields.Fields)
    //Create a new pdf file
    PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(Application.StartupPath + "\\HRForm_Fill.pdf", FileMode.Create));
    AcroFields pdfFormFields = pdfStamper.AcroFields;
    //Fill data to your pdf form
    pdfFormFields.SetField("firstname", "lucy");
    //flatten the form to remove editting options
    pdfStamper.FormFlattening = true;

As you all know, ITextSharp is a library port from java to C# allows you to handle pdf file, such as create a pdf, add digital signature in pdf, fill data into a pdf form...etc

If you run the above snippet code, the generated pdf file will delete all the data that you filled in the pdf form. To solve the problem you should set GenerateAppearances properties to true to force iTextSharp generate appearances for the form fields.

AcroFields pdfFormFields = pdfStamper.AcroFields;
pdfFormFields.GenerateAppearances = true;

If you want to keep a PDF editable after populating some form fields you can set the FormFlattening properties to false.