How to Add Graphics and Drawings to PDFs in .NET

How to Add Graphics and Drawings to PDFs in .NET

How to Add Graphics and Drawings to PDFs in .NET

This guide shows how to draw vector shapes on PDF pages using the Graph container.


Prerequisites

RequirementDetail
Runtime.NET 8.0 or later
Packagedotnet add package Aspose.Pdf.Foss

Create a graph and draw a line

The Graph class acts as a container for vector shapes on a PDF page. Create a graph with a fixed width and height, add shape objects to its Shapes collection, then add the graph to the page’s Paragraphs collection. The layout engine positions it automatically alongside other paragraph elements.

using var doc = new Document();
var page = doc.Pages.Add();

var graph = new Graph(400, 200);
var line = new Line(new float[] { 0, 0, 300, 100 });
graph.Shapes.Add(line);
page.Paragraphs.Add(graph);
doc.Save("drawing.pdf");

Draw a circle

Add a Circle shape by specifying the center coordinates and radius. All shapes share the same Graph container and render in the order they are added.

var circle = new Circle(150, 100, 50);
graph.Shapes.Add(circle);

Draw a freeform path

For complex shapes, use DrawingPath to build an arbitrary vector path with move, line, and Bezier curve segments. Call Close to connect the last point back to the start, forming a closed shape that can be filled or stroked.

var path = new DrawingPath();
path.MoveTo(10, 10);
path.LineTo(100, 10);
path.CurveTo(150, 50, 150, 100, 100, 100);
path.Close();

Key Classes

ClassPurpose
GraphDrawable shape container
LineLine shape
CircleCircle shape
ArcArc shape
DrawingPathFreeform vector path
ColorColor values (RGB, ARGB)

See Also