Docotic.Pdf Library Help > Samples > Layers > Add annotations and controls to layers

This samples shows how to add annotations (widgets) and controls to layers in a PDF document.

All widget and control classes have Layer property. All you need to do to associate a widget or

a control with a layer is to assign the layer to the property.

To remove a widget or a control from a layer you only need to set the widget / control Layer property to null.

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

using BitMiracle.Docotic.Pdf;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class AddAnnotationsAndControlsToLayers
    {
        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 = "AddAnnotationsAndControlsToLayers.pdf";

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

                PdfLayer buttonsLayer = pdf.CreateLayer("Buttons");
                PdfLayer textLayer = pdf.CreateLayer("Text");

                PdfPage page = pdf.Pages[0];

                int top = 100;
                int height = 50;
                foreach (string name in new string[] { "Button 1", "Button 2", "Button 3" })
                {
                    PdfButton button = page.AddButton(name, new PdfRectangle(10, top, 100, height));
                    button.Text = name;
                    button.Layer = buttonsLayer;

                    top += height + 10;
                }

                top = 100;
                height = 50;
                foreach (string title in new string[] { "Text 1", "Text 2", "Text 3" })
                {
                    PdfTextAnnotation annot = page.AddTextAnnotation(new PdfPoint(200, top), title);
                    annot.Layer = textLayer;
                    top += height + 10;
                }

                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 AddAnnotationsAndControlsToLayers
        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 = "AddAnnotationsAndControlsToLayers.pdf"

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

                Dim buttonsLayer As PdfLayer = pdf.CreateLayer("Buttons")
                Dim textLayer As PdfLayer = pdf.CreateLayer("Text")

                Dim page As PdfPage = pdf.Pages(0)

                Dim top As Integer = 100
                Dim height As Integer = 50
                For Each name As String In New String() {"Button 1", "Button 2", "Button 3"}
                    Dim button As PdfButton = page.AddButton(name, New PdfRectangle(10, top, 100, height))
                    button.Text = name
                    button.Layer = buttonsLayer

                    top += height + 10
                Next

                top = 100
                height = 50
                For Each title As String In New String() {"Text 1", "Text 2", "Text 3"}
                    Dim annot As PdfTextAnnotation = page.AddTextAnnotation(New PdfPoint(200, top), title)
                    annot.Layer = textLayer
                    top += height + 10
                Next

                pdf.Save(pathToFile)
            End Using

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