How To Draw Circles And Ellipses In PDF Documents Using PDF Mosaic .NET

This sample shows how to draw circles and ellipses, and how to use such operations as path, clipping.

Circles And Ellipses

 


using PDFMosaic;
using System;
using System.Drawing;

namespace CirclesEllipses
{
    class CirclesEllipses
    {
        static void Main()
        {
            PDFDocument document = new PDFDocument();
            document.Pages.Add(new PDFPage(PDFPaperFormat.A4));
            
            PDFCanvas canvas = document.Pages[0].Canvas;
            PDFPath path = new PDFPath();
            path.AddEllipse(50, 50, document.Pages[0].Width - 100, document.Pages[0].Height - 100);
            canvas.SetClip(path);
            canvas.DrawPath(new PDFSolidPen(), path);
            path.Reset();

            Random rand = new Random();
            for (int i = 0; i < 200; ++i)
            {
                float x = (float)rand.NextDouble() * (document.Pages[0].Width - 100 + 1);
                float y = (float)rand.NextDouble() * (document.Pages[0].Height - 100 + 1);
                float s = (float)rand.NextDouble() * (150 + 1);

                if (i % 2 == 0)
                {
                    path.AddCircle(x, y, s);
                }
                else
                {
                    float k = (float)rand.NextDouble() * (150 + 1);
                    path.AddEllipse(x, y, s, k);
                }
                PDFColor color = new PDFColorRGB((byte)rand.Next(256), (byte)rand.Next(256), (byte)rand.Next(256));
                canvas.DrawPath(new PDFSolidPen(color), new PDFSolidBrush(color), path);
                path.Reset();
            }
            
            path.AddEllipse(50, 50, document.Pages[0].Width - 100, document.Pages[0].Height - 100);
            canvas.SetClip(path);
            PDFSolidPen pen = new PDFSolidPen();
            pen.Width = 2.5f;
            canvas.DrawPath(pen, path);
            
            document.Save("TestCirclesEllipses.pdf", true);
        }
    }
}