In this article, we will see how we can convert a string of data to PDF and then send email with attached copy of generated PDF in C#.
Firstly, we can convert the string of data to PDF by using Popular Library for rendering PDF in ItextSharp. Secondly, we can download/save the converted PDF by using HTTP Response Class which provides response to client and contains information about response in the form of headers and other piece of necessary information.
So, lets start to build our first step,
Step 1: Convert HTML String to PDF,
In this step we will first create a button which will do the rest of the work on Click event.
Let's create the button to perform the required operation.
The UI view looks like the following:
![]()
So our front end is all set and we need to apply the cs logic to perform operation.
Let's start building HTML string.
I am using StringBuilder class for generating HTML string and pass to the parser for generating PDF. Before proceeding further add the following references.
Now let's write the code for generating in-memory PDF from HTML string.
Now let's understand the Line of code. After building the string we can read from the string as we have passed the generated string.
We are building the PDF document with default page size of A4 Page size.
Parse the HTML string using HTMLWorker of Itextsharp library,
Use the memory stream to reside the file in-memory.
Now we get the PDF and memory stream to create the instance and write the document. Then first open the document, parse by the html worker and then after completing the work close the document (dispose off the resources) managing the resource properly.
Now we add the created document to the memory stream and use the bytes of it as a in-memory reference to later attach to the email.
This is all about first step which will generate the PDF file and we will later use this as an attachment.
First Output
Now let's proceed to the second step:
Step 2
In the next step we will see how we can download the in memory generated PDF File.
As we are using the stream so that we use response method to send the information to the client.
Final Output
Now I will compare the ironPDF Library with itextsharp for Converting HTML to PDF, so that we can get better idea which one library convert it in better way without losing formatting.
C# HTML to PDF rendering is undertaken by a fully functional version of the Google Chromium engine, embedded within IronPDF DLL.
Lets Start, the Configuration of IronPDF in our project.
Step 1
Open Visual Studio and Create New Dot Net Web App Project By Pressing Ctrl + Shift + N Combination of Keys or File --> New --> Project.
![]()
Lets Name the project as per your business need as its demo so I have mentioned this name.
![]()
After creating new project, Lets Start adding Nuget package of IronPDF,
Open nuget packager and type IronPDF and select to install or go to the above link and use command interface to install.
![]()
or using Package Manager Console of nuget Manager using this command
Install-Package IronPdf
![]()
Now we All set to configure IronPDF in our project.
![]()
Step 2
Once you have IronPDF installed and referenced in you project you can start using it right away by typing a couple of code lines: regarding how to convert HTML to PDF in C#.
But Firstly, Lets Create Button for Export into PDF
Then, Create instance of ChromerPdfRenderer.
Secondly, Add couple of lines to convert html to pdf.
And the perfect (without losing formatting) exported pdf preview is shown below:
![]()
Lets now compare the difference between itextsharp and IronPDF rendering below:
iText is a lower level library which focuses on a drawing API where we add objects, shapes, and text to pages.
While IronPDF uses a full embedded web browser renderer to convert HTML to PDF.
At the end a key difference with HTML to PDF between C# iTextSharp and IronPDF is that IronPDF has more advanced and accurate HTML-To-PDF rendering by using an embedded Chrome based web browser.
Read more articles on C#: