Docotic.Pdf Library Help > Samples > Layers > Enumerate layers

This samples shows how to access and enumerate collection of PDF document layers (also known as optional content groups).

Use PdfDocument.Layers property to access teh collection of document layers. This collection is enumerable. Each item

in the collection is a PdfLayer onject. The PdfLayer object can be used to access layer properties like name, visibility state and intents.

C#
using BitMiracle.Docotic.Pdf;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class EnumerateLayers
    {
        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.

            using (PdfDocument pdf = new PdfDocument(@"Sample Data\BorderPinksOranges.pdf"))
            {
                PdfCollection<PdfLayer> layers = pdf.Layers;
                foreach (PdfLayer layer in layers)
                {
                    string message = string.Format("Name = {0}\nVisible = {1}\nIntents = ",
                        layer.Name, layer.Visible);

                    foreach (PdfLayerIntent intent in layer.GetIntents())
                    {
                        message += intent.ToString();
                        message += " ";
                    }

                    System.Windows.Forms.MessageBox.Show(message, "Layer Info");
                }
            }
        }
    }
}
VB
Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class EnumerateLayers
        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.

            Using pdf As New PdfDocument("Sample Data\BorderPinksOranges.pdf")
                Dim layers As PdfCollection(Of PdfLayer) = pdf.Layers
                For Each layer As PdfLayer In layers
                    Dim message As String = String.Format("Name = {0}" & vbLf & "Visible = {1}" & vbLf & "Intents = ", layer.Name, layer.Visible)

                    For Each intent As PdfLayerIntent In layer.GetIntents()
                        message += intent.ToString()
                        message += " "
                    Next

                    System.Windows.Forms.MessageBox.Show(message, "Layer Info")
                Next
            End Using
        End Sub
    End Class
End Namespace