Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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 the C# language.
Discover how to edit PDF files using C# with IronPDF and iTextSharp, making the task straightforward by leveraging these powerful libraries.
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 features and trial information are available to help developers efficiently work 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 subscription options for more extensive usage.
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 the 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:
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using System.IO;
// Create a new PDF document
using (var writer = new PdfWriter(new FileStream("newfile.pdf", FileMode.Create)))
{
using (var pdf = new PdfDocument(writer))
{
var document = new Document(pdf);
// Create a header paragraph
Paragraph header = new Paragraph("HEADER")
.SetTextAlignment(TextAlignment.CENTER)
.SetFontSize(16);
// Add the header to the document
document.Add(header);
// Loop through pages and align header text
for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
{
Rectangle pageSize = pdf.GetPage(i).GetPageSize();
float x = pageSize.GetWidth() / 2;
float y = pageSize.GetTop() - 20;
// Add the header text to each page
document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
}
// Set the margins
document.SetTopMargin(50);
document.SetBottomMargin(50);
}
}
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using System.IO;
// Create a new PDF document
using (var writer = new PdfWriter(new FileStream("newfile.pdf", FileMode.Create)))
{
using (var pdf = new PdfDocument(writer))
{
var document = new Document(pdf);
// Create a header paragraph
Paragraph header = new Paragraph("HEADER")
.SetTextAlignment(TextAlignment.CENTER)
.SetFontSize(16);
// Add the header to the document
document.Add(header);
// Loop through pages and align header text
for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
{
Rectangle pageSize = pdf.GetPage(i).GetPageSize();
float x = pageSize.GetWidth() / 2;
float y = pageSize.GetTop() - 20;
// Add the header text to each page
document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
}
// Set the margins
document.SetTopMargin(50);
document.SetBottomMargin(50);
}
}
Imports iText.Kernel.Pdf
Imports iText.Layout
Imports iText.Layout.Element
Imports iText.Layout.Properties
Imports System.IO
' Create a new PDF document
Using writer = New PdfWriter(New FileStream("newfile.pdf", FileMode.Create))
Using pdf = New PdfDocument(writer)
Dim document As New Document(pdf)
' Create a header paragraph
Dim header As Paragraph = (New Paragraph("HEADER")).SetTextAlignment(TextAlignment.CENTER).SetFontSize(16)
' Add the header to the document
document.Add(header)
' Loop through pages and align header text
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
' Add the header text to each page
document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0)
i += 1
Loop
' Set the margins
document.SetTopMargin(50)
document.SetBottomMargin(50)
End Using
End Using
In the code above, we created a new PDF file called "newfile.pdf" and added a paragraph header to it.
To edit an existing PDF file using iTextSharp, you need a PdfReader
object to read the existing PDF document and a PdfStamper
object to modify it. Here's an example:
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Html2pdf;
using System.IO;
/**
* 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");
// Convert HTML to PDF
Document document = HtmlConverter.ConvertToDocument(
new FileStream("Test_iText7_1.pdf", FileMode.Open), pdf, properties);
// Create and add a header paragraph
Paragraph header = new Paragraph("HEADER")
.SetTextAlignment(TextAlignment.CENTER)
.SetFontSize(16);
document.Add(header);
// Align header text for each page
for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
{
Rectangle pageSize = pdf.GetPage(i).GetPageSize();
float x = pageSize.GetWidth() / 2;
float y = pageSize.GetTop() - 20;
// Add header text aligned at the top
document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
}
// Set the top and bottom margins
document.SetTopMargin(50);
document.SetBottomMargin(50);
document.Close();
}
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Html2pdf;
using System.IO;
/**
* 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");
// Convert HTML to PDF
Document document = HtmlConverter.ConvertToDocument(
new FileStream("Test_iText7_1.pdf", FileMode.Open), pdf, properties);
// Create and add a header paragraph
Paragraph header = new Paragraph("HEADER")
.SetTextAlignment(TextAlignment.CENTER)
.SetFontSize(16);
document.Add(header);
// Align header text for each page
for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
{
Rectangle pageSize = pdf.GetPage(i).GetPageSize();
float x = pageSize.GetWidth() / 2;
float y = pageSize.GetTop() - 20;
// Add header text aligned at the top
document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
}
// Set the top and bottom margins
document.SetTopMargin(50);
document.SetBottomMargin(50);
document.Close();
}
Imports iText.Kernel.Pdf
Imports iText.Layout
Imports iText.Layout.Element
Imports iText.Layout.Properties
Imports iText.Html2pdf
Imports System.IO
'''
''' * 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")
' Convert HTML to PDF
Dim document As Document = HtmlConverter.ConvertToDocument(New FileStream("Test_iText7_1.pdf", FileMode.Open), pdf, properties)
' Create and add a header paragraph
Dim header As Paragraph = (New Paragraph("HEADER")).SetTextAlignment(TextAlignment.CENTER).SetFontSize(16)
document.Add(header)
' Align header text for each page
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
' Add header text aligned at the top
document.ShowTextAligned(header, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0)
i += 1
Loop
' Set the top and bottom margins
document.SetTopMargin(50)
document.SetBottomMargin(50)
document.Close()
End Using
End Sub
In this code, an existing PDF opens, and we add headers to its pages with proper text alignment.
IronPDF is a powerful PDF library for C# that facilitates editing PDF documents. This tutorial will walk through the steps to edit an existing PDF file using IronPDF, including creating new PDF documents, adding pages, merging PDFs, and more.
Ensure you have:
Create a new C# project in Visual Studio. Choose the "Console Application" project type.
Use the NuGet Package Manager to install the IronPDF library into your project.
// Execute this command in the Package Manager Console
Install-Package IronPdf
// Execute this command in the Package Manager Console
Install-Package IronPdf
Load an existing PDF document using the PdfDocument
class:
using IronPdf;
// Path to an existing PDF file
var existingPdf = @"C:\path\to\existing\pdf\document.pdf";
// Load the PDF document
var pdfDoc = PdfDocument.FromFile(existingPdf);
using IronPdf;
// Path to an existing PDF file
var existingPdf = @"C:\path\to\existing\pdf\document.pdf";
// Load the PDF document
var pdfDoc = PdfDocument.FromFile(existingPdf);
Imports IronPdf
' Path to an existing PDF file
Private existingPdf = "C:\path\to\existing\pdf\document.pdf"
' Load the PDF document
Private pdfDoc = PdfDocument.FromFile(existingPdf)
To add a new page:
// Add a new page with default size
var newPage = pdfDoc.AddPage();
newPage.Size = PageSize.Letter;
// Add a new page with default size
var newPage = pdfDoc.AddPage();
newPage.Size = PageSize.Letter;
' Add a new page with default size
Dim newPage = pdfDoc.AddPage()
newPage.Size = PageSize.Letter
Generate a PDF directly from a webpage URL. Here's an example:
using IronPdf;
/**
* IronPDF URL to PDF
* anchor-ironpdf-website-to-pdf
**/
private void ExistingWebURL()
{
// Create PDF from a webpage
var Renderer = new IronPdf.ChromePdfRenderer();
// Set rendering options
Renderer.RenderingOptions.MarginTop = 50; // millimeters
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.EnableJavaScript = true;
Renderer.RenderingOptions.RenderDelay = 500; // milliseconds
// Render URL as PDF
using var PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format");
PDF.SaveAs("wikipedia.pdf");
}
using IronPdf;
/**
* IronPDF URL to PDF
* anchor-ironpdf-website-to-pdf
**/
private void ExistingWebURL()
{
// Create PDF from a webpage
var Renderer = new IronPdf.ChromePdfRenderer();
// Set rendering options
Renderer.RenderingOptions.MarginTop = 50; // millimeters
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.EnableJavaScript = true;
Renderer.RenderingOptions.RenderDelay = 500; // milliseconds
// Render URL as PDF
using var PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format");
PDF.SaveAs("wikipedia.pdf");
}
Imports IronPdf
'''
''' * IronPDF URL to PDF
''' * anchor-ironpdf-website-to-pdf
''' *
Private Sub ExistingWebURL()
' Create PDF from a webpage
Dim Renderer = New IronPdf.ChromePdfRenderer()
' Set rendering options
Renderer.RenderingOptions.MarginTop = 50 ' millimeters
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.EnableJavaScript = True
Renderer.RenderingOptions.RenderDelay = 500 ' milliseconds
' Render URL as PDF
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 for creating, manipulating, and extracting data from PDF documents in C#. It is well-documented and widely used. IronPDF, on the other hand, is more modern, with additional features and benefits that make it a better choice for developers.
Here's how you can use IronPDF to create a PDF from HTML:
using IronPdf;
/**
* IronPDF HTML to PDF
* anchor-ironpdf-document-from-html
**/
private void HTMLString()
{
// Render HTML to PDF
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 = "ChromeHtmlToPdf.pdf";
PDF.SaveAs(OutputPath);
}
using IronPdf;
/**
* IronPDF HTML to PDF
* anchor-ironpdf-document-from-html
**/
private void HTMLString()
{
// Render HTML to PDF
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 = "ChromeHtmlToPdf.pdf";
PDF.SaveAs(OutputPath);
}
Imports IronPdf
'''
''' * IronPDF HTML to PDF
''' * anchor-ironpdf-document-from-html
''' *
Private Sub HTMLString()
' Render HTML to PDF
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 = "ChromeHtmlToPdf.pdf"
PDF.SaveAs(OutputPath)
End Sub
Convert HTML text to a PDF using iText 7:
using iText.Html2pdf;
using System.IO;
/**
* iText HTML to PDF
* anchor-itext-html-to-pdf
**/
private void HTMLString()
{
HtmlConverter.ConvertToPdf("<h1>Hello iText7</h1>", new FileStream("iText7HtmlToPdf.pdf", FileMode.Create));
}
using iText.Html2pdf;
using System.IO;
/**
* iText HTML to PDF
* anchor-itext-html-to-pdf
**/
private void HTMLString()
{
HtmlConverter.ConvertToPdf("<h1>Hello iText7</h1>", new FileStream("iText7HtmlToPdf.pdf", FileMode.Create));
}
Imports iText.Html2pdf
Imports System.IO
'''
''' * iText HTML to PDF
''' * anchor-itext-html-to-pdf
''' *
Private Sub HTMLString()
HtmlConverter.ConvertToPdf("<h1>Hello iText7</h1>", New FileStream("iText7HtmlToPdf.pdf", FileMode.Create))
End Sub
IronPDF is designed to be faster and more efficient than iTextSharp, allowing quicker generation of PDFs using fewer resources. This efficiency is crucial for large or complex documents.
iTextSharp requires a commercial license for certain use cases, which can be expensive. IronPDF, however, offers a more affordable pricing model with various options tailored to different needs and budgets.
One of the key differences between iTextSharp and IronPDF is their licensing and pricing models.
In conclusion, while both iTextSharp and IronPDF can handle PDF manipulation in C#, IronPDF stands out as a more versatile and efficient choice. It offers advanced features, an intuitive API, and better performance. Its flexible pricing makes it suitable for commercial projects and larger organizations.
With IronPDF's superior HTML to PDF conversion, developers can easily generate reports or documents with rich media or interactive content. Coupled with cost-effective pricing, IronPDF is an excellent choice for developers needing a powerful and efficient PDF library for C# projects.
The article compares iTextSharp and IronPDF for editing PDF files in C#, focusing on output quality, performance, and pricing, highlighting how IronPDF provides superior output.
To use iTextSharp, you need Visual Studio, basic knowledge of C#, and the iTextSharp library installed in your project.
You can install iTextSharp using the NuGet Package Manager in Visual Studio by searching for 'iTextSharp' and installing the package.
IronPDF offers a more modern API, better performance, and flexible pricing, making it a preferred choice for many developers.
Yes, IronPDF can generate PDFs directly from a webpage URL using its ChromePdfRenderer.
iTextSharp is AGPL licensed, requiring a commercial license for non-open-source use, while IronPDF offers flexible licensing options, including a free trial.
In IronPDF, you can add a new page by using the AddPage() method on a PdfDocument object.
You need to have Visual Studio IDE and the IronPDF library installed, which can be done via the NuGet Package Manager.
A developer might choose IronPDF for its superior performance, modern features, intuitive API, and more flexible pricing options.
IronPDF provides advanced HTML to PDF conversion capabilities, allowing easy generation of documents with rich media and interactive content.