News Category

Show or Hide PDF Layers in C#

2017-10-30 08:02:54 Written by  support iceblue
Rate this item
(0 votes)

When creating a PDF layer, Spire.PDF allows developers to set an initial visibility state for the layer. While it also supports to change the visibility of existing layers in a PDF document. This article explains how to show or hide the existing layers using Spire.PDF.

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

The following example shows how to hide a specific PDF layers:

using Spire.Pdf;
using Spire.Pdf.Graphics.Layer;

namespace HideLayer
{
    class Program
    {
        static void Main(string[] args)
        {
            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
                doc.SaveToFile("HideLayer.pdf");
            }
        }
    }
}

To show or hide all of the layers:

using Spire.Pdf;
using Spire.Pdf.Graphics.Layer;

namespace ShowLayer
{
    class Program
    {
        static void Main(string[] args)
        {
            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
                doc.SaveToFile("HideAllLayers.pdf");
            }
        }
    }
}

Screeshot of the sample PDF document:

Show or Hide PDF Layers in C#

Screenshot after hiding all of the layers:

Show or Hide PDF Layers in C#

Additional Info

  • tutorial_title:
Last modified on Sunday, 26 September 2021 01:42