How To Save And Restore Graphics State

This sample shows how to save and restore graphics state using
use PDFCanvas.SaveGraphicsState and PdfCanvas.RestoreGraphicsState
methods.

Graphics State

C# :

using PDFMosaic;
using System.Drawing;
using System;

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

      canvas.SaveGraphicsState();
      PDFPath path = new PDFPath();
      path.AddCircle(100, 100, 50);
      canvas.SetClip(path);
      canvas.DrawRectangle(new PDFSolidBrush(new PDFColorRGB(0, 255, 0)), 50, 50, 50, 50);
      canvas.RestoreGraphicsState();
      canvas.DrawRectangle(new PDFSolidBrush(new PDFColorRGB(255, 0, 0)), 100, 50, 50, 50);

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


VB.NET :

Imports PDFMosaic
Imports System.Drawing
Imports System

Module GraphicsState
  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 path As PDFPath = New PDFPath()
    path.AddCircle(100, 100, 50)
    canvas.SetClip(path)
    canvas.DrawRectangle(New PDFSolidBrush(New PDFColorRGB(0, 255, 0)), 50, 50, 50, 50)
    canvas.RestoreGraphicsState()
    canvas.DrawRectangle(New PDFSolidBrush(New PDFColorRGB(255, 0, 0)), 100, 50, 50, 50)

    document.Save("GraphicsState.pdf", True)
  End Sub
End Module