How To Use Current Graphics Path As a Clip

This sample shows how to use current graphics path as a clip region of a canvas.

When a clip region is set on a canvas, any part of a drawn object (including text and images) that lay outside the clip region will not be visible.

Clipping

C# :

using PDFMosaic;
using System.Drawing;
using System;

namespace Clipping
{
  class Clipping
  {
    static void Main()
    {
      PDFDocument document = new PDFDocument();
      document.Pages.Add(new PDFPage(PDFPaperFormat.A4));
      PDFCanvas canvas = document.Pages[0].Canvas;

      canvas.SaveGraphicsState();
      PointF start = new PointF(200, 200);
      PointF[] points = new PointF[5];
      for (int i = 0; i < 5; ++i)
      {
        points[i].X = start.X + 50 * (float)Math.Cos((i * 2 * 72 - 90) * Math.PI / 180);
        points[i].Y = start.Y + 50 * (float)Math.Sin((i * 2 * 72 - 90) * Math.PI / 180);
      }

      PDFPath path = new PDFPath();
      path.AddPolygon(points);
      canvas.SetClip(path);

      PDFSolidBrush brush = new PDFSolidBrush(new PDFColorRGB(0, 255, 0));
      canvas.DrawRectangle(brush, start.X - 50, start.Y - 50, start.X + 50, start.Y + 50);
      canvas.RestoreGraphicsState();

      document.Save("Clipping.pdf", true);
    }
  }
}

VB.NET :

Imports PDFMosaic
Imports System.Drawing
Imports System

Module Clipping
  Sub Main()
    Dim document As PDFDocument = New PDFDocument()
    document.Pages.Add(New PDFPage(PDFPaperFormat.A4))

    Dim canvas As PDFCanvas = document.Pages(0).Canvas
    canvas.SaveGraphicsState()

    Dim start As PointF = New PointF(200, 200)
    Dim points As PointF() = New PointF(5) {New PointF(0, 0), New PointF(0, 0), New PointF(0, 0), New PointF(0, 0), New PointF(0, 0), New PointF(0, 0)}
    For i As Integer = 0 To 5
      points(i).X = start.X + 50 * Math.Cos(((i * 2 * 72 - 90) * Math.PI / 180))
      points(i).Y = start.Y + 50 * Math.Sin(((i * 2 * 72 - 90) * Math.PI / 180))
    Next

    Dim path As PDFPath = New PDFPath()
    path.AddPolygon(points)
    canvas.SetClip(path)

    Dim brush As PDFSolidBrush = New PDFSolidBrush(New PDFColorRGB(0, 255, 0))
    canvas.DrawRectangle(Brush, start.X - 50, start.Y - 50, start.X + 50, start.Y + 50)
    canvas.RestoreGraphicsState()
    document.Save("Clipping.pdf", True)
  End Sub
End Module