How to fill WinForms PDF form from database using C# and VB.NET?
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:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- 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
- 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.
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 .NET PDF Forms.
An online sample link to generate .NET PDF Forms
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion:
I hope you enjoyed learning about how to fill WinForms PDF form from database using C# and VB.NET.
You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to explore our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!