iTextSharp for C# (.NET): Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to iTextSharp: What It Is and Why You Need It

\n\n

iTextSharp is a powerful library for generating and processing PDF documents on the .NET platform (C#, VB.NET, F#). It is a port of the popular Java library iText and provides developers with full control over creating, modifying, and reading PDF files.

\n\n

Key features of iTextSharp:

\n
    \n
  • Creating PDF documents from scratch
  • \n
  • Adding text, images, tables, lists
  • \n
  • Working with fonts (including Cyrillic and Unicode)
  • \n
  • Inserting hyperlinks, bookmarks, metadata
  • \n
  • Encryption and password protection
  • \n
  • Merging and splitting PDF files
  • \n
  • Adding watermarks and headers/footers
  • \n
  • Working with forms (AcroForms)
  • \n
\n\n

The library is widely used in enterprise applications for generating reports, invoices, waybills, certificates, and other documents. iTextSharp supports .NET Framework 2.0+ and .NET Core (via iTextSharp.LGPLv2.Core).

\n\n

Installing iTextSharp

\n\n

To install iTextSharp in a .NET project, use the NuGet Package Manager. There are several versions of the library:

\n\n
    \n
  • iTextSharp (old version 5.x, LGPL) — free but with limitations
  • \n
  • iTextSharp.LGPLv2.Core — fork for .NET Core / .NET 5+
  • \n
  • iText7 (iText.LGPLv2.Core) — new version of iText 7 for .NET
  • \n
\n\n

Installation via Package Manager Console:

\n
Install-Package iTextSharp.LGPLv2.Core -Version 1.7.0
\n\n

Installation via .NET CLI:

\n
dotnet add package iTextSharp.LGPLv2.Core --version 1.7.0
\n\n

Installation via Visual Studio NuGet UI:

\n
    \n
  1. Right-click on the project → "Manage NuGet Packages"
  2. \n
  3. Search: "iTextSharp.LGPLv2.Core"
  4. \n
  5. Select a version and click "Install"
  6. \n
\n\n

Important: Working with Cyrillic and other non-standard fonts will require additional configuration (see the "Tips" section).

\n\n

Quick Start: Creating Your First PDF Document

\n\n

Let's look at a minimal example of creating a PDF file with the text "Hello, World!":

\n\n
using System.IO;\nusing iTextSharp.text;\nusing iTextSharp.text.pdf;\n\nclass Program\n{\n    static void Main()\n    {\n        // Create a document\n        Document doc = new Document();\n        \n        // Create a stream for writing to a file\n        using (FileStream fs = new FileStream("HelloWorld.pdf", FileMode.Create))\n        {\n            // Get a PdfWriter instance\n            PdfWriter writer = PdfWriter.GetInstance(doc, fs);\n            \n            // Open the document\n            doc.Open();\n            \n            // Add a paragraph with text\n            doc.Add(new Paragraph("Hello, World!"));\n            \n            // Close the document\n            doc.Close();\n        }\n        \n        Console.WriteLine("PDF created successfully!");\n    }\n}
\n\n

This code will create a file HelloWorld.pdf in the executable's folder. Note the sequence: create Document → open → add content → close.

\n\n

Main Methods and Functions of iTextSharp

\n\n

Let's look at 10 key methods/classes of the library with detailed descriptions and examples.

\n\n

1. Document (class)

\n

What it does: Represents the PDF document itself. Manages pages, margins, and orientation.

\n

Constructor parameters:

\n
    \n
  • Rectangle pageSize — page size (e.g., PageSize.A4)
  • \n
  • float marginLeft, marginRight, marginTop, marginBottom — margins
  • \n
\n

Example:

\n
// Creating an A4 document with landscape orientation and custom margins\nDocument doc = new Document(PageSize.                    

Recommendations