Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
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 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.
Explore IronPDF's capabilities as 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 documents with page breaks.
Before we dive into the details of the comparison, let's briefly discuss how to set up these libraries in your C# project.
To install iTextSharp (iText7) using the NuGet Package Manager, follow these steps:
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.
Search for "iText7.pdfhtml" and select "iText7.pdfhtml" from the search results.
Browse for "itext7.bouncy-castle-adapter" and select "itext7.bouncy-castle-adapter to install."
Installing IronPDF is equally straightforward:
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.
Search for "IronPDF" and select "IronPdf" from the search results.
iText7.pdfHTML provides a flexible approach to HTML to PDF conversion with iTextSharp, 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))
The above code snippet demonstrates how to use iText 7's HtmlConverter
to convert plain text or specified HTML content with CSS styling into a PDF file. In the 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
.
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.
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")
Here's a more detailed explanation of the above code:
In the 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 the source code above. The core steps include creating a PdfDocument
and rendering HTML content directly into it.
In the 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 files, 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."
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.
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.
Check out IronPDF's pricing and licensing details as it is free for development purposes, but needs to be licensed for commercial use. Download IronPDF for a free trial from IronPDF's website.
9 .NET API products for your office documents