News Category

Draw Ellipses in PDF in C#/VB.NET

2011-07-07 05:38:06 Written by  support iceblue
Rate this item
(0 votes)
pdf ellipses pdf ellipses

In geometry, an ellipse is a plane curve that results from the intersection of a cone by a plane in a way that produces a closed curve. Circles are special cases of ellipses, obtained when the cutting plane is orthogonal to the cone's axis. An ellipse is also the locus of all points of the plane whose distances to two fixed points add to the same constant. In this section, I will introduce a solution to draw ellipses in PDF document and set ellipses size, color and position via Spire.PDF for .NET in C#, VB.NET. First let us see the ellipses I draw in PDF document as below picture:

Draw Ellipses in PDF

For Spire.PDF for .NET users, we can easily draw ellipses and set ellipses size and position by this method: Spire.Pdf.PdfPageBase.Canvas.DrawEllipse(PdfPen pen, PdfBrush brush, float x, float y, float width, float height); There are six parameters passed in this method. The first parameter is a class in Spire.PDF: Spire.Pdf.Graphics.PdfPen which is responsible for drawing ellipses. When drawing PDF ellipses, we also can set the outline color and outline thickness by classes provided by Microsoft: System.Drawing.Color and System.Single. The second parameter is a class Spire.Pdf.Graphics.PdfBrush, when we define this class, we can set the ellipse color. The third and fourth parameters are the distance between the left and top margin with ellipse, which certainly decide the position of ellipse we draw. The last two parameters are ellipse width and height. To say in other word, the last two parameters can determine ellipse size.

Here we can download download Spire.PDF for .NET. After installing it on system, we can begin our task as below code:

[C#]
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;

namespace PDFellipses
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();
            // Create one page
            PdfPageBase page = doc.Pages.Add();
            //save graphics state
            PdfGraphicsState state = page.Canvas.Save();
            //draw ellipses in PDF and set ellipse size and position
            PdfPen pen = new PdfPen(Color.Red, 1f);
            PdfBrush brush = new PdfSolidBrush(Color.Yellow);
            PdfPen pen1 = new PdfPen(Color.DeepPink, 1f);
            PdfBrush brush1 = new PdfSolidBrush(Color.DeepSkyBlue);
            PdfPen pen2 = new PdfPen(Color.Cornsilk, 1f);
            PdfBrush brush2 = new PdfSolidBrush(Color.Red);
            PdfPen pen3 = new PdfPen(Color.SpringGreen, 1f);
            PdfBrush brush3 = new PdfSolidBrush(Color.SeaGreen);
            PdfPen pen4 = new PdfPen(Color.YellowGreen, 1f);
            PdfBrush brush4 = new PdfSolidBrush(Color.Orange);
            page.Canvas.DrawEllipse(pen, brush, 50, 70, 20, 60);
            page.Canvas.DrawEllipse(pen1, brush1, 100, 100, 20, 70);
            page.Canvas.DrawEllipse(pen2, brush2, 150, 150, 20, 70);
            page.Canvas.DrawEllipse(pen3, brush3, 200, 100, 20, 70);
            page.Canvas.DrawEllipse(pen4, brush4, 250, 70, 20, 60);
            //restor graphics
            page.Canvas.Restore(state);
            doc.SaveToFile("Ellipse.pdf");
            System.Diagnostics.Process.Start("Ellipse.pdf");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics

Namespace PDFellipses
	Class Program
		Private Shared Sub Main(args As String())
			'Create a pdf document.
			Dim doc As New PdfDocument()
			' Create one page
			Dim page As PdfPageBase = doc.Pages.Add()
			'save graphics state
			Dim state As PdfGraphicsState = page.Canvas.Save()
			'draw ellipses in PDF and set ellipse size and position
			Dim pen As New PdfPen(Color.Red, 1F)
			Dim brush As PdfBrush = New PdfSolidBrush(Color.Yellow)
			Dim pen1 As New PdfPen(Color.DeepPink, 1F)
			Dim brush1 As PdfBrush = New PdfSolidBrush(Color.DeepSkyBlue)
			Dim pen2 As New PdfPen(Color.Cornsilk, 1F)
			Dim brush2 As PdfBrush = New PdfSolidBrush(Color.Red)
			Dim pen3 As New PdfPen(Color.SpringGreen, 1F)
			Dim brush3 As PdfBrush = New PdfSolidBrush(Color.SeaGreen)
			Dim pen4 As New PdfPen(Color.YellowGreen, 1F)
			Dim brush4 As PdfBrush = New PdfSolidBrush(Color.Orange)
			page.Canvas.DrawEllipse(pen, brush, 50, 70, 20, 60)
			page.Canvas.DrawEllipse(pen1, brush1, 100, 100, 20, 70)
			page.Canvas.DrawEllipse(pen2, brush2, 150, 150, 20, 70)
			page.Canvas.DrawEllipse(pen3, brush3, 200, 100, 20, 70)
			page.Canvas.DrawEllipse(pen4, brush4, 250, 70, 20, 60)
			'restor graphics
			page.Canvas.Restore(state)
			doc.SaveToFile("Ellipse.pdf")
			System.Diagnostics.Process.Start("Ellipse.pdf")
		End Sub
	End Class
End Namespace

Spire.PDF for .NET is a professional PDF library that enables users draw different kinds of shapes in PDF document such as rectangles, arcs, circles, ellipse, five-pointed star etc.

Additional Info

  • tutorial_title: Draw PDF Ellipses
Last modified on Sunday, 26 September 2021 02:08