.Net Core Console Application - iTextSharp Data Merge
C# .Net Core iTextSharp Data Merge Console Application for PDF Files
This article describes how to create a .Net Core Console Application to create an merged PDF form file using iTextSharp. The console application uses the iTextSharp package to generate the Byte Stream and output SQL Server data into a PDF File. The code for this application uses the iTextSharp PdfStamper class which is no longer available in iText7.
Editable PDF Form for Data Merge
The PDF Form contains editable fields. The task is to take a list and import the data for each individual into the PDF Form and name the files using the Sales Person's name.
Create a SQL Server Stored Procedure
Create a stored procedure in SQL Server from a database. This example uses the AdventureWork2017 SQL database View Sales.vIndividualCustomer in a Stored Proc (usp_getSalesCustomers). The few fields in this Stored Proc will be used as Fields for the PDF merge.
The output in SSMS from the Stored Procedure.
C# .Net Core Console Application with iTextSharp
Function to retrieve PDF File - Add a function to retrieve the byte array containing the PDF form from the SQL database table. (The file was converted \ imported into a varbinary(max) field.)
SQL Function to retrieve of PDF form from binary field in SQL Table
Function to retrieve iTextSharp Fields from PDF - Add a function to retrieve a Dictionary containing the iTextSharp field data for output to PDF from the Stream provided.
Code from Program.cs
The PDFHelper class iTextSharp code to extract Form Fields from PDF file Stream.
Retrieve Fields Values - Add a code to assign the SQL values to the FormFieldMap Dictionary.
Code for GeneratePDF function from Program.cs using the FormFieldMap dictionary and the Form Stream.
The final function to call is ReturnPDF to write out the data from iTextSharp into a PDF file. The arguments passed are the PDF file byte array + the path and fileName.
Code for ReturnPDF function from Program.cs
ReturnPDF in PDF Helper Class
Completed Merge PDF sample