PRODUCT COMPARISONS

iTextsharp HTML to PDF With CSS Styles C# Example vs IronPDF

Updated November 13, 2023
Share:

Converting an HTML file to PDF with CSS in C# is a common requirement in modern web and desktop application development. It enables developers to transform web pages, reports, or formatted documents into PDF files, which is crucial for tasks like generating invoices, creating printable reports, or archiving your web page content. In this comprehensive guide, we will explore two popular C#

Libraries for HTML to PDF Conversion

iTextSharp

iTextSharp is a renowned open-source library commonly used for creating and manipulating PDF documents in C#. Now, it is commonly known as iText7. While its primary focus is not HTML to PDF conversion, its robust feature set makes it a flexible option for this purpose. It provides pdfHTML, which is an add-on and allows developers with the power to generate, convert and customize PDF documents programmatically using string HTML snippets and CSS stylesheet.

While iText7 offers fine-grained control over the PDF generation process, it may require additional effort when dealing with images with complex HTML layouts and fonts with intricate CSS styling.

IronPDF

IronPDF, in contrast, is a dedicated C# library designed specifically for PDF generation and for HTML page to PDF format conversion. It focuses on simplifying the process of converting HTML content with CSS into PDF files, making it an excellent choice for straightforward and HTML to PDF tasks.

IronPDF's approach is ideal for scenarios where you require hassle-free conversion without encountering the complexities of PDF document construction. It simplifies the process for developers and allows for rapid implementation of new document with page breaks.

Installation and Setup

Before we dive into the details of the comparison, let's briefly discuss how to set up these libraries in your C# project.

Installing iTextSharp

To install iTextSharp (iText7) using the NuGet Package Manager, follow these steps:

  1. Create a new project in Visual Studio or open an existing one.
  2. Right-click on your project in the Solution Explorer and select "Manage NuGet Packages" or click on Tools menu and select NuGet Package Manager --> Manage NuGet Packages for Solution.

    iTextSharp HTML to PDF With CSS Styles C# Example vs (IronPDF) Figure 1 - To install iTextSharp in your Visual Studio project, right-click on the project in the Solution Explorer and select Manage NuGet Packages option.

    iTextSharp HTML to PDF With CSS Styles C# Example vs (IronPDF) Figure 2 - Another way to install iTextSharp in your Visual Studio project: Navigate to the Tools menu, select the NuGet Package Manager > Manage NuGet Packages for Solution.

  3. In the "NuGet Package Manager" window, switch to the "Browse" tab.
  4. Search for "iText7.pdfhtml" and select "iText7.pdfhtml" from the search results.

    iTextSharp HTML to PDF With CSS Styles C# Example vs (IronPDF) Figure 3 - In the Browse tab, enter itext7.pdf in the search box. Select iText7.pdfhtml from the search results and click on the Install button.

  5. Click the "Install" button to add the iText7 HTML2PDF converter to your project.
  6. One more important dependency is required to use HTML converter and i.e. "itext7.bouncy-castle-adapter". The itext7.bouncy-castle-adapter module is required for itext7.pdfhtml because it provides cryptographic algorithms and security features that are used in the process of creating and manipulating PDF documents.
  7. Browse for "itext7.bouncy-castle-adapter" and select "itext7.bouncy-castle-adapter to install."

    iTextSharp HTML to PDF With CSS Styles C# Example vs (IronPDF) Figure 4 - The itext7.bouncy-castle-adapter module is another dependency required to use HTML convertor with itext7.pdfhtml. In the Browse tab, enter itext7.bouncy-castle-adapter in the search box. Select itext7.bouncy-castle-adapter from the search results and click on the Install button.

Installing IronPDF

Installing IronPDF is equally straightforward:

  1. Create a new project in Visual Studio or open an existing one.
  2. Right-click on your project in the Solution Explorer and select "Manage NuGet Packages" or click on the Tools menu and select NuGet Package Manager -> Manage NuGet Packages for Solution.

    iTextSharp HTML to PDF With CSS Styles C# Example vs (IronPDF) Figure 5 - To install IronPDF in your Visual Studio project, Navigate to the Tools menu, select the NuGet Package Manager > Manage NuGet Packages for Solution.

    iTextSharp HTML to PDF With CSS Styles C# Example vs (IronPDF) Figure 6 - Another way to install IronPDF library in your Visual Studio project: Right-click on the project in the Solution Explorer and select Manage NuGet Packages option.

  3. In the "NuGet Package Manager" window, switch to the "Browse" tab.
  4. Search for "IronPDF" and select "IronPdf" from the search results.

    iTextSharp HTML to PDF With CSS Styles C# Example vs (IronPDF) Figure 7 - In the Browse tab, enter ironpdf in the search box. Select IronPdf from the search results and click on the Install button.

  5. Click the "Install" button to add IronPDF to your project.

Convert HTML to PDF with iTextSharp

iText7.pdfHTML provides a flexible approach to HTML to PDF conversion, granting developers control over the PDF generation process. Here is the source code for converting an HTML file to PDF:

using iText.Html2pdf;

// Input HTML page content with CSS styling
var html = "<html><head><style>body { font-family: Arial, sans-serif; }</style></head><body><h1>Hello, iText 7!</h1><p>This is a sample HTML to PDF conversion.</p></body></html>";

// Output PDF file path
string outputPdfPath = "results/output.pdf";

HtmlConverter.ConvertToPdf(html, new FileStream(outputPdfPath, FileMode.Create));
using iText.Html2pdf;

// Input HTML page content with CSS styling
var html = "<html><head><style>body { font-family: Arial, sans-serif; }</style></head><body><h1>Hello, iText 7!</h1><p>This is a sample HTML to PDF conversion.</p></body></html>";

// Output PDF file path
string outputPdfPath = "results/output.pdf";

HtmlConverter.ConvertToPdf(html, new FileStream(outputPdfPath, FileMode.Create));
Imports iText.Html2pdf

' Input HTML page content with CSS styling
Private html = "<html><head><style>body { font-family: Arial, sans-serif; }</style></head><body><h1>Hello, iText 7!</h1><p>This is a sample HTML to PDF conversion.</p></body></html>"

' Output PDF file path
Private outputPdfPath As String = "results/output.pdf"

HtmlConverter.ConvertToPdf(html, New FileStream(outputPdfPath, FileMode.Create))
VB   C#

The above code snippet demonstrates how to use iText 7's HtmlConverter to convert plain text or a specified HTML content with CSS styling into a PDF file. In CSS style tag, you can also set the font size along with more styles like background color, content disposition etc. It defines the input HTML content, specifies the output PDF file path, and then invokes the conversion process. The resulting PDF will be saved at the location specified in outputPdfPath.

iTextSharp HTML to PDF With CSS Styles C# Example vs (IronPDF) Figure 8 - Output PDF for HTMLtoPDF conversion using iText7.pdfhtml

While iText7 provides fine-grained style control, it may require additional code for complex HTML layouts or intricate CSS styling. However, it shines in scenarios where extensive PDF style customization is required beyond HTML conversion.

Convert HTML to PDF with IronPDF

IronPDF simplifies HTML to PDF conversion with a straightforward method for rendering HTML tags and CSS directly into a PDF document. Here's the code sample for converting HTML string to PDF page:

using IronPdf;

// Basic Example
// Instantiate Renderer
var renderer = new ChromePdfRenderer();

// Create a PDF from a HTML string using C#
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");

// Export to a file or Stream
pdf.SaveAs("output.pdf");

// Advanced Example with HTML Assets
// Load external html assets: Images, CSS and JavaScript.
// An optional BasePath 'C:\site\assets\' is set as the file location to load assets from
var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\");
myAdvancedPdf.SaveAs("html-with-assets.pdf");
using IronPdf;

// Basic Example
// Instantiate Renderer
var renderer = new ChromePdfRenderer();

// Create a PDF from a HTML string using C#
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");

// Export to a file or Stream
pdf.SaveAs("output.pdf");

// Advanced Example with HTML Assets
// Load external html assets: Images, CSS and JavaScript.
// An optional BasePath 'C:\site\assets\' is set as the file location to load assets from
var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\");
myAdvancedPdf.SaveAs("html-with-assets.pdf");
Imports IronPdf

' Basic Example
' Instantiate Renderer
Private renderer = New ChromePdfRenderer()

' Create a PDF from a HTML string using C#
Private pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")

' Export to a file or Stream
pdf.SaveAs("output.pdf")

' Advanced Example with HTML Assets
' Load external html assets: Images, CSS and JavaScript.
' An optional BasePath 'C:\site\assets\' is set as the file location to load assets from
Dim myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", "C:\site\assets\")
myAdvancedPdf.SaveAs("html-with-assets.pdf")
VB   C#

Here's a more detailed explanation of the above code:

Simplified Conversion Process

In basic example, IronPDF offers a simplified approach to HTML to PDF document conversion. Developers can achieve this in just a few lines of code, as demonstrated in source code above. The core steps include creating a PdfDocument and rendering HTML content directly into it.

iTextSharp HTML to PDF With CSS Styles C# Example vs (IronPDF) Figure 9 - Output PDF for simplified HTMLtoPDF conversion using IronPDF for .NET Library.

Advanced Example with CSS Assets

In advanced example, the code loads HTML elements content that includes an image (<img src='icons/iron.png'>) and specifies a base path for loading external assets (such as images, external CSS file, and JavaScript files) using the RenderHtmlAsPdf method. The resulting PDF, which includes the HTML content and its associated assets, is saved as "html-with-assets.pdf."

iTextSharp HTML to PDF With CSS Styles C# Example vs (IronPDF) Figure 10 - Output PDF for advanced HTMLtoPDF conversion using IronPDF for .NET Library.

Efficiency and Ease of Use

IronPDF's approach is ideal for scenarios where you require a hassle-free data conversion process without any need to dive into the complexities of PDF document construction nor it requires any other dependencies. It abstracts much of the underlying complexity and provides a user-friendly experience.

Conclusion

In this thorough comparison, we've explored two powerful libraries for HTML to PDF conversion in C#: iTextSharp and IronPDF.

iTextSharp, with its comprehensive PDF manipulation capabilities, provides developers with the tools for precise control over the PDF generation process. It excels in scenarios where complex PDF customizations are required beyond simple HTML to PDF conversion. On the other hand, IronPDF shines as a dedicated library designed specifically for effortless HTML to PDF conversion. It offers an intuitive and user-friendly approach, ideal for straightforward tasks that demand minimal coding effort.

To make the right choice between these two libraries, consider the complexity of your PDF requirements. If you need extensive customization and advanced PDF functionality, iTextSharp is the way to go. However, for quick and efficient HTML to PDF conversions without the need for intricate PDF modifications and other library dependencies, IronPDF is a highly efficient and user-friendly option. It also offers advanced PDF manipulation options.

IronPDF is free for development purposes, however it needs to be licensed for commercial-use. It also provides a free trial. Download the software from IronPDF's website.

< PREVIOUS
A Comparison Between Puppeteer & IronPDF in C#
NEXT >
How to Merge PDF Files Using iTextSharp

Ready to get started? Version: 2024.6 just released

Start Free Trial Total downloads: 9,580,872
View Licenses >