# Graphics

Work with DsPdf to draw shapes and add color, gradient, and transformation into it.

## Content

Graphics are visual elements that can be displayed in the form of different shapes, lines, curves or images in a document. These are generally used to supplement text for better illustration of a theory or concept.
DsPdf allows you to draw graphics in a document using methods such as [DrawRectangle](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.DrawRectangle.html), [DrawEllipse](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.DrawEllipse.html), etc., available in [GcGraphics](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.html) class. These methods use an object of [GcPdfGraphics](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Pdf/GrapeCity.Documents.Pdf.GcPdfGraphics.html) class to draw graphics on a page. Following is a list of graphic elements supported by DsPdf:

* Line
* Rectangle
* Ellipse
* Polygon
* Path

![Graphic Elements](https://cdn.mescius.io/document-site-files/images/884d2bfb-301b-49bc-8d90-656c5b910507/images/graphics.png)

## Add Shape

To add a shape in a PDF document:

1. Create an object of [GcPdfDocument](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Pdf/GrapeCity.Documents.Pdf.GcPdfDocument.html) class.
2. Add a blank page to the document using GcPdfDocument object.
3. Draw an ellipse using [DrawEllipse](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.DrawEllipse.html) method provided by [GcGraphics](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.html) class.
<br>
    The following code snippet shows how to add a shape in a PDF document.

    ```csharp
    public void CreatePDF(Stream stream)
    {
        // Create a new PDF document:
        var doc = new GcPdfDocument();
        var page = doc.NewPage();
        var g = page.Graphics;
    
        // Pen used to draw shape
        var pen = new Pen(Color.Orange, 2);
        // Draw a shape
        g.DrawEllipse(new RectangleF(0.0F, 0.0F, 200.0F, 100.0F), pen);
    
        // Save document
        doc.Save(stream);
    }
    ```

## Fill Shape

To fill a shape:

1. Create an object of [GcPdfDocument](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Pdf/GrapeCity.Documents.Pdf.GcPdfDocument.html) class.
2. Add a blank page to document using the GcPdfDocument object.
3. Draw an ellipse using the [DrawEllipse](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.DrawEllipse.html) method provided by the [GcGraphics](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.html) class.
4. Fill the shape using the [FillEllipse](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.FillEllipse.html) method of GcGraphics class.

    ```csharp
    public void CreatePDF(Stream stream)
    {
        // Create a new PDF document:
        var doc = new GcPdfDocument();
        var page = doc.NewPage();
        var g = page.Graphics;
        // Pen used to draw shape
        var pen = new Pen(Color.Orange, 2);
        // Draw a Shape
        g.DrawEllipse(new RectangleF(0.0F, 0.0F, 200.0F, 100.0F), pen);
        // Fill a Shape
        g.FillEllipse(new RectangleF(0.0F, 0.0F, 200.0F, 100.0F), Color.Orange);
        // Save document
        doc.Save(stream);
    }
    ```

## Add Gradients

To use gradients in a PDF document:

1. Draw a shape by creating an instance of class corresponding to shape you want to add to a page, for example, DrawRectangle class.
2. Create a linear gradient brush by initializing the [LinearGradientBrush](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.LinearGradientBrush.html) class and specify the start color and end color for the gradient.
3. Fill the shape by passing the object of LinearGradientBrush in the corresponding fill method, for example, FillRectangle.

    ```csharp
    public void CreatePDF(Stream stream)
    {
        // Create a new PDF document:
        var doc = new GcPdfDocument();
        var page = doc.NewPage();
        var g = page.Graphics;
        // Pen used for Drawing
        var pen = new Pen(Color.Orange, 2);
        // Draw a Shape
        g.DrawRectangle(new RectangleF(0.0F, 0.0F, 200.0F, 100.0F), pen);
        // Create a linear gradient brush
        LinearGradientBrush linearGradBrush = new LinearGradientBrush(Color.Red, Color.Blue);
        // Fill a Shape
        g.FillRectangle(new RectangleF(0.0F, 0.0F, 200.0F, 100.0F), linearGradBrush);
        // Save document
        doc.Save(stream);
    }
    ```

Similarly, the [RadialGradientBrush](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.RadialGradientBrush.html) class provides radial gradient brush and the [HatchBrush](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.HatchBrush.html) class provides hatch patterns to fill the shapes.

## Add Transformations

To perform transformations using DsPdf, set the [Transform](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.Transform.html) property provided by the GcGraphics class. In this example, we have transformed the rectangle box, which is scaled by 0.7, translated by (3',5'), and rotated 70 degrees counterclockwise. The values for transformation are calculated with the help of different methods provided by the [Matrix3x2](https://docs.microsoft.com/en-us/dotnet/api/system.numerics.matrix3x2?view=netframework-4.8) class.

```csharp
public void CreatePDF(Stream stream)
{
    // Create a PDF document
    var doc = new GcPdfDocument();
    var page = doc.NewPage();
    var g = page.Graphics;

    // Translation Values
    var translate0 = Matrix3x2.CreateTranslation(72 * 3, 72 * 5);
    var scale0 = Matrix3x2.CreateScale(0.7F);
    var rotate0 = Matrix3x2.CreateRotation((float)(-70 * Math.PI) / 180F);

    //Draw Rectangle and Apply Transformations
    var box = new RectangleF(0, 0, 72 * 4, 72 * 2);
    g.Transform = rotate0 * translate0 * scale0;
    g.FillRectangle(box, Color.FromArgb(100, Color.DarkSeaGreen));
    g.DrawRectangle(box, Color.DarkOrange, 1);
    g.DrawString("Sample Box: Text drawn at (0,0) in a 4\"x2\" box"+
    ", scaled by 0.7, translated by (3\",5\"), " +
     "and rotated 70 degrees counterclockwise.",
    new TextFormat() { Font = StandardFonts.Times, FontSize = 14, }, box);
    // Save document
    doc.Save(stream);
}
```

For more information about implementation of graphics using DsPdf, see [DsPdf sample browser](https://developer.mescius.com/documents-api-pdf/demos/basics/graphics/shapes/code-cs).