Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
PDF stands for "Portable Document Format", a file format developed by Adobe. PDFs are useful for presenting documents that require the formatting of text and images contained within them. PDF files play an important role in the modern world and are used in all types of business sectors for purposes such as invoicing and document generation. Today, PDF generation has become almost intuitive thanks to the PDF libraries now available on the market. When deciding to use a PDF library for your project, it is important to consider the features and advantages of each in order to be able to select the library best suited to you.
In this article, we will compare two of the most popular .NET PDF libraries, these are:
The IronPDF and iText PDF libraries are both used to create, read, and modify PDF files in your Microsoft .NET application or project. In order to decide which library is best for your application, we are going to compare the features of the two libraries before moving on to the performance costs for converting and manipulating the PDFs. Both libraries are supported by Microsoft .NET Frameworks. We will also log the time duration of each library for analysis.
IronPDF is a powerful PDF .NET library used by developers to easily create, read and modify PDFs. Internally, IronPDF uses a chromium engine and includes many useful and powerful features such as converting HTML5, JavaScript, CSS, and image files to PDF, adding custom Headers and Footers, and rendering PDFs exactly as they are displayed in a browser. IronPDF supports various web and net formats such as HTML, ASPX, Razor View, and MVC. The main features of IronPDF include:
iText PDF is an open-source Java library that can convert text into PDF documents. iText follows an AGPL license software scheme. APGL is a free and open-source software license.
In this article, we are going to use a console application to generate PDF documents. To begin, open the Microsoft Visual Studio application and select “new project” from the file menu, then select “Console Application”.
Enter a project name and select a file path. Then, click the Create button. Also, select the desired .NET Framework, as shown in the screenshot below:
Microsoft Visual Studio will now generate the structure for the selected application. If you have selected the console, Windows, and web application now it will open the program.cs file where you can enter the code and build/run the application.
Select the .NET framework version. In this example, we are going to use .NET 5.0
Next, we can add the library to test the code.
The IronPDF Library can be downloaded and installed in four ways:
Visual Studio provides the NuGet Package Manager option to install the package directly to the solution. The screenshot below shows how to open the NuGet Package Manager.
It provides a search box to find packages from the NuGet website. In the package manager we can simply search for "IronPDF", as shown in the screenshot below:
In the image above we see the list of the related search results. Please select the required options in order to install the package to your system.
In Visual Studio Tool Go to Tools-> NuGet Package manager -> Package manager console
Enter the following line into the package manager console tab:
Install-Package IronPdf
The package will now download and install in the current project ready to be used.
The third way is to download the NuGet package directly from the website.
Click this link to download the latest package directly from our website. After downloading, follow these steps to add the package to your project:
The iText library can be downloaded and installed in four ways:
The first three methods are the same for both the IronPDF and iText libraries. The only differing method. It is important to note that when adding iText through Visual Studio, you must install two packages.
First, search for iText on the NuGet package manager. We need to install both itext7 and iText.pdfhtml. because these packages have their features split into several packages.
If you prefer the Visual Command-Line, the packages to be installed are the following:
Install-Package itext7
Install-Package itext7.pdfhtml
iText 7 is the latest version, so we are using that version in our solution.
Source code will be available on GitHub here for iText 7.
We can get iText 7 here.
You can obtain the source code which has all the class files related to iText 7 and include it on the solution as a reference in the solution.
Both the PDF library and the webpage-source-to-PDF-converter are useful. Let's take a look at how we can make a PDF file from a webpage URL.
We can easily create a PDF with IronPDF. It will generate a webpage source file from a URL and convert it to a new document.
The following steps make it easy to create a PDF document.
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
namespace PdfConsoleApp
{
internal class Program
{
public static void Main(string [] args)
{
IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
var Pdf = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/%22Hello,_World!%22_program");
Pdf.SaveAs("result.pdf");
// or optionally:
var Renderer = new IronPdf.ChromePdfRenderer().
RenderUrlAsPdf("https://en.wikipedia.org/wiki/%22Hello,_World!%22_program").SaveAs("result.pdf");
}
}
}
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
namespace PdfConsoleApp
{
internal class Program
{
public static void Main(string [] args)
{
IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
var Pdf = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/%22Hello,_World!%22_program");
Pdf.SaveAs("result.pdf");
// or optionally:
var Renderer = new IronPdf.ChromePdfRenderer().
RenderUrlAsPdf("https://en.wikipedia.org/wiki/%22Hello,_World!%22_program").SaveAs("result.pdf");
}
}
}
Imports System.Diagnostics
Imports System.IO
Imports System.Net
Imports System.Text
Namespace PdfConsoleApp
Friend Class Program
Public Shared Sub Main(ByVal args() As String)
Dim Renderer As New IronPdf.ChromePdfRenderer()
Dim Pdf = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/%22Hello,_World!%22_program")
Pdf.SaveAs("result.pdf")
' or optionally:
Dim Renderer = (New IronPdf.ChromePdfRenderer()).RenderUrlAsPdf("https://en.wikipedia.org/wiki/%22Hello,_World!%22_program").SaveAs("result.pdf")
End Sub
End Class
End Namespace
Two methods have been included above to convert a webpage URL into a PDF document and save it as in the example above. The first method is to create a document by constructing an IronPDF PDF document, while the other method is to render the URL and save it as a PDF in the same line. This downloads the page source into a chunk and converts into documents to speed up the process.
In the example above, we use RenderUrlAsPdf to convert the webpage into a document. Only the link and the save location must be supplied. The PDF conversion takes 7.3 seconds to complete.
iText PDF also allows us to create a PDF document. On iText, we need to WebClient function while we need to generate PDF from the URL. client function which allows us to download the URL into a string then we can able to convert the string into a PDF file.
In iText, we need different classes to generate a PDF document. PdfWriter is a class that allows us to create an empty file at the given location and create an object for that class. PdfDocument is also a class that will help us to read a PDF and write it into a PDF-document object. PageSize is also a class that helps us to handle the page-setting of the document.
ConverterProperties is a class that is used to format the web page string in the PDF document there are different to set the width of the page etc.,
HtmlConverter is a class from the namespace iText.Html2pdf. The HTML converter is a class that has a function called "ConvertTopdf". This is a static function that allows us to write the downloaded web page into a PDF file. But, we need to pass the Pdfwriter and ConverterProperties object as parameters to the HTML converter.
using iText.Html2pdf;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.StyledXmlParser.Css.Media;
namespace PdfConsoleApp
{
internal class Program
{
public static void Main(string [] args)
{
PdfWriter writer = new PdfWriter("itext.pdf");
PdfDocument pdf = new PdfDocument(writer);
PageSize pageSize = new PageSize(850, 1700);
pdf.SetDefaultPageSize(pageSize);
ConverterProperties properties = new ConverterProperties();
MediaDeviceDescription mediaDeviceDescription =
new MediaDeviceDescription(MediaType.SCREEN);
mediaDeviceDescription.SetWidth(pageSize.GetWidth());
properties.SetMediaDeviceDescription(mediaDeviceDescription);
string strdownload = new WebClient().DownloadString("https://en.wikipedia.org/wiki/%22Hello,_World!%22_program");
HtmlConverter.ConvertToPdf(strdownload, pdf, properties);
}
}
}
using iText.Html2pdf;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.StyledXmlParser.Css.Media;
namespace PdfConsoleApp
{
internal class Program
{
public static void Main(string [] args)
{
PdfWriter writer = new PdfWriter("itext.pdf");
PdfDocument pdf = new PdfDocument(writer);
PageSize pageSize = new PageSize(850, 1700);
pdf.SetDefaultPageSize(pageSize);
ConverterProperties properties = new ConverterProperties();
MediaDeviceDescription mediaDeviceDescription =
new MediaDeviceDescription(MediaType.SCREEN);
mediaDeviceDescription.SetWidth(pageSize.GetWidth());
properties.SetMediaDeviceDescription(mediaDeviceDescription);
string strdownload = new WebClient().DownloadString("https://en.wikipedia.org/wiki/%22Hello,_World!%22_program");
HtmlConverter.ConvertToPdf(strdownload, pdf, properties);
}
}
}
Imports iText.Html2pdf
Imports iText.Kernel.Geom
Imports iText.Kernel.Pdf
Imports iText.StyledXmlParser.Css.Media
Namespace PdfConsoleApp
Friend Class Program
Public Shared Sub Main(ByVal args() As String)
Dim writer As New PdfWriter("itext.pdf")
Dim pdf As New PdfDocument(writer)
Dim pageSize As New PageSize(850, 1700)
pdf.SetDefaultPageSize(pageSize)
Dim properties As New ConverterProperties()
Dim mediaDeviceDescription As New MediaDeviceDescription(MediaType.SCREEN)
mediaDeviceDescription.SetWidth(pageSize.GetWidth())
properties.SetMediaDeviceDescription(mediaDeviceDescription)
Dim strdownload As String = (New WebClient()).DownloadString("https://en.wikipedia.org/wiki/%22Hello,_World!%22_program")
HtmlConverter.ConvertToPdf(strdownload, pdf, properties)
End Sub
End Class
End Namespace
Result:
Here is the generated PDF from IronPDF and iText. The left-hand side PDF is generated from IronPDF, and the right-hand side PDF file is generated by iText. Below is the time is taken to generate the PDF.
The results will change depending on network bandwidth.
Both IronPDF and iText provide a way to convert HTML strings into PDF. Both libraries provide an easy way to do this.
We can transform the webpage source into a document with the help of IronPDF. The following is an example of how to turn an HTML string into a document. It also has the ability to turn any tag element into a PDF document.
var Renderer = new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("<h1>Hello world!!</h1>").SaveAs("result.pdf");
var Renderer = new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("<h1>Hello world!!</h1>").SaveAs("result.pdf");
Dim Renderer = (New IronPdf.ChromePdfRenderer()).RenderHtmlAsPdf("<h1>Hello world!!</h1>").SaveAs("result.pdf")
The preceding example demonstrates how to use RenderHtmlAsPdf to transform an HTML string. In addition, we can supply any number of HTML strings to the function that converts webpage source to a string. We can save the document after retrieving the string using the Save as function. It takes 2.7 seconds to complete the process.
With the help of iText PDF, we can convert webpage source strings into PDF. The following is an example of how to create a document from a webpage source string. It is also capable of converting any tag element into a new document.
PdfWriter writer = new PdfWriter("itext.pdf");
PdfDocument pdf = new PdfDocument(writer);
PageSize pageSize = new PageSize(850, 1700);
pdf.SetDefaultPageSize(pageSize);
ConverterProperties properties = new ConverterProperties();
MediaDeviceDescription mediaDeviceDescription =
new MediaDeviceDescription(MediaType.SCREEN);
mediaDeviceDescription.SetWidth(pageSize.GetWidth());
properties.SetMediaDeviceDescription(mediaDeviceDescription);
HtmlConverter.ConvertToPdf("<h1>Hello world!!</h1>", pdf, properties);
PdfWriter writer = new PdfWriter("itext.pdf");
PdfDocument pdf = new PdfDocument(writer);
PageSize pageSize = new PageSize(850, 1700);
pdf.SetDefaultPageSize(pageSize);
ConverterProperties properties = new ConverterProperties();
MediaDeviceDescription mediaDeviceDescription =
new MediaDeviceDescription(MediaType.SCREEN);
mediaDeviceDescription.SetWidth(pageSize.GetWidth());
properties.SetMediaDeviceDescription(mediaDeviceDescription);
HtmlConverter.ConvertToPdf("<h1>Hello world!!</h1>", pdf, properties);
Dim writer As New PdfWriter("itext.pdf")
Dim pdf As New PdfDocument(writer)
Dim pageSize As New PageSize(850, 1700)
pdf.SetDefaultPageSize(pageSize)
Dim properties As New ConverterProperties()
Dim mediaDeviceDescription As New MediaDeviceDescription(MediaType.SCREEN)
mediaDeviceDescription.SetWidth(pageSize.GetWidth())
properties.SetMediaDeviceDescription(mediaDeviceDescription)
HtmlConverter.ConvertToPdf("<h1>Hello world!!</h1>", pdf, properties)
This is similar to the URL-to-PDF conversion. The only one step we need to remove is the WebClient, and instead, we need to use the web-page source string in the parameter. This will fetch the given string and convert it into a new document.
Result:
We can read PDF documents using IronPDF and EO.pdf.
IronPDF helps us to read existing PDF files. Below is the sample used to read PDFs using IronPDF.
var pdfDocument = IronPdf.PdfDocument.FromFile("result.pdf");
string AllText = pdfDocument.ExtractAllText();
var pdfDocument = IronPdf.PdfDocument.FromFile("result.pdf");
string AllText = pdfDocument.ExtractAllText();
Dim pdfDocument = IronPdf.PdfDocument.FromFile("result.pdf")
Dim AllText As String = pdfDocument.ExtractAllText()
The Fromfile method is used to read a PDF from an existing file and transform it into PDF-document objects, as shown in the code above. We can read the text and images accessible on the PDF page using this object. It takes mere milliseconds to complete the process.
iText software also allows us to read documents without the help of an external reader.
using (PdfReader reader = new PdfReader("result.pdf"))
{
text = new StringBuilder();
PdfDocument pdf = new PdfDocument(reader);
var strategy = new LocationTextExtractionStrategy();
for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
{
var pdfpage = pdf.GetPage(i);
text.Append(PdfTextExtractor.GetTextFromPage(pdfpage, strategy));
}
}
using (PdfReader reader = new PdfReader("result.pdf"))
{
text = new StringBuilder();
PdfDocument pdf = new PdfDocument(reader);
var strategy = new LocationTextExtractionStrategy();
for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
{
var pdfpage = pdf.GetPage(i);
text.Append(PdfTextExtractor.GetTextFromPage(pdfpage, strategy));
}
}
Using reader As New PdfReader("result.pdf")
text = New StringBuilder()
Dim pdf As New PdfDocument(reader)
Dim strategy = New LocationTextExtractionStrategy()
Dim i As Integer = 1
Do While i <= pdf.GetNumberOfPages()
Dim pdfpage = pdf.GetPage(i)
text.Append(PdfTextExtractor.GetTextFromPage(pdfpage, strategy))
i += 1
Loop
End Using
The code above shows that we can read documents using the PDF-reader. The only thing we need to do is pass the file location as a parameter. The PDF-reader reads the document and converts it into an object using the object we can read the text from all pages for that we need to use them for a loop.
Result:
Both the IronPDF and iText PDF libraries allow users to split pages into separate documents. Both also provide a simple method for achieving this.
IronPDF allows us to split document pages into separate documents. Below is an example of how to split pages into separate documents.
var Splitdocument = IronPdf.PdfDocument.FromFile("result.pdf");
for (int i = 0; i < Splitdocument.PageCount; i++)
{
Splitdocument.CopyPage(i).SaveAs("Ironpdfmerged"+i.ToString()+".pdf");
}
var Splitdocument = IronPdf.PdfDocument.FromFile("result.pdf");
for (int i = 0; i < Splitdocument.PageCount; i++)
{
Splitdocument.CopyPage(i).SaveAs("Ironpdfmerged"+i.ToString()+".pdf");
}
Dim Splitdocument = IronPdf.PdfDocument.FromFile("result.pdf")
For i As Integer = 0 To Splitdocument.PageCount - 1
Splitdocument.CopyPage(i).SaveAs("Ironpdfmerged" & i.ToString() & ".pdf")
Next i
In the preceding example, we first load an existing document using the pdf-document class's fromfile method. The copypage method then allows us to copy pages from an existing document, and the Saveas method allows us to save the document into a separate file. The page numbers all begin with zero. Accordingly, we must supply a page number starting with a zero.
We can also split pages in a PDF document using iText 7. However, we need to write a separate class file that inherits PdfSplitter from the itext.kernel.Utils namespace first. This will create an instance to split the PDF data. Here is a code example:
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using iText.Html2pdf;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Parser;
using iText.Kernel.Pdf.Canvas.Parser.Listener;
using iText.Kernel.Utils;
using iText.StyledXmlParser.Css.Media;
namespace PdfConsoleApp
{
internal class Program
{
static void Main(string [] args)
{
using (var pdfDoc = new PdfDocument(new PdfReader("result.pdf")))
{
var outputDir = @"Itext_{0}.pdf";
var splitter = new CustomPdfSplitter(pdfDoc, outputDir);
var splittedDocs = splitter.SplitByPageCount(1);
foreach (var splittedDoc in splittedDocs)
{
splittedDoc.Close();
}
}
Console.ReadKey();
}
}
public class CustomPdfSplitter : PdfSplitter
{
private String dest;
private int partNumber = 1;
public CustomPdfSplitter(PdfDocument pdfDocument, String dest) : base(pdfDocument)
{
this.dest = dest;
}
protected override PdfWriter GetNextPdfWriter(PageRange documentPageRange)
{
return new PdfWriter(String.Format(dest, partNumber++));
}
}
}
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using iText.Html2pdf;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Parser;
using iText.Kernel.Pdf.Canvas.Parser.Listener;
using iText.Kernel.Utils;
using iText.StyledXmlParser.Css.Media;
namespace PdfConsoleApp
{
internal class Program
{
static void Main(string [] args)
{
using (var pdfDoc = new PdfDocument(new PdfReader("result.pdf")))
{
var outputDir = @"Itext_{0}.pdf";
var splitter = new CustomPdfSplitter(pdfDoc, outputDir);
var splittedDocs = splitter.SplitByPageCount(1);
foreach (var splittedDoc in splittedDocs)
{
splittedDoc.Close();
}
}
Console.ReadKey();
}
}
public class CustomPdfSplitter : PdfSplitter
{
private String dest;
private int partNumber = 1;
public CustomPdfSplitter(PdfDocument pdfDocument, String dest) : base(pdfDocument)
{
this.dest = dest;
}
protected override PdfWriter GetNextPdfWriter(PageRange documentPageRange)
{
return new PdfWriter(String.Format(dest, partNumber++));
}
}
}
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Net
Imports System.Text
Imports iText.Html2pdf
Imports iText.Kernel.Geom
Imports iText.Kernel.Pdf
Imports iText.Kernel.Pdf.Canvas.Parser
Imports iText.Kernel.Pdf.Canvas.Parser.Listener
Imports iText.Kernel.Utils
Imports iText.StyledXmlParser.Css.Media
Namespace PdfConsoleApp
Friend Class Program
Shared Sub Main(ByVal args() As String)
Using pdfDoc = New PdfDocument(New PdfReader("result.pdf"))
Dim outputDir = "Itext_{0}.pdf"
Dim splitter = New CustomPdfSplitter(pdfDoc, outputDir)
Dim splittedDocs = splitter.SplitByPageCount(1)
For Each splittedDoc In splittedDocs
splittedDoc.Close()
Next splittedDoc
End Using
Console.ReadKey()
End Sub
End Class
Public Class CustomPdfSplitter
Inherits PdfSplitter
Private dest As String
Private partNumber As Integer = 1
Public Sub New(ByVal pdfDocument As PdfDocument, ByVal dest As String)
MyBase.New(pdfDocument)
Me.dest = dest
End Sub
Protected Overrides Function GetNextPdfWriter(ByVal documentPageRange As PageRange) As PdfWriter
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: return new PdfWriter(String.Format(dest, partNumber++));
Dim tempVar = New PdfWriter(String.Format(dest, partNumber))
partNumber += 1
Return tempVar
End Function
End Class
End Namespace
We have created a custom class that overrides the pdfwriter and allows us to rename the split file using the custom name using the original filename. It will return the PDF pages into a new list item later, which can then be used to save into a separate document.
Result:
IronPDF does not render HTML to PDF from a remote server. Instead, it spins an instance up of a real standard compliant web browser behind the scenes (without any additional software needing to be installed). The HTML is rendered with complete accuracy — and in a vector format suitable for the highest standards of commercial printing. The output is a clean and high-quality PDF. Therefore, IronPDF has an advantage over iText PDF. IronPDF is highly recommended for pragmatic coders seeking to work effectively and efficiently. It is openly commercial, with licensing and pricing details all published on the website. You can visit here to learn about our license options for IronPDF.
iText PDF supports multiple fonts and we are only able to use an inline style sheet. It does not allow us to add external style sheets because it does not have an engine or browser to convert the HTML into PDF files. iText renders the HTML correctly and accurately but creates a PDF file of a very large size. iText is complex, and not suitable for beginners. iText comes with a commercial license and a free developer license. To learn more about the iText license click here.
IronPDF is a must-have for pragmatic coders who want to work quickly and productively. Most significantly, it saves you time. IronPDF licensing packages provide a lifetime license, and there are no ongoing costs.
You can download IronPDF from this link.
9 .NET API products for your office documents