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

C# .Net Core iText7 Console Application for PDF Files

This article describes how to create a .Net Core Console Application to create an PDF file using iText7. The console application uses the iText7 package in the Repository to generate the Byte Stream and output SQL Server data into a PDF File.  The code for this application varies from the iTextSharp method as the PdfStamper class is no longer available in iText7.

 

Create a SQL Server Stored Procedure

Create a stored procedure in SQL Server from a database. This example uses a local database named Testing and a table named petTable with a few fields for the PDF merge.

TSQL Stored Procedure - AdventureWorks Database SSMS

The output in SSMS from the Stored Procedure.

TSQL Output SSMS

C# .Net Core Console Application

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 PDF Form from SQL Table

function for SQL Server retrieval of PDF form

Retrieve PDF Form from SQL Table

Function to retrieve iText7 Fields from PDF - Add a function to retrieve a Dictionary containing the iText7 field data for output to PDF from the Stream provided.

Code from Program.cs

Additional function for MemoryStream to retrieve fields in the Repository Class

iText7 code in PDFHelper class to extract Form Fields from PDF file Stream.

iText7 Dictionary with Field Names

Function to merge PDF File & Fields + Values - Add a function (GeneratePDF) to generate the merged PDF with args of the byte array containing the PDF form + Dictionary of Form Fields with Values.

Code for GeneratePDF function from Program.cs

Function to generate the merged PDF

Add the Values to the PDF form using the Key in the Dictionary passed into the function.

Function to return byte array of the file merged with the field data

The final function to call is ReturnPDF to write out the data from iText7 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