PDF Mosaic Pilot: How to manipulate primitives to clip drawing objects


Home       Features       Download       Tutorial       Version History       License       Source Code

 

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.

C# :

using PDFMosaic;
using System.Drawing;
using System;
 
namespace Clipping
{
  class Clipping
  {
    static void Main(string[] args)
    {
      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);
    }
  }
}

 

Visual Basic :

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

 


Home       Features       Download       Tutorial       Version History       License       Source Code