Auto Fill PDF Form from Database using C#

This short guide describes how to auto fill PDF form from database using C#. It has IDE settings, a list of steps, and sample code to transform data from a database to PDF form using C#. The sample code in the article uses MySql database whereas you may use any other database to fetch data and fill the PDF form.

Steps to Populate PDF Form from Database using C#

  1. Set the environment to use Aspose.PDF for .NET to fill the form from the DB
  2. Define the database connection settings and build the connection string
  3. Open the connection to the database and run a parameterized query for the desired record
  4. Create a PDF Document using the template PDF, having form fields in it
  5. Set values from the DB record into the form fields using the TextBoxField class
  6. Save the filled PDF on the disk

These steps summarize how to auto populate PDF form from database using C#. Define the database credentials, create a connection string, set the output directory, and instantiate the connection with the database using the connection string. Use the parameterized query to fetch data from the database, check if data is available, fill each form field using the field name with the respective column data, and save the PDF on the disk.

Code to Populate PDF from Database using C#

This code has demonstrated how to generate PDF from database using C#. You may use the environment variables instead of setting hardcoded credentials in the code. You can fill CheckBoxField, RadioButtonField, ComboBoxField, DateField, and NumberField if available in the form.

This article has taught us the process to populate a PDF form. To resize images in a PDF, refer to the article Resize Images in PDF using C#.