iTextSharp C# HTML to PDF Alternative for .NET CorePhoto by Sergey Zolkin

 

IronPDF and iText 7 (formerly known as iTextSharp) both provide the ability generate, manipulate, and print PDFs in .NET and .NET Core.

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

Code Examples

IronPDF URL to PDFVB C#

  1. private void ExistingWebURL()
  2. {
  3. // Create a PDF from any existing web page
  4. var Renderer = new IronPdf.HtmlToPdf();
  5. var PDF = Renderer.RenderUrlAsPdf(“https://en.wikipedia.org/wiki/Portable_Document_Format”);
  6. // Create a PDF from an existing HTML
  7. Renderer.PrintOptions.MarginTop = 50; //millimetres
  8. Renderer.PrintOptions.MarginBottom = 50;
  9. Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Print;
  10. Renderer.PrintOptions.Header = new SimpleHeaderFooter()
  11. {
  12. CenterText = “{pdf-title}”,
  13. DrawDividerLine = true,
  14. FontSize = 16
  15. };
  16. Renderer.PrintOptions.Footer = new SimpleHeaderFooter()
  17. {
  18. LeftText = “{date} {time}”,
  19. RightText = “Page {page} of {total-pages}”,
  20. DrawDividerLine = true,
  21. FontSize = 14
  22. };
  23. Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Print;
  24. Renderer.PrintOptions.EnableJavaScript = true;
  25. Renderer.PrintOptions.RenderDelay = 500; //milliseconds
  26. PDF.SaveAs(“wikipedia.pdf”);
  27. }

Compare IronPDF to iTextSharp C#

 

Overview

C# Library Comparison

IronPdf is:

  • .NET First
  • Openly commercial with published pricing
  • Focuses on rendering PDF’s from HTML so that developers do not need to learn how PDF’s work
  • A great choice for pragmatic coders trying to get a job done.

iText (iTextSharp) is:

  • Java First
  • Very much Open Source. We may call them, for a quote for use other than in strict open source AGLP projects.
  • Focuses on rendering PDF’s using a programmatic API based around how PDFs work internally
  • A great choice for free and academic projects

iText 7 vs. IronPDF .NET Library

iTextSharp has been around for at least 6 years, based on an open source Java codebase called iText, and still has somewhat of a Java flavor. Developers who first learned Java may find this library familiar.

IronPDF is a .NET-first library with an API designed around ease of use in Visual Studio. .NET has been in existence for almost 20 years, continually growing and expanding, and opening up many possibilities, which IronPDF is designed to leverage. It allows us to create and manipulate PDF documents in .NET framework projects. You can download IronPDF as an iTextSharp Alternative.

The rendering API of iText and IronPDF are quite different. 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

  1. Renderer.PrintOptions.Header = new SimpleHeaderFooter()
  2. {
  3. CenterText = “{pdf-title}”,
  4. DrawDividerLine = true,
  5. FontSize = 16
  6. };
  7. Renderer.PrintOptions.Footer = new SimpleHeaderFooter()
  8. {
  9. LeftText = “{date} {time}”,
  10. RightText = “Page {page} of {total-pages}”,
  11. DrawDividerLine = true,
  12. FontSize = 14
  13. };

Copy code to clipboardVB C#

iTextSharp Add PDF Headers & Footers

  1. Paragraph header = new Paragraph(“HEADER”)
  2. .SetTextAlignment(TextAlignment.CENTER)
  3. .SetFontSize(16);
  4. document.Add(header);
  5. for (int i = 1; i <= pdf.getnumberofpages(); i++)< li>
  6. {
  7. Rectangle pageSize = pdf.GetPage(i).GetPageSize();
  8. float x = pageSize.GetWidth() / 2;
  9. float y = pageSize.GetTop() – 20;
  10. document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
  11. }
  12. document.SetTopMargin(50);
  13. document.SetBottomMargin(50);

Copy code to clipboardVB C#

By quickly glancing at the code, you can see that IronPDF is pragmatic, based on common end user requirements.

iText is a lower level library which focuses on a drawing API where we add objects, shapes, and text to pages.

iTextSharp.dll uses a primarily programmatic model to render PDFs. When using the iTextSharp PDF library, each piece of PDF text, graphic, table or line is “plotted” or drawn onto a PDF. The API appears low level and is focused on the PDF document standard. This model allows precision, but may require developers to learn a little about how PDFs work. Closely matching an existing design styles or web assets may take some of iteration and reading the iTextSharp documentation. In keeping with its heritage, the methodology and programmatic interface has a distinct Java flavor.

In contrast, Iron PDF uses a full embedded web browser renderer to convert HTML to PDF. Following short (1- and 2-line) C# code examples, developers can generate PDFs from existing or new HTML, images and CSS. This allows developers to work closely with existing web assets and also work in parallel with designers during a project. iText does include HTML to PDF functionality for C# .NET, though it is not, apparently, the library’s dominant feature.

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:

1. Licensing

Licensing options are also an important factor to developer projects. iTextSharp is Open Source under the AGPL license agreement. When licensed under AGLP, anyone who uses any part of an application which contains iTextSharp (even across a company network or the internet) may be entitled to a full copy of the app’s full source code. This is excellent for academic work. If we wish to use iTextSharp in commercial applications it is best practice to contact iText and ask them for a quote on the pricing for iText commercial usage.

IronPDF is proprietary software free for development, and can then be licensed for commercial deployment at publicly published, reasonable prices starting at $399.

IronPDF and iTextSharp

For me personally, this is how the 2 libraries stack up:

IronPDFiTextSharpConvert HTML to PDF via a full built-in web browserBasic HTML to PDF via a pdfHTML add-onRendering focus: Embedded web browserRendering focus: Programmatic drawing modelIronPDF has explicit licenses with published pricesAGPL! Commercial use pricing not published. Easy to Code with .NET First DesignBased on a Java API Not suited to academic assignments and coursework Excellent for academic assignments and research

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 a fully embedded web browser 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.

2. IronPDF Features

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.

  • 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

2. iTextSharp Documentation Features

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.

  • 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:

 

 

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 Library 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

Manually install into your project

orC# Nuget Library for PDF

Install with NuGet

Install-Package IronPdfnuget.org/packages/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.

  1. private void ExistingWebURL()
  2. {
  3. // Create a PDF from any existing web page
  4. var Renderer = new IronPdf.HtmlToPdf();
  5. var PDF = Renderer.RenderUrlAsPdf(“https://en.wikipedia.org/wiki/Portable_Document_Format”);
  6. // Create a PDF from an existing HTML
  7. Renderer.PrintOptions.MarginTop = 50; //millimetres
  8. Renderer.PrintOptions.MarginBottom = 50;
  9. Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Print;
  10. Renderer.PrintOptions.Header = new SimpleHeaderFooter()
  11. {
  12. CenterText = “{pdf-title}”,
  13. DrawDividerLine = true,
  14. FontSize = 16
  15. };
  16. Renderer.PrintOptions.Footer = new SimpleHeaderFooter()
  17. {
  18. LeftText = “{date} {time}”,
  19. RightText = “Page {page} of {total-pages}”,
  20. DrawDividerLine = true,
  21. FontSize = 14
  22. };
  23. Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Print;
  24. Renderer.PrintOptions.EnableJavaScript = true;
  25. Renderer.PrintOptions.RenderDelay = 500; //milliseconds
  26. PDF.SaveAs(“wikipedia.pdf”);
  27. }

Copy code to clipboardVB 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.

  1. private void ExistingWebURL()
  2. {
  3. //Initialize PDF writer
  4. PdfWriter writer = new PdfWriter(“wikipedia.pdf”);
  5. //Initialize PDF document
  6. PdfDocument pdf = new PdfDocument(writer);
  7. ConverterProperties properties = new ConverterProperties();
  8. properties.SetBaseUri(“https://en.wikipedia.org/wiki/Portable_Document_Format”);
  9. Document document = HtmlConverter.ConvertToDocument(new FileStream(“Test_iText7_1.pdf”, FileMode.Open), pdf, properties);
  10. Paragraph header = new Paragraph(“HEADER”)
  11. .SetTextAlignment(TextAlignment.CENTER)
  12. .SetFontSize(16);
  13. document.Add(header);
  14. for (int i = 1; i <= pdf.getnumberofpages(); i++)< li>
  15. {
  16. Rectangle pageSize = pdf.GetPage(i).GetPageSize();
  17. float x = pageSize.GetWidth() / 2;
  18. float y = pageSize.GetTop() – 20;
  19. document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
  20. }
  21. document.SetTopMargin(50);
  22. document.SetBottomMargin(50);
  23. document.Close();
  24. }

Copy code to clipboardVB C#

4.3. Code Comparison

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

  1. MemoryStream wiki = GetStreamFromUrl(“https://en.wikipedia.org/wiki/Tiger”);
  2. HtmlConverter.ConvertToPdf(wiki, new FileStream(“wikipedia.pdf”,FileMode.OpenOrCreate));

Copy code to clipboardVB 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:

  1. var Renderer = new IronPdf.HtmlToPdf();
  2. var PDF = Renderer.RenderUrlAsPdf(“https://en.wikipedia.org/wiki/Portable_Document_Format”);
  3. PDF.SaveAs(“wikipedia.pdf”)

Copy code to clipboardVB C#

No need for a FileStream or an additional .NET object, as all the functionality seems to be built-in to the IronPdf 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…. IronPDF shines at HTML to PDF rendering.

 

 

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.

  1. private void HTMLString()
  2. {
  3. // Render any HTML fragment or document to HTML
  4. var Renderer = new IronPdf.HtmlToPdf();
  5. var PDF = Renderer.RenderHtmlAsPdf(“

    Hello IronPdf

    ”);
  6. Renderer.PrintOptions.Footer = new HtmlHeaderFooter() { HtmlFragment = “
    page {page} of {total-pages}
    ” };
  7. var OutputPath = “HtmlToPDF.pdf”;
  8. PDF.SaveAs(OutputPath);
  9. Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Screen;
  10. }

Copy code to clipboardVB C#

5.2. iText 7 HTML to PDF

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

  1. private void HTMLString()
  2. {
  3. HtmlConverter.ConvertToPdf(“< h1 > Hello iText7 ”, new FileStream(“HtmlToPDF.pdf”, FileMode.Create));
  4. }

Copy code to clipboardVB 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 allowed a lot of control over the rendering process and even using HTML to add headers and footers to PDF Pages.

 

 

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. The Web form becomes a dynamic PDF by adding 1 line of code to the Page_Load event. IronPdf.AspxToPdf.RenderThisPageAsPdf();

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. IronPdf.AspxToPdf.RenderThisPageAsPdf();
  4. }

Copy code to clipboardVB 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, from what I could discover. This functionality would need to be coded on a project-for-project basis.

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

 

 

7. Convert XML to PDF

The following code takes XML and converts it to PDF

7.1. IronPDF Creates PDF from XML

  1. private void XMLtoPDF(string XSLT, string XML)
  2. {
  3. XslCompiledTransform transform = new XslCompiledTransform();
  4. using(XmlReader reader = XmlReader.Create(new StringReader(XSLT)))
  5. {
  6. transform.Load(reader);
  7. }
  8. StringWriter results = new StringWriter();
  9. using(XmlReader reader = XmlReader.Create(new StringReader(XML)))
  10. {
  11. transform.Transform(reader, null, results);
  12. }
  13. IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
  14. // options, headers, and footers may be set there
  15. // Render our XML as a PDF via XSLT
  16. Renderer.RenderHtmlAsPdf(results.ToString()).SaveAs(“XMLtoPDF.pdf”);
  17. }

Copy code to clipboardVB C#

The structure of the XSLT file is as follows:

  1. xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>
  2. My CD Collection

  3. Titles:

  4. ,
  5. , and
  6. !

Copy code to clipboardHTML

7.2. iTextSharp XML to PDF

iText’s support for processing XML to PDF may require custom development work.

 

 

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 using HTML to PDF.

  1. private void Chart()
  2. {
  3. var Renderer = new HtmlToPdf();
  4. var PDF = Renderer.RenderUrlAsPdf(“https://bl.ocks.org/mbostock/4062006”);
  5. Renderer.PrintOptions.PaperSize = PdfPrintOptions.PdfPaperSize.A4;
  6. Renderer.PrintOptions.PaperOrientation = PdfPrintOptions.PdfPaperOrientation.Landscape;
  7. PDF.SaveAs(“chart.pdf”);
  8. }

Copy code to clipboardVB C#

8.2. iText C# Charts

The following code uses iText7 to create a chart and set properties. We can see a programmatic drawing-api style.

  1. private void Chart()
  2. {
  3. //Initialize PDF writer
  4. PdfWriter writer = new PdfWriter(“chart.pdf”);
  5. //Initialize PDF document
  6. PdfDocument pdf = new PdfDocument(writer);
  7. ConverterProperties properties = new ConverterProperties();
  8. properties.SetBaseUri(“https://bl.ocks.org/mbostock/4062006”);
  9. Document document = HtmlConverter.ConvertToDocument(new FileStream(“Test_iText7_1.pdf”, FileMode.Open), pdf, properties);
  10. Paragraph header = new Paragraph(“HEADER”)
  11. .SetTextAlignment(TextAlignment.CENTER)
  12. .SetFontSize(16);
  13. document.Add(header);
  14. for (int i = 1; i <= pdf.getnumberofpages(); i++)< li>
  15. {
  16. Rectangle pageSize = pdf.GetPage(i).GetPageSize();
  17. float x = pageSize.GetWidth() / 2;
  18. float y = pageSize.GetTop() – 20;
  19. document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
  20. }
  21. document.Close();
  22. }

Copy code to clipboardVB C#

 

 

9. Is there a Free iTextSharp License for Commercial Use?

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.”

As i understand it, this means that if I use any part of an application using iTextSharp – even over a local network OR over the internet, all of my application’s full source code must be freely available to every user. That is not always in a project’s best interest.

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 many commercial use cases, 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 clear 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.

10. Summary

From my experience I discovered:

IronPdf is:

  • .Net First with an intuitive API for C# and VB developers
  • Openly commercial with published pricing
  • Focuses on rendering PDF’s from HTML so that developers do not need to learn how PDF’s work
  • A great choice for pragmatic coders trying to get a job done efficiantly.

iText (iTextSharp) is:

  • Java First
  • Very much Open Source. We can call them for a quote for use other than in strict open source AGLP projects.
  • Focuses on rendering PDF’s using a programmatic API based around how PDFs work internally
  • A great choice for free and academic projects – and also for highly technical commercial PDF applications on high budget projects.

Information contained on this page is provided by an independent third-party content provider. Frankly and this Site make no warranties or representations in connection therewith. If you are affiliated with this page and would like it removed please contact [email protected]