Docs / Guides

Drawing shapes and colours

Vector graphics in SoftZenPdf range from one-line helpers for common shapes to the raw path operators of the PDF specification, with RGB colours, opacity and coordinate transforms.

Shape helpers

DrawLine, DrawRectangle, DrawEllipse and DrawCircle take a fill colour, a stroke colour or both, and a lineWidth in points. Each saves and restores the graphics state, so its colours never affect later drawing.

Program.cs
using SoftZenPdf;

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

content
    .DrawLine(72, 780, 520, 780, PdfColor.Gray, lineWidth: 0.5)
    .DrawRectangle(72, 620, 180, 120, fill: PdfColor.FromHex("#864FFE"))
    .DrawEllipse(390, 680, 110, 60, fill: PdfColor.FromHex("#E8FBC6"), stroke: PdfColor.Black, lineWidth: 2);

// Low-level path operators, with 50% opacity limited by SaveState/RestoreState.
content
    .SaveState()
    .SetOpacity(0.5)
    .SetFillColor(PdfColor.Blue)
    .MoveTo(72, 560).CurveTo(160, 640, 280, 480, 360, 560).LineTo(360, 480).LineTo(72, 480).ClosePath()
    .Fill()
    .RestoreState();

document.Save("shapes.pdf");

Colours and opacity

PdfColor is an RGB colour. Create it from a CSS-style hex string, from 0–255 components or from 0–1 components, or use the named ones (Black, White, Red, Green, Blue, Gray). SetOpacity makes later fills and strokes translucent; wrap it in SaveState/RestoreState to limit its effect.

Program.cs
using SoftZenPdf;

var brand = PdfColor.FromHex("#864FFE");        // CSS-style hex
var teal = PdfColor.FromRgb(20, 184, 166);        // 0–255 components
var grey = new PdfColor(0.9, 0.9, 0.9);           // 0–1 components

var document = new PdfDocument();
var content = document.AddPage().Content;
content
    .DrawRectangle(72, 640, 150, 100, fill: grey, stroke: teal, lineWidth: 4)
    .DrawCircle(330, 690, 50, fill: brand);

// Rotate a square 30° around its centre (cx, cy): translate, rotate, translate back.
double cx = 150, cy = 450, angle = 30 * Math.PI / 180, cos = Math.Cos(angle), sin = Math.Sin(angle);
content
    .SaveState()
    .SetOpacity(0.6)
    .Transform(cos, sin, -sin, cos, cx - cos * cx + sin * cy, cy - sin * cx - cos * cy)
    .DrawRectangle(cx - 50, cy - 50, 100, 100, fill: brand)
    .RestoreState();

document.Save("colors-and-transforms.pdf");

Paths

For other shapes, build a path and then paint it. A path starts with MoveTo, continues with LineTo, CurveTo (a cubic Bézier curve with two control points), and can include Rectangle and Ellipse subpaths; ClosePath joins the end back to the start. Then Stroke, Fill or FillAndStroke paints it using the current colours and line width. Filling uses the non-zero winding rule.

Transforms

Transform(a, b, c, d, e, f) changes the coordinate system for everything drawn after it. Always wrap it in SaveState/RestoreState. Common matrices:

EffectArguments
Move by (tx, ty)1, 0, 0, 1, tx, ty
Scale by (sx, sy)sx, 0, 0, sy, 0, 0
Rotate by θ counter-clockwise around the origincos θ, sin θ, -sin θ, cos θ, 0, 0
Rotate by θ around (cx, cy)cos θ, sin θ, -sin θ, cos θ, cx - cos θ·cx + sin θ·cy, cy - sin θ·cx - cos θ·cy

Limits

Colours are DeviceRGB only: there is no CMYK, greyscale, spot colour, gradient or pattern support, and no API yet for dash patterns, line caps or line joins.

API: ContentStreamWriter, PdfColor.