How to Create Layers (Optional Contrent Groups) in PDF

See more details about layers here: http://www.colorpilot.com/blog/layers-optional-content-groups/
This sample shows you how to create multiple optional content groups.

Layers

C# :

using PDFMosaic;
using System;

namespace AddLayers
{
  class AddLayers
  {
    static void Main()
    {
      PDFDocument document = new PDFDocument();
      PDFPage page = new PDFPage(PDFPaperFormat.A4);
      document.Pages.Add(page);
      PDFCanvas canvas = page.Canvas;

      PDFSolidBrush br = new PDFSolidBrush(new PDFColorRGB(0, 0, 255));

      PDFLayer allContents = new PDFLayer("All contents");
      canvas.BeginMarkedContent(allContents);

      PDFLayer circle = new PDFLayer("Circle");
      canvas.BeginMarkedContent(circle);
      canvas.DrawCircle(br, 100, 100, 50);
      canvas.EndMarkedContent();

      PDFLayer text = new PDFLayer("Text");
      canvas.BeginMarkedContent(text);
      br.Color = new PDFColorRGB(0, 0, 0);
      canvas.DrawString("Text text", new PDFFont(PDFStandardFont.Helvetica, 14), br, 200, 100);
      canvas.EndMarkedContent();

      canvas.EndMarkedContent();

      document.OptionalContents.Layers.Add(circle);
      document.OptionalContents.Layers.Add(text);
      document.OptionalContents.Layers.Add(allContents);

      PDFOptionalContentGroup group = new PDFOptionalContentGroup();
      group.Add(new PDFOptionalContentGroupLayer(circle));
      group.Add(new PDFOptionalContentGroupLayer(text));

      document.OptionalContents.Configuration.Order.Add(new PDFOptionalContentGroupLayer(allContents));
      document.OptionalContents.Configuration.Order.Add(group);

      document.PageMode = PDFPageMode.OptionalContent;
      document.Save("AddLayers.pdf", true);
    }
  }
}

VB.NET :

Imports PDFMosaic
Imports System

Module AddLayers
  Sub Main()
    Dim document As New PDFDocument()
    Dim page As New PDFPage(PDFPaperFormat.A4)
    document.Pages.Add(page)
    Dim canvas = page.Canvas

    Dim br = New PDFSolidBrush(New PDFColorRGB(0, 0, 255))

    Dim allContents As New PDFLayer("All contents")
    canvas.BeginMarkedContent(allContents)

    Dim circle As New PDFLayer("Circle")
    canvas.BeginMarkedContent(circle)
    canvas.DrawCircle(br, 100, 100, 50)
    canvas.EndMarkedContent()

    Dim text As New PDFLayer("Text")
    canvas.BeginMarkedContent(text)
    br.Color = New PDFColorRGB(0, 0, 0)
    canvas.DrawString("Text text", New PDFFont(PDFStandardFont.Helvetica, 14), br, 200, 100)
    canvas.EndMarkedContent()

    canvas.EndMarkedContent()

    document.OptionalContents.Layers.Add(circle)
    document.OptionalContents.Layers.Add(text)
    document.OptionalContents.Layers.Add(allContents)

    Dim group As New PDFOptionalContentGroup()
    group.Add(New PDFOptionalContentGroupLayer(circle))
    group.Add(New PDFOptionalContentGroupLayer(text))

    document.OptionalContents.Configuration.Order.Add(New PDFOptionalContentGroupLayer(allContents))
    document.OptionalContents.Configuration.Order.Add(group)

    document.PageMode = PDFPageMode.OptionalContent
    document.Save("AddLayers.pdf", True)
  End Sub
End Module