30th May 2023

Modifying Fillable Form PDFs with iTextSharp in .NET

Build React Application

PDF (Portable Document Format) files often contain fillable form fields, which allow users to input data directly into the document. iTextSharp is a important open- source library for. NET that enables inventors to produce, manipulate, and modify PDF lines programmatically. In this blog post, we will explore how to work the iTextSharp package to modify fillable form PDFs in a. NET operation.

Modifying Fillable Form PDFs using iTextSharp

Step 1: First let’s create a console application in visual studio By following the below steps

  • iTextSharp

Step 1: Load an existing form

In this step, load an existing PDF form using the PdfReader class. Once the PDF form is loaded, a PdfStamper object is created to facilitate the modification of the form.

                                    
                                        
    // Path for the output file
    string outputFilePath = "Your Output File Path";
    // Load the existing form
    var existingPdf = new PdfReader("Your Existing File Path");
    var stamper = new PdfStamper(existingPdf, new FileStream(outputFilePath,
    FileMode.Create));
    var form = stamper.AcroFields;
                                     
                                

Step 2: Get all field names

In this step, the AcroFields property of the PdfStamper object is used to retrieve all the field names present in the form. These field names will be used to modify the respective fields later.

                                
                                    
  // Get all field names 
  var fieldNames = form.Fields.Keys;
    
                                 
                            

Step 3: Modify the field values

In this step, the code performs an iteration over each field name retrieved from the previous step. It checks whether each field name contains certain specific substrings.However," it's considered to represent the tab number field, If a field name contains the substring"InvoiceNo. However, the asked value will be assigned to it, If the tab number field is set up within the form.

                                
                                    
  // Modify field values
  foreach (var fieldName in fieldNames)
  {
      if (fieldName.Contains("InvoiceNo"))
      {
          form.SetField(fieldName, "INV-21-12-009");
      }
      if (fieldName.Contains("Date"))
      {
          form.SetField(fieldName, "11-11-2019");
      }
  }
                                 
                            

Step 4: Save the modified form

In this step, the modified form is saved. To flatten the form fields, set the Pdf Stamper object's FormFlattening property to true (optional). Finally, the Close() method is called on both the stamper and existing Pdf objects to release any resources associated with them. csharp Copy code

                                
                                    
  // Save the modified form 
  stamper.FormFlattening = true; // Optional: Flattens the form fields 
  stamper.Close(); 
  existingPdf.Close();
                                
                            

Conclusion

In this blog post, we have explored how to use the iTextSharp library in a .NET application to modify fillable form PDFs programmatically. We've covered colorful aspects similar as lading and modifying form fields, filling form fields, adding new form fields, and removing form fields. iTextSharp provides a important set of features that empower inventors to manipulate fillable form PDFs according to their specific conditions.

Let's develop your ideas into reality