Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
Converting HTML to PDF is a crucial task in many modern software development workflows, whether it's for generating reports, invoices, or creating documentation. As a C# developer, you have several libraries at your disposal to streamline this process.
In this article, we'll compare two of the most popular libraries within the .NET ecosystem: IronPDF and iTextSharp. Both libraries offer robust functionality, but they differ in key areas such as ease of use, PDF creation tools, CSS styling support, and licensing. Whether you're a beginner or a seasoned developer, this guide will help you understand their core features and decide which one is the best fit for you based off your needs and project requirements.
Want to follow along? Download the IronPDF free trial to explore IronPDF's powerful features for yourself.
Both iTextSharp and IronPDF provide developers with the tools they need for HTML to PDF conversion in C#. However, each has its own strengths and weaknesses.
iTextSharp is an open-source library that has been around for a long time. It offers flexibility and extensive customization options, but it can be somewhat complex to set up and use, especially when dealing with advanced HTML and CSS rendering. However, it should be noted that iTextSharp is a legacy product and only receives security-related updates now.
Before we dive into how to convert HTML to PDF format with these two libraries, let's first look at a basic example comparison which shows the difference in how these two libraries handle converting CSS and JavaScript Heavy web page/HTML content into a PDF document.
As you can see from the resulting PDF documents, this demonstrates how iTextSharp can really only process the raw HTML content from the provided URL, whereas IronPDF is capable of maintaining the original CSS layout and styling, ensuring the generated PDF document closely resembles the original web page.
Install-Package iTextSharp
Install-Package iTextSharp
Then, search for the iTextSharp library and click "Install".
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
Install-Package IronPdf
Install-Package IronPdf
Install via NuGet Package Manager: Alternatively, you can install it via the NuGet package manager for Solution screen just as we did in the steps above for iTextSharp. However, this time, search for IronPDF, before clicking "Install".
using IronPdf;
using IronPdf;
It is important to remember that IronPDF requires a license key for commercial use/use outside of development.
Once iTextSharp is set up, you need to also ensure you have the itextsharp.xmlworker package installed within your project before you can start to create PDFs from HTML content. However, you'll face challenges with more complex HTML, especially when CSS styles are involved. iTextSharp tends to require additional effort to achieve perfect styling when compared to IronPDF. Its important to remember that iTextSharp handles basic HTML/CSS2 only (no flexbox, no grid, limited CSS).
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
string html = @"
<html>
<head>
<style>
body { font-family: Arial; color: #333; }
h1 { background: #3498db; color: #fff; padding: 10px; }
table { width: 100%; border-collapse: collapse; margin-top: 15px; }
th, td { border: 1px solid #ccc; padding: 6px; }
th { background: #2980b9; color: #fff; }
.footer { margin-top: 20px; font-size: 12px; color: #777; text-align: center; }
</style>
</head>
<body>
<h1>April Report</h1>
<p>Here’s a quick overview of this month’s performance metrics.</p>
<table>
<tr><th>Metric</th><th>Value</th></tr>
<tr><td>Features</td><td>12</td></tr>
<tr><td>Bugs Fixed</td><td>89</td></tr>
</table>
<p class='footer'>Generated by iTextSharp</p>
</body>
</html>";
using (FileStream stream = new FileStream("report.pdf", FileMode.Create))
{
Document pdfDoc = new Document(PageSize.A4, 25, 25, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
using (StringReader sr = new StringReader(html))
{
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
}
pdfDoc.Close();
writer.Close();
}
Console.WriteLine("PDF generation completed successfully.");
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
string html = @"
<html>
<head>
<style>
body { font-family: Arial; color: #333; }
h1 { background: #3498db; color: #fff; padding: 10px; }
table { width: 100%; border-collapse: collapse; margin-top: 15px; }
th, td { border: 1px solid #ccc; padding: 6px; }
th { background: #2980b9; color: #fff; }
.footer { margin-top: 20px; font-size: 12px; color: #777; text-align: center; }
</style>
</head>
<body>
<h1>April Report</h1>
<p>Here’s a quick overview of this month’s performance metrics.</p>
<table>
<tr><th>Metric</th><th>Value</th></tr>
<tr><td>Features</td><td>12</td></tr>
<tr><td>Bugs Fixed</td><td>89</td></tr>
</table>
<p class='footer'>Generated by iTextSharp</p>
</body>
</html>";
using (FileStream stream = new FileStream("report.pdf", FileMode.Create))
{
Document pdfDoc = new Document(PageSize.A4, 25, 25, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
using (StringReader sr = new StringReader(html))
{
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
}
pdfDoc.Close();
writer.Close();
}
Console.WriteLine("PDF generation completed successfully.");
Output PDF File
IronPDF makes the PDF generation process simple, able to easily convert HTML content into new PDF documents in just a few lines of code, as demonstrated in the code sample below. It's capable of working with advanced HTML documents with CSS files used for styling, HTML strings, and CSS/JavaScript-heavy web content.
Here's a simple example that includes inline CSS:
using IronPdf;
class Program
{
static void Main()
{
var content = @"
<html>
<head>
<style>
body {
font-family: 'Segoe UI', sans-serif;
margin: 40px;
background-color: #f8f9fa;
}
h1 {
color: #2c3e50;
border-bottom: 2px solid #2980b9;
padding-bottom: 10px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table, th, td {
border: 1px solid #ccc;
}
th {
background-color: #2980b9;
color: white;
padding: 10px;
}
td {
padding: 10px;
background-color: #ecf0f1;
}
.footer {
margin-top: 40px;
text-align: center;
font-size: 0.9em;
color: #7f8c8d;
}
img {
width: 120px;
float: right;
}
</style>
</head>
<body>
<img src='https://ironpdf.com/img/svgs/iron-pdf-logo.svg' alt='Company Logo' />
<h1>Monthly Report - April 2025</h1>
<p>This report outlines performance statistics and key updates for the development team.</p>
<table>
<tr>
<th>Metric</th>
<th>Value</th>
<th>Change</th>
</tr>
<tr>
<td>Feature Releases</td>
<td>12</td>
<td style='color: green;'>+20%</td>
</tr>
<tr>
<td>Bugs Resolved</td>
<td>89</td>
<td style='color: green;'>+45%</td>
</tr>
<tr>
<td>Downtime</td>
<td>2 hrs</td>
<td style='color: red;'>+15%</td>
</tr>
</table>
<div class='footer'>
Generated with IronPDF | © 2025 DevCorp
</div>
</body>
</html>";
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("AdvancedStyledReport.pdf");
}
}
using IronPdf;
class Program
{
static void Main()
{
var content = @"
<html>
<head>
<style>
body {
font-family: 'Segoe UI', sans-serif;
margin: 40px;
background-color: #f8f9fa;
}
h1 {
color: #2c3e50;
border-bottom: 2px solid #2980b9;
padding-bottom: 10px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table, th, td {
border: 1px solid #ccc;
}
th {
background-color: #2980b9;
color: white;
padding: 10px;
}
td {
padding: 10px;
background-color: #ecf0f1;
}
.footer {
margin-top: 40px;
text-align: center;
font-size: 0.9em;
color: #7f8c8d;
}
img {
width: 120px;
float: right;
}
</style>
</head>
<body>
<img src='https://ironpdf.com/img/svgs/iron-pdf-logo.svg' alt='Company Logo' />
<h1>Monthly Report - April 2025</h1>
<p>This report outlines performance statistics and key updates for the development team.</p>
<table>
<tr>
<th>Metric</th>
<th>Value</th>
<th>Change</th>
</tr>
<tr>
<td>Feature Releases</td>
<td>12</td>
<td style='color: green;'>+20%</td>
</tr>
<tr>
<td>Bugs Resolved</td>
<td>89</td>
<td style='color: green;'>+45%</td>
</tr>
<tr>
<td>Downtime</td>
<td>2 hrs</td>
<td style='color: red;'>+15%</td>
</tr>
</table>
<div class='footer'>
Generated with IronPDF | © 2025 DevCorp
</div>
</body>
</html>";
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("AdvancedStyledReport.pdf");
}
}
Output PDF File
With IronPDF, you can expect a polished PDF file with accurate CSS styling, as seen with the above code example, making it a go-to for many developers working with complex HTML files, strings, and more. Beyond simple HTML to PDF document conversion tasks, IronPDF is capable of advanced PDF manipulation tasks and PDF security. This makes it an excellent all-in-one PDF library.
When evaluating iTextSharp and IronPDF, it's important to consider not just their features, but also the competitive landscape. Other competitors like Apryse and Aspose.PDF offer similar HTML to PDF solutions, but with their own trade-offs in terms of pricing and functionality.
Feature | IronPDF | iTextSharp | Apryse | Aspose.PDF |
---|---|---|---|---|
Ease of Use | High | Medium | High | Medium |
CSS Support | Full | Partial | Full | Full |
Licensing | Commercial | Open Source | Commercial | Commercial |
Support | Excellent | Community | Premium | Premium |
Pricing | From $749 | Free/Commercial license | Quote-based | From $1, 679 per year |
IronPDF stands out due to its comprehensive support for modern HTML5 and CSS3, which is critical for most developers today. Its extended support for manipulating PDF documents, control over PDF document creation, ease with the conversion process, and more, make IronPDF a popular PDF library.
In conclusion, both IronPDF and iTextSharp offer strong capabilities for HTML to PDF conversion in C#, but they cater to different types of developers. If you’re looking for an open-source solution with strong community support, iTextSharp may be the right choice. However, for developers who need ease of use, robust CSS support, and a commercial solution, IronPDF offers a far more streamlined and feature-rich experience. Whether you're looking for a tool that can automate generating invoices, create branded PDF documents, or convert entire web pages to PDF files, IronPDF has you covered.
Try IronPDF’s user-friendly features today – download the free trial for a firsthand experience and see how easy HTML to PDF conversion can be.