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 Graph are relative to its bounds.
  • Use Color.FromArgb for semi-transparent fills (alpha channel support).
  • Combine multiple shapes in a single Graph for efficient rendering.
  • DrawingPath is ideal for complex Bezier curves and arbitrary polygon shapes.
  • Add the Graph to the page’s Paragraphs collection — do not add it to annotations.

Common Issues

IssueCauseFix
Shape not visibleGraph dimensions too small for the shape coordinatesIncrease Graph width/height or scale shape coordinates
Colors appear different in viewerColor space mismatchUse Color.FromRgb with values in 0.0-1.0 range
Drawing overlaps textGraph placed at wrong position in paragraph flowAdjust 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 / MethodDescription
GraphContainer for drawable shapes, added to page paragraphs
LineStraight line between two or more points
ArcCircular arc defined by center, radius, and angles
CircleCircle shape defined by center and radius
DrawingPathFreeform path with MoveTo, LineTo, CurveTo, Close
ColorColor value with RGB, ARGB, and factory methods
Color.FromRgbCreate a color from RGB components
Color.FromArgbCreate a color with alpha transparency
GraphicElementCollectionCollection of graphic elements on a page
ImageFormatEnumeration of raster image formats (Bmp, Jpeg, Png, Tiff, Gif)

See Also

 English