Programming Samples

Click here to go to:



Excel VBA

Word VBA

MS Access

Python

T-SQL

SSIS

SSRS

Power BI

Crystal Reports

SSAS

SQL Replication

C# Code

ASP .NET Code

Oracle PL/SQL

Database Diagramming


Back to Home Page


.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.

Editable PDF Form + Fields

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.

TSQL Stored Procedure - AdventureWorks Database SSMS

The output in SSMS from the Stored Procedure.

TSQL Output from SP

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.)

Retrieve Data from SQL Table

SQL Function to retrieve of PDF form from binary field in SQL Table

Retrieve PDF Form from 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

Function to retrieve fields as a Dictionary

The PDFHelper class iTextSharp code to extract Form Fields from PDF file Stream.

iTextSharp Dictionary with Field Names + Empty string

Retrieve Fields Values - Add a code to assign the SQL values to the FormFieldMap Dictionary.

Assign SQL data to form field map

Code for GeneratePDF function from Program.cs using the FormFieldMap dictionary and the Form Stream.

Function to generate the merged PDF using byte Form & formFieldMap

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 Program.cs

ReturnPDF in PDF Helper Class

ReturnPDF in the PDFHelper Class

Completed Merge PDF sample

Merged PDF with Data in Fields