Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
PDF (Portable Document Format) is a widely-used document format that is popular due to its ability to preserve document formatting, security, and portability.
PDF files have become one of the most widely used document formats in the world, and there are several libraries available for creating and manipulating PDFs in C# language.
Editing PDF files can also be challenging, but with the help of libraries like iTextSharp DLL and IronPDF, you can easily read, write and edit PDF files using C#.
In this article, we will compare two popular libraries for PDF manipulation in C#: iTextSharp and IronPDF. We will discuss how to edit PDF files using both libraries, and then we will explore how IronPDF is a superior option compared to iTextSharp, especially in terms of output print, performance, and pricing.
iTextSharp and IronPDF are two popular libraries for working with PDF files in C#. Both libraries provide a wide range of features and functionalities to create, edit, and manipulate PDF documents.
iTextSharp DLL is a C# port of the Java-based iText library. It provides a simple and easy-to-use API for creating and manipulating PDF documents. iTextSharp is an open-source library that is available under the AGPL license.
IronPDF is a .NET library that is designed to create, edit, and manipulate PDF files using C#. It provides a modern and intuitive API for working with PDF documents. IronPDF is a commercial library that comes with a free trial version and a paid version.
Both iTextSharp and IronPDF libraries provide a wide range of features and functionalities to create, edit, and manipulate PDF documents. However, IronPDF has several advantages over iTextSharp, which make it a preferred choice for working with PDF documents in C#.
Now that we have discussed the differences between iTextSharp and IronPDF, let's take a look at how to edit PDF files using both libraries. We will go through examples of adding text, form fields, and filling out forms in an existing PDF document using iTextSharp and IronPDF.
Before we start, you will need the following:
To install iTextSharp library in your project, you can use the NuGet package manager. Open your Visual Studio project and right-click on the project name in the Solution Explorer. Select "Manage NuGet Packages" from the context menu. In the NuGet Package Manager, search for "iTextSharp" and install the latest version of the package.
To create a new PDF file using iTextSharp, we need to create a new instance of the "Document" class and pass a new FileStream
object to its constructor. Here's an example:
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);
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)
In the above code, we created a new PDF file called "newfile.pdf" and added a new paragraph to it.
To edit an existing PDF file using iTextSharp, we need to create a PdfReader
object to read the existing PDF document and a PdfStamper
object to modify it. Here's an example:
/**
iText URL to PDF
anchor-itext-url-to-pdf
**/
private void ExistingWebURL()
{
//Initialize PDF writer
PdfWriter writer = new PdfWriter("wikipedia.pdf");
//Initialize PDF document
using 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();
}
/**
iText URL to PDF
anchor-itext-url-to-pdf
**/
private void ExistingWebURL()
{
//Initialize PDF writer
PdfWriter writer = new PdfWriter("wikipedia.pdf");
//Initialize PDF document
using 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();
}
'''
'''iText URL to PDF
'''anchor-itext-url-to-pdf
'''*
Private Sub ExistingWebURL()
'Initialize PDF writer
Dim writer As New PdfWriter("wikipedia.pdf")
'Initialize PDF document
Using 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 Using
End Sub
In the above code, we opened an existing PDF file called "existingfile.pdf" and created a new PDF file called "editedfile.pdf". We filled the form fields with data and added a new page to the PDF file.
The PdfStamper
class provides many more options to edit existing PDF files, such as adding images, creating annotations, and changing the page size.
IronPDF is a powerful PDF library for C# that enables editing of PDF documents. In this tutorial, we will walk through the steps required to edit an existing PDF file using IronPDF. We will cover various aspects, including creating new PDF documents, adding pages to an existing PDF, adding content to a PDF, and filling form fields in a PDF. Below is an example code in C# to illustrate the various steps.
Before we begin, make sure you have the following installed:
Create a new C# project in Visual Studio. Choose the "Console Application" project type.
Install the IronPDF library by installing it to your project via NuGet package manager.
Install-Package IronPdf
To load an existing PDF document, you can use the PdfDocument
class. Below is the code example to load an existing PDF document:
using IronPdf;
var existingPdf = @"C:\path\to\existing\pdf\document.pdf";
var pdfDoc = PdfDocument.FromFile(existingPdf);
using IronPdf;
var existingPdf = @"C:\path\to\existing\pdf\document.pdf";
var pdfDoc = PdfDocument.FromFile(existingPdf);
Imports IronPdf
Private existingPdf = "C:\path\to\existing\pdf\document.pdf"
Private pdfDoc = PdfDocument.FromFile(existingPdf)
To add a new page to an existing document, you can use the AddPage
method of the PdfDocument
class. Below is an example to add a new page to existing PDFs:
var newPage = pdfDoc.AddPage();
newPage.Size = PageSize.Letter;
var newPage = pdfDoc.AddPage();
newPage.Size = PageSize.Letter;
Dim newPage = pdfDoc.AddPage()
newPage.Size = PageSize.Letter
By using IronPDF, the following code can generate a PDF file directly from a webpage address. Additionally, custom headers and footers are provided.
/**
IronPDF URL to PDF
anchor-ironpdf-website-to-pdf
**/
private void ExistingWebURL()
{
// Create a PDF from any existing web page
var Renderer = new IronPdf.ChromePdfRenderer();
// Create a PDF from an existing HTML
Renderer.RenderingOptions.MarginTop = 50; //millimetres
Renderer.RenderingOptions.MarginBottom = 50;
Renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
Renderer.RenderingOptions.TextHeader = new TextHeaderFooter()
{
CenterText = "{pdf-title}",
DrawDividerLine = true,
FontSize = 16
};
Renderer.RenderingOptions.TextFooter = new TextHeaderFooter()
{
LeftText = "{date} {time}",
RightText = "Page {page} of {total-pages}",
DrawDividerLine = true,
FontSize = 14
};
Renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
Renderer.RenderingOptions.EnableJavaScript = true;
Renderer.RenderingOptions.RenderDelay = 500; //milliseconds
using var PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format");
PDF.SaveAs("wikipedia.pdf");
}
/**
IronPDF URL to PDF
anchor-ironpdf-website-to-pdf
**/
private void ExistingWebURL()
{
// Create a PDF from any existing web page
var Renderer = new IronPdf.ChromePdfRenderer();
// Create a PDF from an existing HTML
Renderer.RenderingOptions.MarginTop = 50; //millimetres
Renderer.RenderingOptions.MarginBottom = 50;
Renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
Renderer.RenderingOptions.TextHeader = new TextHeaderFooter()
{
CenterText = "{pdf-title}",
DrawDividerLine = true,
FontSize = 16
};
Renderer.RenderingOptions.TextFooter = new TextHeaderFooter()
{
LeftText = "{date} {time}",
RightText = "Page {page} of {total-pages}",
DrawDividerLine = true,
FontSize = 14
};
Renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
Renderer.RenderingOptions.EnableJavaScript = true;
Renderer.RenderingOptions.RenderDelay = 500; //milliseconds
using var PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format");
PDF.SaveAs("wikipedia.pdf");
}
'''
'''IronPDF URL to PDF
'''anchor-ironpdf-website-to-pdf
'''*
Private Sub ExistingWebURL()
' Create a PDF from any existing web page
Dim Renderer = New IronPdf.ChromePdfRenderer()
' Create a PDF from an existing HTML
Renderer.RenderingOptions.MarginTop = 50 'millimetres
Renderer.RenderingOptions.MarginBottom = 50
Renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print
Renderer.RenderingOptions.TextHeader = New TextHeaderFooter() With {
.CenterText = "{pdf-title}",
.DrawDividerLine = True,
.FontSize = 16
}
Renderer.RenderingOptions.TextFooter = New TextHeaderFooter() With {
.LeftText = "{date} {time}",
.RightText = "Page {page} of {total-pages}",
.DrawDividerLine = True,
.FontSize = 14
}
Renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print
Renderer.RenderingOptions.EnableJavaScript = True
Renderer.RenderingOptions.RenderDelay = 500 'milliseconds
Dim PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format")
PDF.SaveAs("wikipedia.pdf")
End Sub
iTextSharp is a popular open-source library that enables developers to create, manipulate, and extract data from PDF documents in C#. It is a well-documented and widely used library that has been around for a long time. However, IronPDF is a newer and more modern library that offers additional features and benefits, making it a better choice for developers.
One of the areas where IronPDF surpasses iTextSharp is in its output capabilities. IronPDF provides superior support for HTML to PDF conversion, enabling developers to effortlessly convert HTML pages to PDF documents. This is especially beneficial for generating reports or documents that contain rich media or interactive content. Furthermore, IronPDF allows for finer control over page layout and formatting, making it simpler to create PDF documents that precisely match the desired appearance.
The code that follows uses IronPDF to create a PDF with HTML input.
/**
IronPDF HTML to PDF
anchor-ironpdf-document-from-html
**/
private void HTMLString()
{
// Render any HTML fragment or document to HTML
var Renderer = new IronPdf.ChromePdfRenderer();
using var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>");
Renderer.RenderingOptions.TextFooter = new HtmlHeaderFooter()
{
HtmlFragment = "<div style='text-align:right'><em style='color:pink'>page {page} of {total-pages}</em></div>"
};
var OutputPath = "ChromePdfRenderer.pdf";
PDF.SaveAs(OutputPath);
Renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Screen;
}
/**
IronPDF HTML to PDF
anchor-ironpdf-document-from-html
**/
private void HTMLString()
{
// Render any HTML fragment or document to HTML
var Renderer = new IronPdf.ChromePdfRenderer();
using var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>");
Renderer.RenderingOptions.TextFooter = new HtmlHeaderFooter()
{
HtmlFragment = "<div style='text-align:right'><em style='color:pink'>page {page} of {total-pages}</em></div>"
};
var OutputPath = "ChromePdfRenderer.pdf";
PDF.SaveAs(OutputPath);
Renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Screen;
}
'''
'''IronPDF HTML to PDF
'''anchor-ironpdf-document-from-html
'''*
Private Sub HTMLString()
' Render any HTML fragment or document to HTML
Dim Renderer = New IronPdf.ChromePdfRenderer()
Dim PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>")
Renderer.RenderingOptions.TextFooter = New HtmlHeaderFooter() With {.HtmlFragment = "<div style='text-align:right'><em style='color:pink'>page {page} of {total-pages}</em></div>"}
Dim OutputPath = "ChromePdfRenderer.pdf"
PDF.SaveAs(OutputPath)
Renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Screen
End Sub
The following code generates a PDF using HTML text using iText7.
/**
iText HTML to PDF
anchor-itext-html-to-pdf
**/
private void HTMLString()
{
HtmlConverter.ConvertToPdf("< h1 > Hello iText7 </ h1 >", new FileStream("ChromePdfRenderer.pdf", FileMode.Create));
}
/**
iText HTML to PDF
anchor-itext-html-to-pdf
**/
private void HTMLString()
{
HtmlConverter.ConvertToPdf("< h1 > Hello iText7 </ h1 >", new FileStream("ChromePdfRenderer.pdf", FileMode.Create));
}
'''
'''iText HTML to PDF
'''anchor-itext-html-to-pdf
'''*
Private Sub HTMLString()
HtmlConverter.ConvertToPdf("< h1 > Hello iText7 </ h1 >", New FileStream("ChromePdfRenderer.pdf", FileMode.Create))
End Sub
In terms of performance, IronPDF is also a clear winner. It is designed to be faster and more efficient than iTextSharp, allowing developers to generate PDFs more quickly and with fewer system resources. This is particularly important when dealing with large or complex PDF documents, where performance can become a significant factor.
Another area where IronPDF has an advantage over iTextSharp is pricing. While iTextSharp is free to use for most purposes, it does require a commercial license for certain use cases, and the commercial licenses can be quite expensive. On the other hand, IronPDF offers a more affordable pricing model, with a range of options available to suit different needs and budgets.
One of the key differences between iTextSharp and IronPDF is their licensing and pricing.
iTextSharp is licensed under the AGPL license, which allows free usage for open-source projects but requires a commercial license for commercial use. The commercial license for iTextSharp is available in different tiers, with pricing based on the features and support level required, as well as the number of developers and deployment servers.
On the other hand, IronPDF is a commercial library that offers a free trial for developers to evaluate before purchasing. IronPDF offers flexible licensing options, including per-developer and per-server licenses, as well as enterprise licenses for larger organizations. Pricing for IronPDF is based on the number of developers and features required, with discounts available for multi-year and volume licenses.
While iTextSharp may be more cost-effective for small open-source projects, IronPDF's flexible licensing options and commercial support make it a more viable choice for commercial projects and larger organizations.
In conclusion, while both iTextSharp and IronPDF are capable libraries for PDF manipulation in C#, IronPDF stands out as a more versatile and efficient choice. With its advanced features, intuitive API, and user-friendly documentation, IronPDF offers a superior experience for developers. Its flexible licensing options and cost-effective pricing make it a better fit for commercial projects and larger organizations.
Additionally, IronPDF boasts better technical performance with faster rendering times, efficient memory usage, and optimized handling of large PDF documents. With Iron Software's special offer of buying all five products for the price of two licenses, developers can gain access to a comprehensive suite of powerful tools and libraries to streamline their application development process. If you are in need of a powerful and efficient PDF library for your C# projects, IronPDF is a good choice.
9 .NET API products for your office documents