All formats

iText for .NET

LanguageC# (.NET Standard 2.0, .NET Framework 4.6.1+)
LicenseAGPL-3.0 or commercial license from Apryse
Repositorygithub.com/itext/itext-dotnet
DocumentationiText Knowledge Base
Depends onitext.bouncy-castle-adapter (or the FIPS adapter) for encryption and signatures
Read / writeyes / yes: create with layout, extract text, merge, split, forms, annotations, encryption, digital signatures, PDF/A, PDF/UA

iText (now owned by Apryse) is the most complete open-source PDF library for .NET and Java; the two versions have the same API with .NET naming. Its layout module builds documents from paragraphs, tables, lists and images with automatic page breaks; the kernel module reads and writes PDF objects; add-ons cover forms, digital signatures (PAdES, LTV), PDF/A and PDF/UA conformance, and HTML-to-PDF (pdfHTML). It doesn’t render pages to images in the open-source edition.

The license matters: iText is AGPL-3.0. Any program that uses it, including a web service, must publish its source code under the AGPL, or you buy a commercial license. iText 5 and older .NET ports (iTextSharp 4.x under LGPL) are no longer maintained.

Installation#

Shell
dotnet add package itext
# needed for encryption and digital signatures
dotnet add package itext.bouncy-castle-adapter

Basic usage: create a PDF#

C#
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

using var writer = new PdfWriter("hello.pdf");
using var pdf = new PdfDocument(writer);
pdf.GetDocumentInfo().SetTitle("Hello");
using var document = new Document(pdf);

document.Add(new Paragraph("Hello, PDF!").SetFontSize(24));
document.Add(new Paragraph("Created with iText.").SetTextAlignment(TextAlignment.CENTER));

var table = new Table(UnitValue.CreatePercentArray(new float[] { 3, 1 })).UseAllAvailableWidth();
table.AddHeaderCell("Item");
table.AddHeaderCell("Price");
table.AddCell("Widget");
table.AddCell("9.99");
document.Add(table);

Disposing the Document closes the PDF and writes the file.

How to extract text from a PDF#

C#
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Parser;
using iText.Kernel.Pdf.Canvas.Parser.Listener;

using var pdf = new PdfDocument(new PdfReader("in.pdf"));
for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
{
    string text = PdfTextExtractor.GetTextFromPage(pdf.GetPage(i), new LocationTextExtractionStrategy());
    Console.WriteLine($"--- page {i} ---\n{text}");
}

LocationTextExtractionStrategy orders text by position; SimpleTextExtractionStrategy keeps content-stream order.

How to merge PDF files#

C#
using iText.Kernel.Pdf;
using iText.Kernel.Utils;

using var output = new PdfDocument(new PdfWriter("merged.pdf"));
var merger = new PdfMerger(output);
foreach (string path in new[] { "a.pdf", "b.pdf" })
{
    using var source = new PdfDocument(new PdfReader(path));
    merger.Merge(source, 1, source.GetNumberOfPages());
}

How to split a PDF and rotate pages#

C#
using var source = new PdfDocument(new PdfReader("in.pdf"));
for (int i = 1; i <= source.GetNumberOfPages(); i++)
{
    using var single = new PdfDocument(new PdfWriter($"page-{i}.pdf"));
    source.CopyPagesTo(i, i, single);
    PdfPage page = single.GetPage(1);
    page.SetRotation((page.GetRotation() + 90) % 360);
}

How to fill a PDF form#

C#
using iText.Forms;
using iText.Kernel.Pdf;

using var pdf = new PdfDocument(new PdfReader("form.pdf"), new PdfWriter("filled.pdf"));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, false);
foreach (var name in form.GetAllFormFields().Keys)
    Console.WriteLine(name);
form.GetField("name").SetValue("Jane Doe");
form.FlattenFields();  // optional: make the values part of the page content

How to encrypt and decrypt a PDF#

C#
using System.Text;
using iText.Kernel.Pdf;

var props = new WriterProperties().SetStandardEncryption(
    Encoding.UTF8.GetBytes("user"),
    Encoding.UTF8.GetBytes("owner"),
    EncryptionConstants.ALLOW_PRINTING,
    EncryptionConstants.ENCRYPTION_AES_256);
using (var pdf = new PdfDocument(new PdfReader("in.pdf"), new PdfWriter("encrypted.pdf", props))) { }

// decrypt: open with the owner password, write without encryption
var readerProps = new ReaderProperties().SetPassword(Encoding.UTF8.GetBytes("owner"));
using (var pdf = new PdfDocument(new PdfReader("encrypted.pdf", readerProps), new PdfWriter("decrypted.pdf"))) { }

How to read PDF metadata#

C#
using var pdf = new PdfDocument(new PdfReader("in.pdf"));
PdfDocumentInfo info = pdf.GetDocumentInfo();
Console.WriteLine($"Title: {info.GetTitle()}");
Console.WriteLine($"Author: {info.GetAuthor()}");
Console.WriteLine($"Producer: {info.GetProducer()}");
Console.WriteLine($"Version: {pdf.GetPdfVersion()}, pages: {pdf.GetNumberOfPages()}");

Tips#