.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.
The output in SSMS from the Stored Procedure.
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.)
function for SQL Server retrieval of PDF form
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
iText7 code in PDFHelper class to extract Form Fields from PDF file Stream.
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
Add the Values to the PDF form using the Key in the Dictionary passed into the function.
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 PDF Helper Class
Completed Merge PDF sample