Docs / Start here

Core concepts

A handful of ideas explain almost everything in SoftZenPdf. They come straight from the PDF specification (ISO 32000), so they also help when you read other PDF documentation.

The main types

TypeRole
PdfDocumentA document you are building: pages, metadata, bookmarks, form, watermark and encryption settings. Saved with Save or ToArray.
PdfPageOne page: its size, rotation, links and form fields, and its drawing surface.
ContentStreamWriterThe drawing surface of a page (page.Content): text, shapes, images.
PdfReaderA read-only view of an existing file: pages, metadata, bookmarks, links, form fields and text.
PdfFormFillerFills the form of an existing file by appending an incremental update.

Documents live in memory

A PdfDocument keeps every page, font and image in memory until you save it; nothing is written to disk before Save. Saving does not change the document, so you can keep editing and save again. Fonts are subset and compressed at save time, which is why the output only contains the glyphs you actually used.

Coordinates, units and the baseline

Positions and sizes are in points (1/72 inch). The origin is the bottom-left corner of the page and y grows upwards. Text is positioned by its baseline: the line the letters sit on, with descenders such as g and p hanging below it.

Heading baseline, y = 770 (0, 0) x y 595 pt (A4)

Rotation on a page only tells viewers how to turn it for display; you still draw in the unrotated coordinate system. To rotate or scale what you draw, use Transform, explained in Drawing shapes and colours.

Content streams and the graphics state

In PDF, each page's appearance is a content stream: a list of drawing operators. ContentStreamWriter writes those operators for you. The Draw… methods are high-level helpers that save and restore the graphics state around themselves; the other methods map one-to-one onto PDF operators:

MethodsPDF operatorsPurpose
SaveState, RestoreStateq, QPush and pop the graphics state (colours, line width, opacity, transform)
MoveTo, LineTo, CurveTo, Rectangle, ClosePathm l c re hBuild a path
Stroke, Fill, FillAndStrokeS f BPaint the path
BeginText … EndTextBT … ETA text object; inside it, SetFont, MoveTextPosition, SetTextMatrix, ShowText
TransformcmChange the coordinate system
Program.cs
using SoftZenPdf;

var document = new PdfDocument();
var content = document.AddPage().Content;

// A text object: several lines positioned relative to each other.
content
    .BeginText()
    .SetFont(StandardFont.TimesRoman, 12)
    .MoveTextPosition(72, 770)
    .ShowText("First line")
    .MoveTextPosition(0, -16)
    .ShowText("Second line, 16 points lower")
    .EndText();

// The graphics state stack: the red fill colour ends at RestoreState.
content
    .SaveState()
    .SetFillColor(PdfColor.Red)
    .Rectangle(72, 600, 100, 50).Fill()
    .RestoreState()
    .Rectangle(200, 600, 100, 50).Fill();   // black again

document.Save("low-level-content.pdf");

SoftZenPdf checks operator order as you go: a path operator inside a text object, or ShowText outside one, throws InvalidOperationException immediately, and saving fails if a page ends with an open text object or unmatched SaveState.

Resources: fonts, images and opacity

Fonts, images and opacity settings are resources: objects stored once in the file and referenced by name from content streams. You never manage them yourself. The first time a page uses a font or image, SoftZenPdf adds it to the document and to that page's resource dictionary. Each StandardFont, TrueTypeFont or PdfImage instance is embedded once per document, however many pages use it, so load a font or image once and reuse the object.

Reading versus writing

SoftZenPdf never rewrites an existing file in place. To change a PDF you read it with PdfReader and build a new document from its pages with ImportPages, Merge or Extract, optionally drawing on top of them. The one exception is form filling: PdfFormFiller appends an incremental update, so the original bytes stay exactly as they were.

The low-level object model

Underneath, a PDF is a graph of objects: dictionaries, arrays, names, numbers, strings, streams and references between them. The SoftZenPdf.Objects namespace exposes them for inspection and for structures SoftZenPdf has no high-level API for. Most applications never need it.

Program.cs
using SoftZenPdf;

var reader = PdfReader.Open("hello.pdf");
Console.WriteLine($"PDF {reader.Version}, {reader.PageCount} page(s)");

var page = reader.GetPage(0);
Console.WriteLine(page["MediaBox"]);                        // [0 0 595.275591 841.889764]
Console.WriteLine(reader.Resolve(page["Resources"]));       // the page's fonts and other resources
Output
PDF 1.7, 1 page(s)
[0 0 595.275591 841.889764]
<< /Font << /F1 5 0 R >> >>

Errors and threading

  • Fail early. Invalid input throws as soon as you pass it (for example a colour outside 0–1, or a page size smaller than 3 points), and messages often cite the relevant clause of ISO 32000 or ISO 19005.
  • Reading is forgiving. Damaged files are repaired where possible; problems that were worked around are listed in PdfReader.Warnings.
  • Not thread-safe. PdfDocument, PdfReader and PdfFormFiller instances must not be used from several threads at once; use one instance per thread.