We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to fill PDF form from database using C# and VB.NET

Platform: WinForms |
Control: PDF |
Published Date: November 30, 2018 |
Last Revised Date: April 29, 2019

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can fill PDF form from database using C# and VB.NET.

Steps to fill PDF form from database programmatically:

  1. Create a new C# console application project. Create a console application project
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. NuGet package reference screenshot
  3. Include the following namespaces in Program.cs file.

C#

using Syncfusion.Pdf.Parsing;
using System.Data.OleDb;

VB.NET

Imports Syncfusion.Pdf.Parsing
Imports System.Data.OleDb

 

  1. Use the following code snippet to fill PDF form from database.

C#

//Connection string 
string dbFile = System.IO.Path.GetFullPath(path) + "Database.mdb";
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbFile;
//SQL Query
string strSQL = "SELECT * From Table1";
//Create a connection  
OleDbConnection connection = new OleDbConnection(connectionString);
//Create a command and set its connection  
OleDbCommand command = new OleDbCommand(strSQL, connection);
//Open the connection and execute the select command
connection.Open();
//Execute command  
OleDbDataReader reader = command.ExecuteReader();
//Load the PDF document 
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("FormTemplate.pdf");
//Get the loaded form
PdfLoadedForm loadedForm = loadedDocument.Form;
while (reader.Read())
{
    //Get the values from database
    string name = reader["Name"].ToString();
    string phone = reader["Phone"].ToString();
    string email = reader["Email Address"].ToString();
    //Assign values to fields
    (loadedForm.Fields["Name"] as PdfLoadedTextBoxField).Text = name;
    (loadedForm.Fields["Phone"] as PdfLoadedTextBoxField).Text = phone;
    (loadedForm.Fields["Email address"] as PdfLoadedTextBoxField).Text = email;
}
//SQL Query for another table
strSQL = "select * from Table2";
//Create a connection  
connection = new OleDbConnection(connectionString);
//Create a command and set its connection  
command = new OleDbCommand(strSQL, connection);
//Open the connection and execute the select command
connection.Open();
//Execute command  
reader = command.ExecuteReader();
while (reader.Read())
{
    //Get values from database
    string qualification = reader["Qualification"].ToString();
    bool isSelected = Convert.ToBoolean(reader["IsSelected"]);
 
    //Check the checkbox
    PdfLoadedCheckBoxField loadedCheckBoxField = loadedForm.Fields[qualification] as PdfLoadedCheckBoxField;
    loadedCheckBoxField.Checked = isSelected;
}
//Save the document
loadedDocument.Save("Form.pdf");
//Close the document 
loadedDocument.Close(true);
//This will open the PDF file so, the result will be seen in default PDF Viewer 
Process.Start("Form.pdf");

VB.NET

'Connection string 
Dim dbFile As String = System.IO.Path.GetFullPath(path) & "Database.mdb"
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbFile
'SQL query
Dim strSQL As String = "SELECT * From Table1"
'Create a connection  
Dim connection As OleDbConnection = New OleDbConnection(connectionString)
'Create a command and set its connection  
Dim command As OleDbCommand = New OleDbCommand(strSQL, connection)
'Open the connection and execute the select command
connection.Open()
'Execute command  
Dim reader As OleDbDataReader = command.ExecuteReader()
'Load the PDF document 
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("FormTemplate.pdf")
'Get the loaded form
Dim loadedForm As PdfLoadedForm = loadedDocument.Form
While reader.Read()
    'Get the values from database
    Dim name As String = reader("Name").ToString()
    Dim phone As String = reader("Phone").ToString()
    Dim email As String = reader("Email Address").ToString()
    'Assign values to fields
    TryCast(loadedForm.Fields("Name"), PdfLoadedTextBoxField).Text = name
    TryCast(loadedForm.Fields("Phone"), PdfLoadedTextBoxField).Text = phone
    TryCast(loadedForm.Fields("Email address"), PdfLoadedTextBoxField).Text = email
End While
'SQL query for another table
strSQL = "select * from Table2"
'Create a connection  
connection = New OleDbConnection(connectionString)
'Create a command and set its connection  
command = New OleDbCommand(strSQL, connection)
'Open the connection and execute the select command
connection.Open()
'Execute command  
reader = command.ExecuteReader()
While reader.Read()
    'Get values from database
    Dim qualification As String = reader("Qualification").ToString()
    Dim isSelected As Boolean = Convert.ToBoolean(reader("IsSelected"))
    'Check the checkbox
    Dim loadedCheckBoxField As PdfLoadedCheckBoxField = TryCast(loadedForm.Fields(qualification), PdfLoadedCheckBoxField)
    loadedCheckBoxField.Checked = isSelected
End While
'Save the document
loadedDocument.Save("Form.pdf")
'Close the document
loadedDocument.Close(True)
'This will open the PDF file so, the result will be seen in default PDF Viewer 
Process.Start("Form.pdf")

 

A complete working sample can be downloaded from PDFFormFillingSample.zip.

By executing the program, you will get the PDF document as follows. Output document screenshot

Take a moment to peruse the documentation, where you will find other options like creating PDF form, modifying existing form fields, removing editing capability, removing form fields, importing FDF file to PDF, export PDF file to FDF, adding actions to form fields with code examples.

Refer here to learn more about PDF Forms.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile