Home > Articles > .Net Framework Articles > Add, Hide, Rename and Delete PDF Layers in C#

Add, Hide, Rename and Delete PDF Layers in C#

by Michael Brown   on Nov 15, 2017   Category: .Net Framework  | Level: Beginner  |  Views: 546    |  Points: 100   
Like this article? Bookmark and Share:
This article shows how to add layers to PDF, hide or show layers, rename layers and delete layers from PDF using c#.


Since the PDF 1.5 specification, PDF starts to support Layers. Information such as text, images and shapes can be stored on different layers of a PDF, for instance, we can add some text on a layer, and add an image to another layer behind of the text. Each layer can have a name and one of its key features is that users can show or hide the content associated with the layer, let’s say, we can add a text watermark on a layer and hide it, so that it can keep invisible during reading but becomes visible when printing.

This article elaborates how to operate PDF layers programmatically in c# using a .NET PDF library – Spire.PDF. Contents summary are as show below.

  •        Add layers to PDF
  •        Show or hide layers
  •        Rename a layer
  •        Remove layers from PDF

Using the code

Add layers to PDF

We can use the AddLayer method of PdfLayerCollection class to add layers to a PDF document. When adding the layers, we can set an initial visibility state for them.

using (PdfDocument pdf = new PdfDocument())


    //Add a page

    PdfPageBase page = pdf.Pages.Add();


    //Create a font

    PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 11f));


    //Add a layer and set its visibility to off

    PdfLayer layer = pdf.Layers.AddLayer("TextWaterMark"PdfVisibility.Off);

    PdfCanvas canvas1 = layer.CreateGraphics(pdf.Pages[0].Canvas);

    //Add text to layer

    canvas1.DrawString("TextWaterMark", font, PdfBrushes.Red, new PointF(100, 100));


    //Add a layer

    layer = pdf.Layers.AddLayer("BlueLine"PdfVisibility.On);

    PdfCanvas canvas2 = layer.CreateGraphics(pdf.Pages[0].Canvas);

    //Add a line shape to layer

    canvas2.DrawLine(new PdfPen(PdfBrushes.Blue, 1), new PointF(0, 150), new PointF(100, 150));


    //Add a layer

    layer = pdf.Layers.AddLayer("Rectangle"PdfVisibility.On);

    PdfCanvas canvas3 = layer.CreateGraphics(pdf.Pages[0].Canvas);

    //Add a rectangle shape to layer

    canvas3.DrawRectangle(PdfBrushes.SeaGreen, new RectangleF(0, 200, 100, 100));


    //Save the file



For adding images, we can use one of the overloads as following:

public void DrawImage(PdfImage image, PointF point, SizeF size); 

Show or hide Layers

PdfLayer.Visibility property is used to change the visibility of a PDF layer. To show a hidden layer, set the PdfLayer.Visibility property to On. To hide an existing layer, set the PdfLayer.Visibility to Off.

using (PdfDocument doc = new PdfDocument("AddLayers.pdf"))


    //Hide the layer by index

    doc.Layers[1].Visibility = PdfVisibility.Off;


    //Hide the layer by Name

    //doc.Layers["BlueLine"].Visibility = PdfVisibility.Off;


    //Save the file



To show or hide all of the layers:

using (PdfDocument doc = new PdfDocument("AddLayers.pdf"))


    for (int i = 0; i < doc.Layers.Count; i++)


        //Show all of the layers

        //doc.Layers[i].Visibility = PdfVisibility.On;



        //Hide all of the layers

        doc.Layers[i].Visibility = PdfVisibility.Off;


    //Save the file



Screenshot after hiding all layers:

Rename a layer

We can rename a layer using PdfLayer.Name property using the following line of code. This property also allows getting the name of the layer.

doc.Layers[0].Name = "New Name";


Remove Layers from PDF

RemoveLayer method is used to remove existing layers in a PDF document. When removing the layer, we can choose whether to remove the content of the layer or not.

//Remove a specific layer

using (PdfDocument doc = new PdfDocument("AddLayers.pdf"))


    //Remove layer



    //Remove layer but keep its content



    //Save the file




Remove all of the layers:

using (PdfDocument doc = new PdfDocument("AddLayers.pdf"))


    for (int i = doc.Layers.Count-1; i >= 0; i--)


        //Remove all layers



        //Remove all layers but keep contents

        doc.Layers.RemoveLayer(doc.Layers[i], false);


    //Save the file



Screenshot after remove all layers but keep contents:

Like this article? Bookmark and Share:

Most viewed Articles

User Comments

No response found, be the first to review this article.

Submit feedback about this article

Please sign in to post feedback

Latest Posts

1 XXX Tube Videos New! Forum