Create PDF From Byte Array C# iTextSharp (vs IronPDF)

1.0 Introduction

Adobe developed the Portable Document Format to facilitate the sharing of text and image-based documents (PDF). To view a PDF image file, you must use a different application. Many businesses use PDF documents in today's culture for a variety of tasks, including the preparation of invoices and other paperwork.

Developers also use the existing PDF file format to produce documents or image file that adhere to client specifications. Fortunately, libraries that simplify the process have made producing PDFs simpler than ever. Consider factors like build, read, and convert capabilities when picking a library for your project in order to select the finest one that is readily available.

In this post, two of the most widely used Dot NET PDF libraries will be compared. They are:

In your Microsoft.NET application or project, you can create, read, and modify PDFs using either the IronPDF or iText PDF libraries. We will have a look at the capabilities of the 2 libraries first rather than shifting directly to the overall performance, fees for converting and handling the PDFs in order to determine which library is better for your application. Microsoft.NET Frameworks support both libraries. Additionally, each library's duration will be recorded for reference and later research. To know about the comparison, click here.

2. Library Features

2.1 iText PDF Features

A Java library and system that can convert text into PDF files is called iText PDF. Text adheres to the AGPL software licensing model. The AGPL software license is free and open-source.

  • An API for producing PDF files is available via the iText library.
  • Both HTML and XML strings can be parsed into PDF using the iText program's var reader.
  • We may add bookmarks, page numbers, and markers to our PDF documents using the iText library.
  • We can split a PDF file into multiple PDFs or combine multiple PDF files into a single PDF by using the iText library.
  • We can edit forms in PDFs using iText.
  • Using images from PNG, JPEG, and other image formats, iText can also make PDFs.
  • A Canvas class is offered by the iText library and can be used to draw different geometrical forms on pre-existing texts.
  • In PDF documents, iText provides a tool that allows you to add and edit fonts and images.

2.2 IronPDF Features

Developers can quickly produce, read, and change PDF files with the help of the robust IronPDF, a PDF .NET library. IronPDF has a Chrome engine at its core and offers a wealth of practical and potent capabilities, including the ability to convert HTML5, JavaScript, CSS, and picture files to PDF, add unique Headers and Footers, and produce PDFs precisely as they appear in a web browser. Various web and .NET formats, including HTML, ASPX, Razor View, and MVC, are supported by IronPDF. IronPDF's key attributes are as follows:

  • Easily creating, reading, and editing PDF files within Dot NET C# program.
  • Creating PDFs from a website URL link that has settings for User-Agents, Proxies, Cookies, HTTP Headers, and Form Variables to support login using HTML login forms.
  • Removing photos from already-existing PDF publications.
  • Adding text, photos, bookmarks, watermarks, and other elements to PDF files.
  • Features that make it simple to merge and divide the pages of several PDF documents.
  • The ability to transform media-type assets, including CSS files, into documents.

3.0 Install Library

3.1 Install iText7

Find iText first by using the NuGet Package Manager. iText7 and iText.pdfhtml must both be installed because the features of these packages are divided among numerous packages.

Create PDF From Byte Array C# iTextSharp (vs IronPDF) Figure 1 - iText7

Install the following packages as shown below if you prefer the Visual Studio Command-Line:

Install-Package itext7 && Install-Package itext7.pdfhtml

Since iText7 is the most recent version, we are using it in our solution.

3.2 Install IronPDF Library

As seen in the screenshot below, we can easily search for "IronPDF" in the Package Manager:

Create PDF From Byte Array C# iTextSharp (vs IronPDF) Figure 2 - IronPDF

The list of relevant search results is displayed in the above graphic. To install the package on your machine, please check the necessary boxes.

If you want to install the package using command line, add the following line into the terminal tab of the package manager:

:PackageInstall

The package is now prepared for usage and will download and install in the current project.

4.0 PDF Creation

4.1 Using iTextSharp

We can create PDF documents with public static byte arrays generated from the iText document doc. Below is the sample which help us to create a new document with the help of the iText PDF library.

using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using System.IO;
using iText.Kernel.Geom;
using iText.Html2pdf;

var html = "<h1>Hello world</h1>";
//byte array
byte[] result;
//create new MemoryStream using var ms
using (var ms = new MemoryStream())
{
    //new document
    var doc = new PdfDocument(new PdfWriter(ms));
    doc.SetDefaultPageSize(PageSize.A4);
    doc.SetTagged();
    HtmlConverter.ConvertToPdf(html, doc, new ConverterProperties());
    result = ms.ToArray();
}
File.WriteAllBytes(@"test.pdf", result);
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using System.IO;
using iText.Kernel.Geom;
using iText.Html2pdf;

var html = "<h1>Hello world</h1>";
//byte array
byte[] result;
//create new MemoryStream using var ms
using (var ms = new MemoryStream())
{
    //new document
    var doc = new PdfDocument(new PdfWriter(ms));
    doc.SetDefaultPageSize(PageSize.A4);
    doc.SetTagged();
    HtmlConverter.ConvertToPdf(html, doc, new ConverterProperties());
    result = ms.ToArray();
}
File.WriteAllBytes(@"test.pdf", result);
Imports iText.Kernel.Pdf
Imports iText.Layout
Imports iText.Layout.Element
Imports iText.Layout.Properties
Imports System.IO
Imports iText.Kernel.Geom
Imports iText.Html2pdf

Private html = "<h1>Hello world</h1>"
'byte array
Private result() As Byte
'create new MemoryStream var ms
Using ms = New MemoryStream()
	'new document
	Dim doc = New PdfDocument(New PdfWriter(ms))
	doc.SetDefaultPageSize(PageSize.A4)
	doc.SetTagged()
	HtmlConverter.ConvertToPdf(html, doc, New ConverterProperties())
	result = ms.ToArray()
End Using
File.WriteAllBytes("test.pdf", result)
VB   C#

The above example shows we are creating a PDF document from the given HTML string. First, we are creating MemoryStream to hold the data. Then we are creating PdfWriter and passing the MemoryStream as a parameter to the hold the data. Then we are creating the PDF file with the help of the HtmlConverter. After that, all the output has been saved in the MemoryStream. Then we are converting the data into bytes, and then we are using the File class to save the MemoryStream ms into a new file like the below output.

Create PDF From Byte Array C# iTextSharp (vs IronPDF) Figure 3 - Output

4.2 Using IronPDF

IronPDF makes PDF file creation very simple with few lines of code. Below is the sample code to create PDF file using IronPDF.

var html = "<h1>Hello world</h1>";
var pdf = new IronPdf.ChromePdfRenderer();
var bytedata=pdf.RenderHtmlAsPdf(html).BinaryData;
File.WriteAllBytes(@"test.pdf", bytedata);
var html = "<h1>Hello world</h1>";
var pdf = new IronPdf.ChromePdfRenderer();
var bytedata=pdf.RenderHtmlAsPdf(html).BinaryData;
File.WriteAllBytes(@"test.pdf", bytedata);
Dim html = "<h1>Hello world</h1>"
Dim pdf = New IronPdf.ChromePdfRenderer()
Dim bytedata=pdf.RenderHtmlAsPdf(html).BinaryData
File.WriteAllBytes("test.pdf", bytedata)
VB   C#

In the first step, we are creating an object for ChromePdfRenderer which helps to create PDF file. Then we are adding the HTML data into the PDF document using the method RenderHtmlAsPdf. Then we are converting the data into byte array. Last step is using the File which helps us to convert byte array to PDF file. The output will be like below.

Create PDF From Byte Array C# iTextSharp (vs IronPDF) Figure 4 - IronPDF Output

For more code tutorials, click here.

5.0 Conclusion

iText7 is one of the commonly used PDF libraries. iText code is so complex, and it is not suitable for beginners. We need a basic knowledge about the library to use this on user application. iText is generating PDF files with large space thus it may impact the user memory when they generate file with many pages. iText7 has divided the library into multiple packages unlike other packages. iText7 is having dependency with multiple packages. We need to download all the packages to make it work, it may increase the size of the application. Both a development license and a business license is free with iText. Click here to read more about the iText license.

On the other hand, IronPDF is easy and simple to use. With the few lines of code we are able to create PDF files. It is suitable for beginners and no basic knowledge is needed to use this application. IronPDF does not have any dependency with any other package. It is solo package that works all alone. Developers can choose from a variety of licenses to buy to suit their needs in addition to a free license that is provided. The $749 Lite edition does include all library features, such as a permanent license, a 30-day money-back guarantee, a year of software support, upgrades, and possibilities for SaaS and OEM redistribution. Additionally, it excludes recurring expenses. These licenses are one-time purchases that can be applied to development, staging, and production. Additionally, time-limited, non-distributable free licenses are available from IronPDF. Please click here for a detailed breakdown of IronPDF prices and licensing information.