Docotic.Pdf Library Help > Samples > Layers > Add layers to PDF

This samples shows how to create layers (also known as optional content groups) in a PDF document.

The PdfDocument class provides CreateLayer methods. You can use these methods to create layers in a PDF document.

All created layers are automatically added to collection of document layers.

C#
using System.Diagnostics;
using System.Drawing;
using System.IO;

using BitMiracle.Docotic.Pdf;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class CreateLayers
    {
        public static void Main()
        {
            // NOTE: 
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "CreateLayers.pdf";

            using (PdfDocument pdf = new PdfDocument())
            {
                pdf.PageMode = PdfPageMode.UseOC;

                PdfLayer firstLayer = pdf.CreateLayer("First Layer");
                firstLayer.Visible = false;

                PdfLayer secondLayer = pdf.CreateLayer("Second Layer", false, PdfLayerIntent.View);
                secondLayer.Visible = true;

                pdf.Save(pathToFile);
            }

            Process.Start(pathToFile);
        }
    }
}
VB
Imports System.Diagnostics
Imports System.Drawing
Imports System.IO

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class CreateLayers
        Private Sub New()
        End Sub
        Public Shared Sub Main()
            ' NOTE: 
            ' When used in trial mode, the library imposes some restrictions.
            ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            ' for more information.

            Dim pathToFile As String = "CreateLayers.pdf"

            Using pdf As New PdfDocument()
                pdf.PageMode = PdfPageMode.UseOC

                Dim firstLayer As PdfLayer = pdf.CreateLayer("First Layer")
                firstLayer.Visible = False

                Dim secondLayer As PdfLayer = pdf.CreateLayer("Second Layer", False, PdfLayerIntent.View)
                secondLayer.Visible = True

                pdf.Save(pathToFile)
            End Using

            Process.Start(pathToFile)
        End Sub
    End Class
End Namespace