Graphics and Drawing
Graphics and Drawing
Aspose.PDF FOSS for .NET includes a drawing subsystem for adding vector
graphics to PDF pages. The Graph container holds shape elements (Line,
Arc, Circle) and is added to a page’s Paragraphs collection.
Creating a graph container
Graph defines a drawable area with a specified width and height.
using var doc = new Document();
var page = doc.Pages.Add();
var graph = new Graph(400, 200);
page.Paragraphs.Add(graph);Drawing lines
var line = new Line(new float[] { 0, 0, 300, 100 });
graph.Shapes.Add(line);Drawing arcs and circles
var arc = new Arc(150, 100, 80, 0, 180);
graph.Shapes.Add(arc);
var circle = new Circle(150, 100, 50);
graph.Shapes.Add(circle);Working with colors
The Color class provides factory methods for RGB, ARGB, and named colors.
var red = Color.FromRgb(1.0, 0.0, 0.0);
var semiTransparent = Color.FromArgb(128, 0, 0, 255);Drawing paths
DrawingPath supports MoveTo, LineTo, CurveTo, and Close for
freeform vector shapes.
var path = new DrawingPath();
path.MoveTo(10, 10);
path.LineTo(100, 10);
path.CurveTo(150, 50, 150, 100, 100, 100);
path.Close();Image formats
ImageFormat provides enumeration values for raster formats used when
converting pages to images: Bmp, Jpeg, Png, Tiff, Gif.
Tips and Best Practices
- Set graph dimensions to match the area you want to draw in — coordinates inside
Graphare relative to its bounds. - Use
Color.FromArgbfor semi-transparent fills (alpha channel support). - Combine multiple shapes in a single
Graphfor efficient rendering. DrawingPathis ideal for complex Bezier curves and arbitrary polygon shapes.- Add the
Graphto the page’sParagraphscollection — do not add it to annotations.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
| Shape not visible | Graph dimensions too small for the shape coordinates | Increase Graph width/height or scale shape coordinates |
| Colors appear different in viewer | Color space mismatch | Use Color.FromRgb with values in 0.0-1.0 range |
| Drawing overlaps text | Graph placed at wrong position in paragraph flow | Adjust paragraph ordering or use FloatingBox for absolute positioning |
FAQ
Can I draw filled shapes?
Yes. Set the shape’s GraphInfo.FillColor property to fill with a solid color.
How do I position a Graph at specific page coordinates?
Wrap the Graph in a FloatingBox and set its Left and Top properties
for absolute positioning.
Does the library support gradients?
The Shading classes support basic shading patterns. Use DrawingPath with
shading for gradient fills.
API Reference Summary
| Class / Method | Description |
|---|---|
Graph | Container for drawable shapes, added to page paragraphs |
Line | Straight line between two or more points |
Arc | Circular arc defined by center, radius, and angles |
Circle | Circle shape defined by center and radius |
DrawingPath | Freeform path with MoveTo, LineTo, CurveTo, Close |
Color | Color value with RGB, ARGB, and factory methods |
Color.FromRgb | Create a color from RGB components |
Color.FromArgb | Create a color with alpha transparency |
GraphicElementCollection | Collection of graphic elements on a page |
ImageFormat | Enumeration of raster image formats (Bmp, Jpeg, Png, Tiff, Gif) |