Introduction to iTextSharp: What It Is and Why You Need It
\n\niTextSharp 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\nKey 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
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\nInstalling iTextSharp
\n\nTo 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
Installation via Package Manager Console:
\nInstall-Package iTextSharp.LGPLv2.Core -Version 1.7.0\n\nInstallation via .NET CLI:
\ndotnet add package iTextSharp.LGPLv2.Core --version 1.7.0\n\nInstallation via Visual Studio NuGet UI:
\n- \n
- Right-click on the project → "Manage NuGet Packages" \n
- Search: "iTextSharp.LGPLv2.Core" \n
- Select a version and click "Install" \n
Important: Working with Cyrillic and other non-standard fonts will require additional configuration (see the "Tips" section).
\n\nQuick Start: Creating Your First PDF Document
\n\nLet's look at a minimal example of creating a PDF file with the text "Hello, World!":
\n\nusing 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\nThis code will create a file HelloWorld.pdf in the executable's folder. Note the sequence: create Document → open → add content → close.
Main Methods and Functions of iTextSharp
\n\nLet's look at 10 key methods/classes of the library with detailed descriptions and examples.
\n\n1. Document (class)
\nWhat it does: Represents the PDF document itself. Manages pages, margins, and orientation.
\nConstructor parameters:
\n- \n
Rectangle pageSize— page size (e.g., PageSize.A4) \n float marginLeft, marginRight, marginTop, marginBottom— margins \n
Example:
\n// Creating an A4 document with landscape orientation and custom margins\nDocument doc = new Document(PageSize. Also in library
Testing in Go (Golang): Frameworks and Tools — Complete Guide
wiremock-rs for Rust: Complete Guide with Examples
QR Code Generator in Go (Golang) with go-qrcode Library
GraphQL in C# (.NET): Complete Guide to the GraphQL.NET Library