Ellipses

This example demonstrates how to draw ellipses on a PDF document.

Ellipse is a graphical shape used for many purposes

When using ellipse you can set coordinates , height and width of ellipse

Clicking on generate button will launch the pdf with several ellipse with different size and color

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using OfficeComponent.Pdf;
using OfficeComponent.Pdf.Graphics;

namespace OfficeComponent.Samples
{
    class EllipsesExample : ExampleBase
    {
        public EllipsesExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {
        }

        public EllipsesExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
        {
        }

        public override string Execute()
        {
            // Create a new instance of PdfDocument class.
            PdfDocument doc = new PdfDocument();

            //Set the font
            Font f = new Font("Arial", 12.0f);
            PdfTrueTypeFont font = new PdfTrueTypeFont(f, false);

            // Add the first page.
            PdfPage page = doc.Pages.Add();

            PdfGraphics g = page.Graphics;

            //Create a text element 
            PdfTextElement element = new PdfTextElement("Random Ellipses on page 1!!", font);
            element.Brush = new PdfSolidBrush(Color.Black);
            element.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
            element.Draw(g, new PointF(320, 0));

            float contentWidth = page.Size.Width - doc.PageSettings.Margins.Left - doc.PageSettings.Margins.Right;
            float contentHeight = page.Size.Height - doc.PageSettings.Margins.Left - doc.PageSettings.Margins.Right;

            Random rnd = new Random();

            PdfLine line = new PdfLine(new PointF(0, 0), new PointF(contentWidth, contentHeight));
            line.Draw(g);

            PdfRectangle rec = new PdfRectangle(0, 0, contentWidth, contentHeight);
            rec.Draw(g);

            // Draw 30 ellipses
            for (int i = 0; i < 30; i++)
            {
                float left = rnd.Next((int)contentWidth);
                float top = rnd.Next((int)contentHeight);
                PdfEllipse ellipse = new PdfEllipse(left,
                    top,
                    rnd.Next((int)(contentWidth - left)),
                    rnd.Next((int)(contentHeight - top)));
                // Random border color.
                ellipse.Pen = new PdfPen(new PdfColor((byte)rnd.Next(256), (byte)rnd.Next(256), (byte)rnd.Next(256)), 1);

                ellipse.Draw(g);
            }




            // Save and close the document.
            var outputPath = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + ".pdf");
            doc.Save(outputPath);
            doc.Close(true);

            return outputPath;
        }
    }
}
Imports System.IO
Imports System.Text
Imports OfficeComponent.Pdf
Imports OfficeComponent.Pdf.Graphics

Namespace OfficeComponent.Samples
	Friend Class EllipsesExample
		Inherits ExampleBase
		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
			MyBase.New(commonDataPath, outputDir)
		End Sub

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
			MyBase.New(commonDataPath, outputDir, xmlFile)
		End Sub

		Public Overrides Function Execute() As String
			' Create a new instance of PdfDocument class.
			Dim doc As New PdfDocument()

			'Set the font
			Dim f As New Font("Arial", 12.0F)
			Dim font As New PdfTrueTypeFont(f, False)

			' Add the first page.
			Dim page As PdfPage = doc.Pages.Add()

			Dim g As PdfGraphics = page.Graphics

			'Create a text element 
			Dim element As New PdfTextElement("Random Ellipses on page 1!!", font)
			element.Brush = New PdfSolidBrush(Color.Black)
			element.Font = New PdfStandardFont(PdfFontFamily.Helvetica, 12)
			element.Draw(g, New PointF(320, 0))

			Dim contentWidth As Single = page.Size.Width - doc.PageSettings.Margins.Left - doc.PageSettings.Margins.Right
			Dim contentHeight As Single = page.Size.Height - doc.PageSettings.Margins.Left - doc.PageSettings.Margins.Right

			Dim rnd As New Random()

			Dim line As New PdfLine(New PointF(0, 0), New PointF(contentWidth, contentHeight))
			line.Draw(g)

			Dim rec As New PdfRectangle(0, 0, contentWidth, contentHeight)
			rec.Draw(g)

			' Draw 30 ellipses
			For i As Integer = 0 To 29
				Dim left As Single = rnd.Next(CInt(Fix(contentWidth)))
				Dim top As Single = rnd.Next(CInt(Fix(contentHeight)))
				Dim ellipse As New PdfEllipse(left, top, rnd.Next(CInt(Fix(contentWidth - left))), rnd.Next(CInt(Fix(contentHeight - top))))
				' Random border color.
				ellipse.Pen = New PdfPen(New PdfColor(CByte(rnd.Next(256)), CByte(rnd.Next(256)), CByte(rnd.Next(256))), 1)

				ellipse.Draw(g)
			Next i




			' Save and close the document.
			Dim outputPath = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & ".pdf")
			doc.Save(outputPath)
			doc.Close(True)

			Return outputPath
		End Function
	End Class
End Namespace