Docs / Start here

Getting started

Install SoftZenPdf, write a PDF with a few lines of C#, and learn the handful of ideas the rest of the library builds on.

Requirements

  • .NET 8 SDK or later. The library targets net8.0, so it works in any .NET 8, 9 or 10 application.
  • Nothing else. SoftZenPdf has no NuGet dependencies and no native binaries.
  • Any operating system .NET runs on. The library uses no platform-specific APIs; it is developed and tested on Windows.

Install

Add the package to your project:

Terminal
dotnet add package SoftZenPdf

The package is on nuget.org and has no other dependencies.

Without a license key SoftZenPdf runs in evaluation mode: everything works, but each saved page carries a small “evaluation copy” banner. Individuals and small businesses can get a free Community key; see Licensing and Pricing.

Your first PDF

Create a console app and add SoftZenPdf as above:

Terminal
dotnet new console -o HelloPdf
cd HelloPdf
dotnet add package SoftZenPdf

Replace the contents of Program.cs with:

Program.cs
using SoftZenPdf;

var document = new PdfDocument();
var page = document.AddPage(PageSize.A4);

page.Content
    .DrawText("Hello, SoftZenPdf", 72, 770, StandardFont.Helvetica, 24)
    .DrawRectangle(72, 560, 200, 120, fill: PdfColor.FromHex("#3B82F6"))
    .DrawCircle(420, 620, 60, stroke: PdfColor.Red, lineWidth: 3);

document.Save("hello.pdf");

Run it with dotnet run. The program writes hello.pdf next to it; open it in any PDF viewer:

hello.pdf
Hello, SoftZenPdf
The top of the A4 page produced by the program.

How the code works

  • PdfDocument holds everything in memory until you save it.
  • AddPage(PageSize.A4) appends a page and returns a PdfPage. Without a size you get the document's DefaultPageSize, which is A4 unless you change it.
  • page.Content is the page's ContentStreamWriter. Every drawing method returns it, so calls chain.
  • DrawText draws one line of text whose baseline starts at the given point. It does not wrap.
  • DrawRectangle and DrawCircle take an optional fill and stroke colour; give at least one.
  • Save writes the file. You can save the same document again after more changes.

The core concepts page explains these pieces in more depth.

Coordinates and units

PDF measures in points: 1 point is 1/72 inch, so an A4 page is about 595 × 842 points and US Letter is 612 × 792. The origin (0, 0) is the bottom-left corner of the page and y grows upwards, which is why y = 770 is near the top of an A4 page. Use the page's Width and Height to position things relative to its edges, and MeasureText to align text:

Program.cs
using SoftZenPdf;

var document = new PdfDocument();
var page = document.AddPage(PageSize.A4);
var font = StandardFont.Helvetica;

const string heading = "Centred heading";
var x = (page.Width - font.MeasureText(heading, 20)) / 2;

page.Content
    .DrawText(heading, x, page.Height - 72, font, 20)
    .DrawText("One inch from the bottom-left corner", 72, 72, font, 10, PdfColor.Gray);

document.Save("coordinates.pdf");

Page sizes

Use a named PageSize (ISO A0 to A6, Letter and Legal), or create one in points, millimetres or inches. Set DefaultPageSize once to change the size of every page added without one. Passing an orientation swaps width and height where needed.

Program.cs
using SoftZenPdf;

var document = new PdfDocument { DefaultPageSize = PageSize.Letter };   // A4 unless you change it
document.AddPage();                                            // Letter, the document's default
document.AddPage(orientation: PageOrientation.Landscape);      // Letter landscape, 792 × 612 pt
document.AddPage(PageSize.A6);                                 // a named ISO size, A0 to A6
document.AddPage(PageSize.FromMillimeters(100, 150));          // a custom size in millimetres
document.AddPage(new PageSize(300, 300));                      // a custom size in points

document.Save("page-sizes.pdf");

Saving to memory

A web application usually returns the PDF instead of writing a file. ToArray gives you the bytes, and Save(Stream) writes to any writable stream without closing it. To read a PDF held in memory, pass the bytes to PdfReader.FromBytes, or a stream (rewound to the start of the PDF) to PdfReader.FromStream; PdfFormFiller has the same two methods.

Program.cs
using SoftZenPdf;

var document = new PdfDocument();
document.AddPage().Content.DrawText("Generated in memory", 72, 770, StandardFont.Helvetica, 18);

byte[] pdf = document.ToArray();   // for example, return File(pdf, "application/pdf") from ASP.NET Core

using var stream = new MemoryStream();
document.Save(stream);             // or write to any writable Stream; it is left open

// Read it back without touching the disk: rewind, then read from the stream (or use PdfReader.FromBytes(pdf)).
stream.Position = 0;
var reader = PdfReader.FromStream(stream);
Console.WriteLine($"{reader.PageCount} page: {reader.ExtractText(0).Trim()}");
Output
1 page: Generated in memory

Text beyond Latin

The standard fonts need no embedding but only cover Western European characters (WinAnsiEncoding). For Greek, Cyrillic, CJK or anything else, load a TrueType font; SoftZenPdf embeds only the glyphs you use:

Program.cs
using SoftZenPdf;

var font = TrueTypeFont.FromFile("NotoSans-Regular.ttf");

var document = new PdfDocument();
document.AddPage().Content
    .DrawText("Ελληνικά, Кириллица, Latin: one embedded font", 72, 770, font, 16)
    .DrawText($"{font.FamilyName}, subset to the glyphs this page uses", 72, 745, font, 11, PdfColor.Gray);

document.Save("truetype-font.pdf");

Text is kerned, Hebrew and Arabic are drawn right to left, and Arabic letters are joined (see right-to-left text and Arabic). Indic scripts such as Devanagari are not shaped yet; see Known limitations.

Common first errors

You seeWhy, and what to do
ArgumentException: Character … has no WinAnsiEncoding codeA standard font cannot show that character. Draw the text with a TrueTypeFont instead.
Text or shapes are missing, or at the bottom of the pagey is measured up from the bottom edge. For text near the top of an A4 page, use a y around 770, or page.Height - 72.
InvalidOperationException when saving: content ends inside a text object, or unmatched SaveStateLow-level calls must be balanced: every BeginText needs an EndText and every SaveState a RestoreState. The Draw… methods do this for you.
ArgumentException: Specify a fill color, a stroke color, or bothShape helpers need at least one of fill: or stroke:.
PdfPasswordException when opening a fileThe PDF is encrypted. Pass the user or owner password to PdfReader.Open(path, password).

More answers are in FAQ and troubleshooting.

Next steps