iText for .NET
| Language | C# (.NET Standard 2.0, .NET Framework 4.6.1+) |
|---|---|
| License | AGPL-3.0 or commercial license from Apryse |
| Repository | github.com/itext/itext-dotnet |
| Documentation | iText Knowledge Base |
| Depends on | itext.bouncy-castle-adapter (or the FIPS adapter) for encryption and signatures |
| Read / write | yes / 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#
dotnet add package itext
# needed for encryption and digital signatures
dotnet add package itext.bouncy-castle-adapter
Basic usage: create a PDF#
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#
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#
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#
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#
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#
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#
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#
- Opening a document with both a
PdfReaderand aPdfWriter(“stamping mode”) modifies an existing file; addnew StampingProperties().UseAppendMode()to save as an incremental update, which keeps signatures valid. - For PDF/A, create a
PdfADocumentwith an output intent; for tagged PDF callpdf.SetTagged(). - The pdfHTML add-on (
itext.html2pdf, also AGPL) converts HTML and CSS to PDF.