Document Solutions for PDF
Features / Graphics
In This Topic
    Graphics
    In This Topic

    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, DrawEllipse, etc., available in GcGraphics class. These methods use an object of GcPdfGraphics class to draw graphics on a page. Following is a list of graphic elements supported by DsPdf:

    Graphic Elements

    Add Shape

    To add a shape in a PDF document:

    1. Create an object of GcPdfDocument class.
    2. Add a blank page to the document using GcPdfDocument object.
    3. Draw an ellipse using DrawEllipse method provided by GcGraphics class.

      The following code snippet shows how to add a shape in a PDF document.

      C#
      Copy Code
      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);
      }
      
    Back to Top

    Fill Shape

    To fill a shape:

    1. Create an object of GcPdfDocument class.
    2. Add a blank page to document using the GcPdfDocument object.
    3. Draw an ellipse using the DrawEllipse method provided by the GcGraphics class.
    4. Fill the shape using the FillEllipse method of GcGraphics class.
      C#
      Copy Code
      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);
      }
      
    Back to Top

    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 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.
      C#
      Copy Code
      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 class provides radial gradient brush and the HatchBrush class provides hatch patterns to fill the shapes.

    Back to Top

    Add Transformations

    To perform transformations using DsPdf, set the Transform 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 class.

    C#
    Copy Code
    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);
    }
    
    Back to Top

    For more information about implementation of graphics using DsPdf, see DsPdf sample browser.