iTextSharp C# HTML to PDF Alternative for .NET Core

IronPDF and iText 7 (formerly known as iTextSharp) both provide the ability to create, edit and print PDF files in .NET and .NET Core.

Which C# PDF library is best suited for your project? You can decide, as this article compares what each product can do and how they do it using the most common functions of each.

IronPDF URL to PDF
private void ExistingWebURL()
{
	// Create a PDF from any existing web page
	var Renderer = new IronPdf.HtmlToPdf();
	var PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format");

	// Create a PDF from an existing HTML
	Renderer.PrintOptions.MarginTop = 50;  //millimetres
	Renderer.PrintOptions.MarginBottom = 50;
	Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Print;
	Renderer.PrintOptions.Header = new SimpleHeaderFooter()
	{
		CenterText = "{pdf-title}",
		DrawDividerLine = true,
		FontSize = 16
	};
	Renderer.PrintOptions.Footer = new SimpleHeaderFooter()
	{
		LeftText = "{date} {time}",
		RightText = "Page {page} of {total-pages}",
		DrawDividerLine = true,
		FontSize = 14
	};

	Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Print;

	Renderer.PrintOptions.EnableJavaScript = true;
	Renderer.PrintOptions.RenderDelay = 500; //milliseconds

	PDF.SaveAs("wikipedia.pdf");
}
Private Sub ExistingWebURL()
	' Create a PDF from any existing web page
	Dim Renderer = New IronPdf.HtmlToPdf()
	Dim PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format")

	' Create a PDF from an existing HTML
	Renderer.PrintOptions.MarginTop = 50 'millimetres
	Renderer.PrintOptions.MarginBottom = 50
	Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Print
	Renderer.PrintOptions.Header = New SimpleHeaderFooter() With {
		.CenterText = "{pdf-title}",
		.DrawDividerLine = True,
		.FontSize = 16
	}
	Renderer.PrintOptions.Footer = New SimpleHeaderFooter() With {
		.LeftText = "{date} {time}",
		.RightText = "Page {page} of {total-pages}",
		.DrawDividerLine = True,
		.FontSize = 14
	}

	Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Print

	Renderer.PrintOptions.EnableJavaScript = True
	Renderer.PrintOptions.RenderDelay = 500 'milliseconds

	PDF.SaveAs("wikipedia.pdf")
End Sub
Jump to Article
iText URL to PDF
private void ExistingWebURL()
{
	//Initialize PDF writer
	PdfWriter writer = new PdfWriter("wikipedia.pdf");
	//Initialize PDF document
	PdfDocument pdf = new PdfDocument(writer);

	ConverterProperties properties = new ConverterProperties();
	properties.SetBaseUri("https://en.wikipedia.org/wiki/Portable_Document_Format");

	Document document = HtmlConverter.ConvertToDocument(new FileStream("Test_iText7_1.pdf", FileMode.Open), pdf, properties);

	Paragraph header = new Paragraph("HEADER")
		.SetTextAlignment(TextAlignment.CENTER)
		.SetFontSize(16);
	document.Add(header);

	for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
	{
		Rectangle pageSize = pdf.GetPage(i).GetPageSize();
		float x = pageSize.GetWidth() / 2;
		float y = pageSize.GetTop() - 20;
		document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
	}

	document.SetTopMargin(50);
	document.SetBottomMargin(50);

	document.Close();
}
Private Sub ExistingWebURL()
	'Initialize PDF writer
	Dim writer As New PdfWriter("wikipedia.pdf")
	'Initialize PDF document
	Dim pdf As New PdfDocument(writer)

	Dim properties As New ConverterProperties()
	properties.SetBaseUri("https://en.wikipedia.org/wiki/Portable_Document_Format")

	Dim document As Document = HtmlConverter.ConvertToDocument(New FileStream("Test_iText7_1.pdf", FileMode.Open), pdf, properties)

	Dim header As Paragraph = (New Paragraph("HEADER")).SetTextAlignment(TextAlignment.CENTER).SetFontSize(16)
	document.Add(header)

	Dim i As Integer = 1
	Do While i <= pdf.GetNumberOfPages()
		Dim pageSize As Rectangle = pdf.GetPage(i).GetPageSize()
'INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
		Dim x As Single = pageSize.GetWidth() / 2
		Dim y As Single = pageSize.GetTop() - 20
		document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0)
		i += 1
	Loop

	document.SetTopMargin(50)
	document.SetBottomMargin(50)

	document.Close()
End Sub
Jump to Article
IronPDF HTML to PDF
private void HTMLString()
{
	// Render any HTML fragment or document to HTML
	var Renderer = new IronPdf.HtmlToPdf();
	var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>");

	Renderer.PrintOptions.Footer = new HtmlHeaderFooter() { HtmlFragment = "<div style='text-align:right'><em style='color:pink'>page {page} of {total-pages}</em></div>" };

	var OutputPath = "HtmlToPDF.pdf";
	PDF.SaveAs(OutputPath);
	Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Screen;
}
Private Sub HTMLString()
	' Render any HTML fragment or document to HTML
	Dim Renderer = New IronPdf.HtmlToPdf()
	Dim PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>")

	Renderer.PrintOptions.Footer = New HtmlHeaderFooter() With {.HtmlFragment = "<div style='text-align:right'><em style='color:pink'>page {page} of {total-pages}</em></div>"}

	Dim OutputPath = "HtmlToPDF.pdf"
	PDF.SaveAs(OutputPath)
	Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Screen
End Sub
Jump to Article
iText HTML to PDF
private void HTMLString()
{
	HtmlConverter.ConvertToPdf("< h1 > Hello iText7 </ h1 >", new FileStream("HtmlToPDF.pdf", FileMode.Create));
}
Private Sub HTMLString()
	HtmlConverter.ConvertToPdf("< h1 > Hello iText7 </ h1 >", New FileStream("HtmlToPDF.pdf", FileMode.Create))
End Sub
Jump to Article
IronPDF ASPX to PDF
protected void Page_Load(object sender, EventArgs e)
{
	IronPdf.AspxToPdf.RenderThisPageAsPdf();
}
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
	IronPdf.AspxToPdf.RenderThisPageAsPdf()
End Sub
Jump to Article
iText ASPX to PDF
// ASPX webpage conversion support not available with iTextSharp.
' ASPX webpage conversion support not available with iTextSharp.
Jump to Article
IronPDF XML to PDF
private void XMLtoPDF(string XSLT, string XML)
{
	XslCompiledTransform transform = new XslCompiledTransform();
	using(XmlReader reader = XmlReader.Create(new StringReader(XSLT)))  
	{
		transform.Load(reader);
	}

	StringWriter results = new StringWriter();
	using(XmlReader reader = XmlReader.Create(new StringReader(XML))) 
	{
		transform.Transform(reader, null, results);
	}

	IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
	// options, headers, and footers may be set there
	// Render our XML as a PDF via XSLT
	Renderer.RenderHtmlAsPdf(results.ToString()).SaveAs("XMLtoPDF.pdf");
}
Private Sub XMLtoPDF(ByVal XSLT As String, ByVal XML As String)
	Dim transform As New XslCompiledTransform()
	Using reader As XmlReader = XmlReader.Create(New StringReader(XSLT))
		transform.Load(reader)
	End Using

	Dim results As New StringWriter()
	Using reader As XmlReader = XmlReader.Create(New StringReader(XML))
		transform.Transform(reader, Nothing, results)
	End Using

	Dim Renderer As New IronPdf.HtmlToPdf()
	' options, headers, and footers may be set there
	' Render our XML as a PDF via XSLT
	Renderer.RenderHtmlAsPdf(results.ToString()).SaveAs("XMLtoPDF.pdf")
End Sub
Jump to Article
iText XML to PDF
// XML file support not available with iTextSharp.
' XML file support not available with iTextSharp.
Jump to Article
IronPDF Create Chart
private void Chart()
{
	var Renderer = new HtmlToPdf();
	var PDF = Renderer.RenderUrlAsPdf("https://bl.ocks.org/mbostock/4062006");

	Renderer.PrintOptions.PaperSize = PdfPrintOptions.PdfPaperSize.A4;
	Renderer.PrintOptions.PaperOrientation = PdfPrintOptions.PdfPaperOrientation.Landscape;
	PDF.SaveAs("chart.pdf");
}
Private Sub Chart()
	Dim Renderer = New HtmlToPdf()
	Dim PDF = Renderer.RenderUrlAsPdf("https://bl.ocks.org/mbostock/4062006")

	Renderer.PrintOptions.PaperSize = PdfPrintOptions.PdfPaperSize.A4
	Renderer.PrintOptions.PaperOrientation = PdfPrintOptions.PdfPaperOrientation.Landscape
	PDF.SaveAs("chart.pdf")
End Sub
Jump to Article
iText Create Chart
private void Chart()
{
	//Initialize PDF writer
	PdfWriter writer = new PdfWriter("chart.pdf");
	//Initialize PDF document
	PdfDocument pdf = new PdfDocument(writer);

	ConverterProperties properties = new ConverterProperties();
	properties.SetBaseUri("https://bl.ocks.org/mbostock/4062006");

	Document document = HtmlConverter.ConvertToDocument(new FileStream("Test_iText7_1.pdf", FileMode.Open), pdf, properties);

	Paragraph header = new Paragraph("HEADER")
		.SetTextAlignment(TextAlignment.CENTER)
		.SetFontSize(16);
	document.Add(header);

	for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
	{
		Rectangle pageSize = pdf.GetPage(i).GetPageSize();
		float x = pageSize.GetWidth() / 2;
		float y = pageSize.GetTop() - 20;
		document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
	}

	document.Close();
}
Private Sub Chart()
	'Initialize PDF writer
	Dim writer As New PdfWriter("chart.pdf")
	'Initialize PDF document
	Dim pdf As New PdfDocument(writer)

	Dim properties As New ConverterProperties()
	properties.SetBaseUri("https://bl.ocks.org/mbostock/4062006")

	Dim document As Document = HtmlConverter.ConvertToDocument(New FileStream("Test_iText7_1.pdf", FileMode.Open), pdf, properties)

	Dim header As Paragraph = (New Paragraph("HEADER")).SetTextAlignment(TextAlignment.CENTER).SetFontSize(16)
	document.Add(header)

	Dim i As Integer = 1
	Do While i <= pdf.GetNumberOfPages()
		Dim pageSize As Rectangle = pdf.GetPage(i).GetPageSize()
'INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
		Dim x As Single = pageSize.GetWidth() / 2
		Dim y As Single = pageSize.GetTop() - 20
		document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0)
		i += 1
	Loop

	document.Close()
End Sub
Jump to Article
Try IronPDF free for development


Overview

iText 7 vs. IronPDF .NET Library

iTextSharp has been around for a very long time, so that should count in their favor a bit. But, in my humble opinion, it seems as if they are starting to lag behind. The libraries are based on Java, which is good, but .NET has been in existence for just about 20 years, and the .NET Framework keeps growing and expanding, opening up many possibilities – much more than what Java can provide easily. This is where IronPDF jumps forward as a great tool to manipulate PDF documents from any web framework. You can download IronPDF as an iTextSharp Alternative.

The rendering options of iText and IronPDF makes a huge difference. Not only with the speed and efficacy of the underlying PDF library, but also in the code when certain sections must be manipulated. Here, IronPDF wins again. Why? Let's compare each code segment to add headers and footers to a PDF document:

Add Headers and Footers to PDFs in C# with IronPDF

Renderer.PrintOptions.Header = new SimpleHeaderFooter()
{
    CenterText = "{pdf-title}",
    DrawDividerLine = true,
    FontSize = 16
};
Renderer.PrintOptions.Footer = new SimpleHeaderFooter()
{
    LeftText = "{date} {time}",
    RightText = "Page {page} of {total-pages}",
    DrawDividerLine = true,
    FontSize = 14
};
Renderer.PrintOptions.Header = New SimpleHeaderFooter() With {
	.CenterText = "{pdf-title}",
	.DrawDividerLine = True,
	.FontSize = 16
}
Renderer.PrintOptions.Footer = New SimpleHeaderFooter() With {
	.LeftText = "{date} {time}",
	.RightText = "Page {page} of {total-pages}",
	.DrawDividerLine = True,
	.FontSize = 14
}
VB   C#

iTextSharp Add PDF Headers & Footers

Paragraph header = new Paragraph("HEADER")
    .SetTextAlignment(TextAlignment.CENTER)
    .SetFontSize(16);
document.Add(header);

for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
{
    Rectangle pageSize = pdf.GetPage(i).GetPageSize();
    float x = pageSize.GetWidth() / 2;
    float y = pageSize.GetTop() - 20;
    document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
}

document.SetTopMargin(50);
document.SetBottomMargin(50);
Dim header As Paragraph = (New Paragraph("HEADER")).SetTextAlignment(TextAlignment.CENTER).SetFontSize(16)
document.Add(header)

Dim i As Integer = 1
Do While i <= pdf.GetNumberOfPages()
	Dim pageSize As Rectangle = pdf.GetPage(i).GetPageSize()
'INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
	Dim x As Single = pageSize.GetWidth() / 2
	Dim y As Single = pageSize.GetTop() - 20
	document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0)
	i += 1
Loop

document.SetTopMargin(50)
document.SetBottomMargin(50)
VB   C#

By quickly glancing at the code, you can see that IronPDF is easier, quicker, and looks more polished. iText doesn’t possess the inherent capability to add a header or footer onto a PDF document through the library itself, and does not allow for straight-forward PDF manipulation.

iTextSharp.dll uses a primarily programmatic model to render PDFs. When using iTextSharp PDF library, each piece of pdf text, graphic, table or line is “plotted” or drawn onto a PDF. This model allows precision, but many developers complain that it is time consuming to generate PDFs - and that it is very hard to closely match existing design styles or web assets. When viewing iTextSharp documentation, we see a port of a Java Library called "iText". In keeping, the methodology and programatic interface has a distinct Java flavor.

In contrast, Iron PDF uses an embedded, full Chromium based web browser renderer to convert HTML to PDF, including C# code examples, allowing developers to generate PDFs from HTML, images and CSS. This allows developers to work closely with existing web assets and also work in parallel with designers during a project. Although iText includes iTextSharp HTML to PDF functionality for C# .Net, developers report that it is not as far developed or dominant within the iTextSharp library.

The licensing options can also be a deal-breaker. iTextSharp is Open Source under the AGPL license agreement. This means that anyone who uses any part of an application using iTextSharp (privately or publicly) must get the application’s full source code. IronPDF is free for development, and can then be licensed for commercial deployment.

Read on for more comparative details on the different functionalities of these two libraries. I will talk about the pros and cons of each library’s methodology, and show the different ways both iText and IronPDF achieve the following goals:


IronPDF vs. iTextSharp

IronPDF iTextSharp
Convert HTML to PDF in C# using IronPDF Convert HTML to PDF in C# using iTextSharp
Convert HTML to PDF using IronPDF in MVC Convert HTML to PDF using iText7 in MVC
Export to PDF using IronPDF in C# Export to PDF using iTextSharp in C#
CSS Responsiveness CSS Responsiveness
Rendering by using an embedded Chrome based web browser Programmatic drawing model
IronPDF has explicit licenses for commercial or private usage AGLP strict open source licensing
Built in .NET natively Based on a Java library


Key Differences

Generate PDF from HTML using IronPDF

IronPDF enables .NET and .NET Core developers to generate, merge, split, edit, and extract pdf content easily in C#, F#, and VB.Net for .NET Core and .NET Framework, as well as create PDFs from HTML, ASPX, CSS, JS, and image files.

It makes use of an embedded, full Chromium based web browser renderer to convert HTML to PDF. This allows developers to generate PDFs from HTML, images, and CSS and to work closely with existing web assets and also work in parallel with designers during a project.

IronPDF really focuses on developer productivity. The library simplifies many common complex PDF code tasks into convenient C# methods to extract text and images, sign PDFS, edit PDFS with new HTML and more, without the developer needing to study the PDF document standard to understand how to achieve their best result.

1. IronPDF Features

  • Generating PDF documents from HTML, images and ASPX files
  • Reading PDF text
  • Extracting data and images from PDFs
  • Merging PDF documents
  • Splitting PDFs
  • Manipulating PDFs

iTextSharp HTML to PDF Conversion

The iTextSharp.dll uses a primarily programmatic model to render PDFs, and it has advanced PDF manipulation APIs that are powerful and follow the PDF standard closely.

2. iTextSharp Documentation Features

  • AGLP strict open source licensing
  • Programmatic drawing model
  • Edit and Read PDFs
  • Solid functionality for PDF manipulation
  • Based on a Java library

Let's compare by creating an example project utilizing both libraries and let the code do the talking!


Example Project

Create an ASP.NET Project

Make use of the following steps to create an ASP.NET website:

  1. Open Visual Studio
  2. Click File New Project
  3. Select Web under Visual C# in the Project type listbox
  4. Select ASP.NET Web Application

Figure 1New Project

  • Click OK

  • On the next screen, select Web Forms as shown in Figure 2 underneath

Figure 2Web Forms

  • Click OK

Now we have something to work with. Let’s Install IronPDF.


Get Started

3. IronPDF C# Library Quick Installation

In order to make use of IronPDF, you first need to install it (free). There are two options:

  • NuGet
  • Download the library

Let’s have a closer look.

C# PDF DLL

Download DLL

Download DLL

Manually install into your project

or
C# Nuget Library for PDF

Install with NuGet

Install-Package IronPdf

3.1. Install using NuGet

There are three ways to install the IronPDF NuGet package:

  1. Visual Studio
  2. Developer Command Prompt
  3. Download the NuGet Package directly

Let’s do them one-by-one.

3.2. Visual Studio

Visual Studio provides the NuGet Package Manager for you to install NuGet packages in your projects. You can access it via the Project Menu, or by right clicking your project in the Solution Explorer. Both these options are shown below in Figures 3 and 4

Figure 3Project menu

Figure 4Right click Solution Explorer

After you have clicked Manage NuGet Packages from either option, Browse for the IronPDF package and install it as shown in Figure 5.

Figure 5Install IronPDF NuGet Package

3.3. Developer Command Prompt

The following steps opens the Developer Command Prompt and installs the IronPDF NuGet package

  1. Search for your Developer Command Prompt – it is usually under your Visual Studio folder
  2. Type in the following command: PM > Install-Package IronPdf
  3. Press Enter
  4. The package will be installed
  5. Reload your Visual Studio project

3.4. Download the NuGet Package directly

In order to download the NuGet package:

  1. Navigate to https://www.nuget.org/packages/IronPdf/
  2. Click on Download Package
  3. After the package has downloaded, double click it
  4. Reload your Visual Studio project

3.5. Download the .DLL Library

The second way to install IronPDF is by direct download.

Figure 6Download IronPDF library

Reference the Library in your project by using the next steps:

  1. Right click the Solution in the Solution Explorer
  2. Select References
  3. Browse for the IronPDF.dll library
  4. Click OK

Now that you’re set up, we can start playing with the awesome features in the IronPDF library after the setup for iTextSharp.

Install iTextSharp by using NuGet

There are three ways to install the iTextSharp NuGet package, they are:

  • Visual Studio
  • Developer Command Prompt
  • Download the NuGet Package directly

Let’s do them one-by-one.

For Visual Studio, search for iText and install the relevant packages, as shown next.

Figure 7iText

Or, in the Developer Command Prompt (as shown previously, enter the following command)

  • PM > Install-Package itext7

Or, download iText 7 directly from their website.

Now that you have created the necessary projects, let’s compare these two libraries in code.


Compare the Code

4. Create a PDF from an Existing URL

The following code downloads a webpage and converts it to a PDF document. I have included page Header and Footer options as well.

4.1. IronPDF Website to PDF

The following code is using IronPDF to create a PDF document directly from a website address. Custom Headers and Footers are also included.

private void ExistingWebURL()
{
    // Create a PDF from any existing web page
    var Renderer = new IronPdf.HtmlToPdf();
    var PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format");

    // Create a PDF from an existing HTML
    Renderer.PrintOptions.MarginTop = 50;  //millimetres
    Renderer.PrintOptions.MarginBottom = 50;
    Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Print;
    Renderer.PrintOptions.Header = new SimpleHeaderFooter()
    {
        CenterText = "{pdf-title}",
        DrawDividerLine = true,
        FontSize = 16
    };
    Renderer.PrintOptions.Footer = new SimpleHeaderFooter()
    {
        LeftText = "{date} {time}",
        RightText = "Page {page} of {total-pages}",
        DrawDividerLine = true,
        FontSize = 14
    };

    Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Print;

    Renderer.PrintOptions.EnableJavaScript = true;
    Renderer.PrintOptions.RenderDelay = 500; //milliseconds

    PDF.SaveAs("wikipedia.pdf");
}
Private Sub ExistingWebURL()
	' Create a PDF from any existing web page
	Dim Renderer = New IronPdf.HtmlToPdf()
	Dim PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format")

	' Create a PDF from an existing HTML
	Renderer.PrintOptions.MarginTop = 50 'millimetres
	Renderer.PrintOptions.MarginBottom = 50
	Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Print
	Renderer.PrintOptions.Header = New SimpleHeaderFooter() With {
		.CenterText = "{pdf-title}",
		.DrawDividerLine = True,
		.FontSize = 16
	}
	Renderer.PrintOptions.Footer = New SimpleHeaderFooter() With {
		.LeftText = "{date} {time}",
		.RightText = "Page {page} of {total-pages}",
		.DrawDividerLine = True,
		.FontSize = 14
	}

	Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Print

	Renderer.PrintOptions.EnableJavaScript = True
	Renderer.PrintOptions.RenderDelay = 500 'milliseconds

	PDF.SaveAs("wikipedia.pdf")
End Sub
VB   C#

4.2. iText7 URL to PDF

The following code uses iText7 to create a PDF document directly from a website address and add headers and footers.

private void ExistingWebURL()
{
    //Initialize PDF writer
    PdfWriter writer = new PdfWriter("wikipedia.pdf");
    //Initialize PDF document
    PdfDocument pdf = new PdfDocument(writer);

    ConverterProperties properties = new ConverterProperties();
    properties.SetBaseUri("https://en.wikipedia.org/wiki/Portable_Document_Format");

    Document document = HtmlConverter.ConvertToDocument(new FileStream("Test_iText7_1.pdf", FileMode.Open), pdf, properties);

    Paragraph header = new Paragraph("HEADER")
        .SetTextAlignment(TextAlignment.CENTER)
        .SetFontSize(16);
    document.Add(header);

    for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
    {
        Rectangle pageSize = pdf.GetPage(i).GetPageSize();
        float x = pageSize.GetWidth() / 2;
        float y = pageSize.GetTop() - 20;
        document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
    }

    document.SetTopMargin(50);
    document.SetBottomMargin(50);

    document.Close();
}
Private Sub ExistingWebURL()
	'Initialize PDF writer
	Dim writer As New PdfWriter("wikipedia.pdf")
	'Initialize PDF document
	Dim pdf As New PdfDocument(writer)

	Dim properties As New ConverterProperties()
	properties.SetBaseUri("https://en.wikipedia.org/wiki/Portable_Document_Format")

	Dim document As Document = HtmlConverter.ConvertToDocument(New FileStream("Test_iText7_1.pdf", FileMode.Open), pdf, properties)

	Dim header As Paragraph = (New Paragraph("HEADER")).SetTextAlignment(TextAlignment.CENTER).SetFontSize(16)
	document.Add(header)

	Dim i As Integer = 1
	Do While i <= pdf.GetNumberOfPages()
		Dim pageSize As Rectangle = pdf.GetPage(i).GetPageSize()
'INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
		Dim x As Single = pageSize.GetWidth() / 2
		Dim y As Single = pageSize.GetTop() - 20
		document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0)
		i += 1
	Loop

	document.SetTopMargin(50)
	document.SetBottomMargin(50)

	document.Close()
End Sub
VB   C#

4.3. Code Comparison

With iText 7, it took a bit longer in code to convert the document at the given URL to PDF. Two lines of code are needed:

MemoryStream wiki = GetStreamFromUrl("https://en.wikipedia.org/wiki/Tiger");

HtmlConverter.ConvertToPdf(wiki, new FileStream("wikipedia.pdf",FileMode.OpenOrCreate));
Dim wiki As MemoryStream = GetStreamFromUrl("https://en.wikipedia.org/wiki/Tiger")

HtmlConverter.ConvertToPdf(wiki, New FileStream("wikipedia.pdf",FileMode.OpenOrCreate))
VB   C#

A MemoryStream object as well as a FileStream object have to be created with set properties. I personally felt it a bit cumbersome.

Let's have a look at IronPDF.

IronPDF needed three lines of code (if you include the SaveAs method at the bottom of the code segment as well), but otherwise just two lines were needed:

var Renderer = new IronPdf.HtmlToPdf();
var PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format");
Dim Renderer = New IronPdf.HtmlToPdf()
Dim PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format")
VB   C#

Quick and to the point. No need for a FileStream or an additional .NET object, as all the functionality seems to be built-in to the RenderUrlAsPdf method.

4.4. Output Comparison

I am including an Output comparison now, as this should apply to all the following exercises that we’ll do during this tutorial.

With this code segment we have transformed the Tiger Wikipedia webpage to PDF with both libraries.

4.5. iTextSharp 7 File Output

The file that was output by using iText’s library has 49 pages. It didn’t render JavaScript nor CSS (as far as I can tell). The resulting output is shown below:

Figure 8iText Tiger Wiki page

4.6. IronPDF File Output

The file that was output by using IronPDF’s library has 12 pages. It rendered JavaScript and CSS quite well. The resulting output is shown below:

Figure 9IronPDF Tiger Wiki Page

A picture tells a thousand words…


5. Generate PDF from HTML Input String

The next code creates a PDF document and prints an HTML string inside it.

5.1. IronPDF Document from HTML

The following code makes use of IronPDF to generate a PDF containing HTML input.

private void HTMLString()
{
    // Render any HTML fragment or document to HTML
    var Renderer = new IronPdf.HtmlToPdf();
    var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>");

    Renderer.PrintOptions.Footer = new HtmlHeaderFooter() { HtmlFragment = "<div style='text-align:right'><em style='color:pink'>page {page} of {total-pages}</em></div>" };

    var OutputPath = "HtmlToPDF.pdf";
    PDF.SaveAs(OutputPath);
    Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Screen;
}
Private Sub HTMLString()
	' Render any HTML fragment or document to HTML
	Dim Renderer = New IronPdf.HtmlToPdf()
	Dim PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>")

	Renderer.PrintOptions.Footer = New HtmlHeaderFooter() With {.HtmlFragment = "<div style='text-align:right'><em style='color:pink'>page {page} of {total-pages}</em></div>"}

	Dim OutputPath = "HtmlToPDF.pdf"
	PDF.SaveAs(OutputPath)
	Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Screen
End Sub
VB   C#

5.2. iText 7 HTML to PDF

The following code is using iText7 to create a PDF containing HTML text.

private void HTMLString()
{
    HtmlConverter.ConvertToPdf("< h1 > Hello iText7 </ h1 >", new FileStream("HtmlToPDF.pdf", FileMode.Create));
}
Private Sub HTMLString()
	HtmlConverter.ConvertToPdf("< h1 > Hello iText7 </ h1 >", New FileStream("HtmlToPDF.pdf", FileMode.Create))
End Sub
VB   C#

5.3. Code Comparison

iText makes use of the HtmlConverter.ConvertToPdf call again to send an HTML string to be output as a PDF.

IronPDF makes use of its RenderHtmlAsPdf method which is specifically designed to work with HTML and PDF.

Both options are quite quick and to the point, but IronPDF has a more meaningful method name.


6. Convert ASPX Pages to PDF

The next code creates a PDF document from an ASPX page.

6.1. IronPDF Render PDF from ASPX

The following code makes use of IronPDF to create a PDF containing from an ASPX file.

protected void Page_Load(object sender, EventArgs e)
{
    IronPdf.AspxToPdf.RenderThisPageAsPdf();
}
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
	IronPdf.AspxToPdf.RenderThisPageAsPdf()
End Sub
VB   C#

6.2. iTextSharp ASPX to PDF

It seems as if the pdfHTML library of iText7 does not support creating PDFs from ASPX web pages. Why? pdfHTML and iText are not aware of the following HTML frameworks:

  • ASP.Net
  • JSP
  • MVC
  • Razor
  • Struts
  • Spring
  • And the following Framework-specific abstractions such as:
    • DataGridViews
    • Repeaters
    • Templates
    • Views

The developer should get the HTML from the framework, then the pdfHTML add-on will accept that HTML for conversion to PDF.

// ASPX webpage conversion support not available with iTextSharp. 
' ASPX webpage conversion support not available with iTextSharp.
VB   C#


7. Convert XML to PDF

The following code takes XML and converts it to PDF

7.1. IronPDF Creates PDF from XML

private void XMLtoPDF(string XSLT, string XML)
{
    XslCompiledTransform transform = new XslCompiledTransform();
    using(XmlReader reader = XmlReader.Create(new StringReader(XSLT)))  
    {
        transform.Load(reader);
    }

    StringWriter results = new StringWriter();
    using(XmlReader reader = XmlReader.Create(new StringReader(XML))) 
    {
        transform.Transform(reader, null, results);
    }

    IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
    // options, headers, and footers may be set there
    // Render our XML as a PDF via XSLT
    Renderer.RenderHtmlAsPdf(results.ToString()).SaveAs("XMLtoPDF.pdf");
}
Private Sub XMLtoPDF(ByVal XSLT As String, ByVal XML As String)
	Dim transform As New XslCompiledTransform()
	Using reader As XmlReader = XmlReader.Create(New StringReader(XSLT))
		transform.Load(reader)
	End Using

	Dim results As New StringWriter()
	Using reader As XmlReader = XmlReader.Create(New StringReader(XML))
		transform.Transform(reader, Nothing, results)
	End Using

	Dim Renderer As New IronPdf.HtmlToPdf()
	' options, headers, and footers may be set there
	' Render our XML as a PDF via XSLT
	Renderer.RenderHtmlAsPdf(results.ToString()).SaveAs("XMLtoPDF.pdf")
End Sub
VB   C#

The structure of the XSLT file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <p>Titles:
    <xsl:for-each select="catalog/cd">
      <xsl:value-of select="title"/>
      <xsl:if test="position() < last()-1">
        <xsl:text>, </xsl:text>
      </xsl:if>
      <xsl:if test="position()=last()-1">
        <xsl:text>, and </xsl:text>
      </xsl:if>
      <xsl:if test="position()=last()">
        <xsl:text>!</xsl:text>
      </xsl:if>
    </xsl:for-each>
    </p>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>
HTML

7.2. iTextSharp XML to PDF

iText's support for processing XML files has been removed a long time ago. Developers are encouraged to use the globally recognized XML standard, which is XHTML, instead.

In order for iText to support an XML format, iText would need first create its own propriety DTD and/or XML Schema.

// XML file support not available with iTextSharp. 
' XML file support not available with iTextSharp.
VB   C#


8. Create a Live Chart Based on External Input

The next code obtains data from an external source and creates a chart accordingly.

8.1. IronPDF Chart Creation

The following code uses IronPDF to quickly create a chart and set the page properties.

private void Chart()
{
    var Renderer = new HtmlToPdf();
    var PDF = Renderer.RenderUrlAsPdf("https://bl.ocks.org/mbostock/4062006");

    Renderer.PrintOptions.PaperSize = PdfPrintOptions.PdfPaperSize.A4;
    Renderer.PrintOptions.PaperOrientation = PdfPrintOptions.PdfPaperOrientation.Landscape;
    PDF.SaveAs("chart.pdf");
}
Private Sub Chart()
	Dim Renderer = New HtmlToPdf()
	Dim PDF = Renderer.RenderUrlAsPdf("https://bl.ocks.org/mbostock/4062006")

	Renderer.PrintOptions.PaperSize = PdfPrintOptions.PdfPaperSize.A4
	Renderer.PrintOptions.PaperOrientation = PdfPrintOptions.PdfPaperOrientation.Landscape
	PDF.SaveAs("chart.pdf")
End Sub
VB   C#

8.2. iText C# Charts

The following code uses iText7 to create a chart and set properties.

private void Chart()
{
    //Initialize PDF writer
    PdfWriter writer = new PdfWriter("chart.pdf");
    //Initialize PDF document
    PdfDocument pdf = new PdfDocument(writer);

    ConverterProperties properties = new ConverterProperties();
    properties.SetBaseUri("https://bl.ocks.org/mbostock/4062006");

    Document document = HtmlConverter.ConvertToDocument(new FileStream("Test_iText7_1.pdf", FileMode.Open), pdf, properties);

    Paragraph header = new Paragraph("HEADER")
        .SetTextAlignment(TextAlignment.CENTER)
        .SetFontSize(16);
    document.Add(header);

    for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
    {
        Rectangle pageSize = pdf.GetPage(i).GetPageSize();
        float x = pageSize.GetWidth() / 2;
        float y = pageSize.GetTop() - 20;
        document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
    }

    document.Close();
}
Private Sub Chart()
	'Initialize PDF writer
	Dim writer As New PdfWriter("chart.pdf")
	'Initialize PDF document
	Dim pdf As New PdfDocument(writer)

	Dim properties As New ConverterProperties()
	properties.SetBaseUri("https://bl.ocks.org/mbostock/4062006")

	Dim document As Document = HtmlConverter.ConvertToDocument(New FileStream("Test_iText7_1.pdf", FileMode.Open), pdf, properties)

	Dim header As Paragraph = (New Paragraph("HEADER")).SetTextAlignment(TextAlignment.CENTER).SetFontSize(16)
	document.Add(header)

	Dim i As Integer = 1
	Do While i <= pdf.GetNumberOfPages()
		Dim pageSize As Rectangle = pdf.GetPage(i).GetPageSize()
'INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
		Dim x As Single = pageSize.GetWidth() / 2
		Dim y As Single = pageSize.GetTop() - 20
		document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0)
		i += 1
	Loop

	document.Close()
End Sub
VB   C#


9. Is there a Free iTextSharp License? (not really)

The biggest difference between IronPDF's and iText's licensing options is the fact that iTextSharp is Open Source under the AGPL license agreement. In short (as quoted, "The AGPL license differs from the other GNU licenses in that it was built for network software. You can distribute modified versions if you keep track of the changes and the date you made them. As per usual with GNU licenses, you must license derivatives under AGPL. It provides the same restrictions and freedoms as the GPLv3 but with an additional clause which makes it so that source code must be distributed along with web publication. Since web sites and services are never distributed in the traditional sense, the AGPL is the GPL of the web."

In layman's terms, this means that anyone who uses any part of an application using iTextSharp - even over a local network OR over the internet, must be given the application’s full source code. This license is often used for highly academic works that are intended to stay academic, and also for open source projects who intend paid usage for software deployed outside of academic environments. The nature of the AGPL license agreement makes the open source iTextSharp license difficult for commercial use, unless a private license can be arranged and legally negotiated with the developers.

IronPDF, on the other hand, is an openly commercial C# PDF library. It is free for development and can always be licensed for commercial deployment. This clearer license model does not require developers to learn the ins and outs of GNU / AGPL license models and can instead focus on their projects. Licenses are available for single project use, single developers, agencies and global corporations, and SaaS and OEM redistribution. No legal fees negotiated, just straight forward licensing.


Tutorial Quick Access

Download this Project on GitHub

You can access and share all the source code from this comparison tutorial in C# on GitHub.

IronPDF and iTextSharp Code Comparison

Get the C# PDF Quickstart Handbook

We created a free PDF resource guide to help make developing PDFs easier for .NET, with walk-throughs of common functions and examples for manipulating, editing, generating, and saving PDFS in C# and VB.NET for your project.

Download the Guide

Explore the IronPDF Object Reference

Explore the Object Reference for IronPDF C# Library, including details of all of IronPDF’s features, classes, method fields, namespaces, and enums.

View the Object Reference