Developers, Don’t Miss This! The Ultimate .NET Guide To PDF Layers | Syncfusion Blogs
Detailed Blog page Skeleton loader
Developers, Don’t Miss This! The Ultimate .NET Guide To PDF Layers

TL;DR: PDF layers allow developers to organize content into toggleable sections for better control and clarity. This guide shows how to create nested layers, add annotations, and flatten them using Syncfusion’s .NET PDF Library in C#.

Developers often struggle with organizing complex PDF content, especially in technical drawings, multilingual documents, or interactive reports. PDF layers, also known as optional content groups, offer a powerful way to structure content visually and programmatically.

PDF generation and programmatic manipulation in C# have become essential for building robust document management, automation, and reporting solutions. As businesses demand richer documents, containing images, tables, forms, and digitally signed records, layers in PDF files are increasingly important for organizing and managing complex visual structures.

Syncfusion’s .NET PDF Library provides a comprehensive set of APIs to create, manipulate, and optimize PDF documents, including robust support for layer management. In this guide, we’ll explore how to use this powerful library to work with PDF layers in your C# applications. 

Agenda:

Let’s Get Started!

Getting started with app creation

Step 1: Create a .NET Core Console app

Open Visual Studio, select Create a New Project from the Start Page or File Menu, choose Console App (.NET Core), and follow the setup instructions.

Step 2: Open the NuGet Package Manager Console

Now, navigate to Visual Studio’s Tools menu, select NuGet Package Manager, and open the Package Manager Console.

Step 3: Install the Syncfusion® PDF NuGet Package

Run the following command to install the Syncfusion.Pdf.Net.Core package in the Package Manager Console.

Install-Package Syncfusion.Pdf.Net.Core

Understanding PDF Layers fundamentals

What are PDF Layers?

PDF layers are optional content groups that allow you to organize content into separate, controllable layers. Each layer can be independently shown or hidden, making them ideal for:

  • Technical drawings with different detail levels
  • Multilingual documents with language-specific content
  • Interactive maps with toggleable features
  • Form documents with conditional fields

Layer properties and behavior

PDF layers have several key properties:

  • Name: A descriptive identifier for the layer
  • Visibility: Default state (visible/hidden)
  • Print state: Whether the layer appears in the printed output
  • Export state: Whether the layer is included in exports

Creating your first PDF with Layers

Let’s start with a basic example that demonstrates how to create a PDF document with multiple layers:

using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;

// Create a new PDF document
using (PdfDocument document = new PdfDocument())
{
    //Set page margin to zero
    document.PageSettings.Margins.All = 0;
    // Add a new page to the document
    PdfPage page = document.Pages.Add();
    // Create the first layer for background content
    PdfPageLayer backgroundLayer = page.Layers.Add("Background");
    // Add background content
    PdfBrush backgroundBrush = new PdfSolidBrush(Color.LightBlue);
    backgroundLayer.Graphics.DrawRectangle(backgroundBrush, new RectangleF(0, 0, page.Size.Width, page.Size.Height));
    // Create a layer for main content
    PdfPageLayer mainContentLayer = page.Layers.Add("Main Content");
    // Add main content
    PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16);
    PdfBrush textBrush = new PdfSolidBrush(Color.Black);
    mainContentLayer.Graphics.DrawString("Welcome to PDF Layers!", font, textBrush, new PointF(50, 50));
    //S the document with layers
    document.Save("LayeredDocument.pdf");
}

Executing the above code will create a document with two separate layers: one containing the background brush and the other containing the main content of the page, as shown below.

PDF document with multiple layers
PDF document with multiple layers

Create a nested layer in a PDF document

Nested layers in a PDF allow for organized, hierarchical content that enhances user navigation and control over visibility. This feature is especially useful in complex documents like architectural plans, maps, or interactive reports. By grouping related layers under parent categories, users can toggle entire sections on or off, improving clarity and usability. This guide walks you through creating nested layers using PDF editing tools or scripting methods, ensuring your document is professional and user-friendly.

Create a nested layer using Syncfusion® .NET PDF library, as shown in the code below.

// Create a new PDF document
using (PdfDocument document = new PdfDocument())
{
    // Add a page to the document
    PdfPage page = document.Pages.Add();
    // Create parent layer group
    PdfLayer engineeringGroup = document.Layers.Add("Engineering");
    // Create child layers
    PdfLayer mechanicalLayer = engineeringGroup.Layers.Add("Mechanical");
    // Add content to the mechanical layer
    PdfGraphics mechanicalLayerGraphics = mechanicalLayer.CreateGraphics(page);
    mechanicalLayerGraphics.DrawRectangle(PdfBrushes.Blue, new RectangleF(50, 50, 100, 50));
    // Create child layers
    PdfLayer electricalLayer = engineeringGroup.Layers.Add("Electrical");
    // Add content to the electrical layer
    PdfGraphics electricalLayerGraphics = electricalLayer.CreateGraphics(page);
    electricalLayerGraphics.DrawEllipse(PdfBrushes.Yellow, new RectangleF(200, 50, 100, 50));
    // Create child layers
    PdfLayer plumbingLayer = engineeringGroup.Layers.Add("Plumbing");
    // Add content to plumbing layer
    PdfGraphics plumbingLayerGraphics = plumbingLayer.CreateGraphics(page);
    plumbingLayerGraphics.DrawLine(new PdfPen(Color.Green, 3), new PointF(50, 150), new PointF(300, 150));
    // Save the document to a file
    document.Save("NestedLayers.pdf");
}

By executing the above code, you will get the PDF with one top-level layer and three nested child layers, as illustrated below.

PDF with one top-level layer and three nested child layers
PDF with one top-level layer and three nested child layers

Adding annotations to Layers in a PDF

Combining annotations with PDF layers allows for more interactive and organized documents, especially when dealing with complex content like technical drawings or collaborative reviews. Using the Syncfusion® .NET PDF library, developers can assign annotations, such as text notes, highlights, or shapes, to specific layers. This enables users to toggle the visibility of annotations and their associated content, improving clarity and user control.

//Create new PDF document.
using (PdfDocument document = new PdfDocument())
{
    //Add page.
    PdfPage page = document.Pages.Add();
    //Draw some text.
    page.Graphics.DrawString(text, new PdfStandardFont(PdfFontFamily.Helvetica, 12), PdfBrushes.Black, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height));
    //Add the layer.
    PdfLayer layer = document.Layers.Add("Annotation Layer");    
    //Create square annotation.
    PdfSquareAnnotation annotation = new PdfSquareAnnotation(new RectangleF(200, 50, 50, 50), "Square annotation");
    //Set the annotation color.
    annotation.Color = new PdfColor(Syncfusion.Drawing.Color.Red);
    //Set layer to annotation.
    annotation.Layer = layer;
    //Add annotation to the created page.
    page.Annotations.Add(annotation);
    //Save the document
    document.Save("AnnotationOnLayer.pdf");
}

By executing the above code, you will get the output PDF as shown below.

Annotations layers in a PDF
Annotations layers in a PDF

Add layers to an existing PDF file

Adding layers to an existing PDF enhances its interactivity and organization, especially for documents that require multiple visual or informational elements. With the Syncfusion® .NET PDF library, developers can seamlessly insert new layers into an already created PDF file. This feature is useful for updating technical drawings, maps, or annotated documents without altering the original content. In this guide, you’ll learn how to integrate additional layers into a PDF, enabling better control over content visibility and structure.

Refer to the sample code below to integrate layers into an existing PDF document.

//Load the PDF document
using (var loadedDocument = new PdfLoadedDocument("data/input.pdf"))
{
    // Find the first text and create the parent layer
    loadedDocument.FindText("PDF Succinctly", 0, out var matchRect);
    PdfLayer? parentLayer = null;
    if (matchRect != null && matchRect.Count > 0)
    {
        parentLayer = AddLayer(loadedDocument, "PDF Succinctly", matchRect[0], PdfBrushes.Red, null);
        // Subsequent texts as children of the parent layer
        var childTexts = new[]
        {
            new { Text = "Introduction", Brush = PdfBrushes.Green },
            new { Text = "The PDF Standard", Brush = PdfBrushes.Blue }
        };
        foreach (var item in childTexts)
        {
            loadedDocument.FindText(item.Text, 0, out var childRect);
            if (childRect != null && childRect.Count > 0)
            {
                AddLayer(loadedDocument, item.Text, childRect[0], item.Brush, parentLayer);
            }
        }
    }
    loadedDocument.Save("AddLayers.pdf");
}

Create the AddLayer method as shown below.

PdfLayer AddLayer(PdfLoadedDocument loadedDocument, string layerName, RectangleF rect, PdfBrush brush, PdfLayer? parentLayer)
{
    var layer = parentLayer == null ? loadedDocument.Layers.Add(layerName) : parentLayer.Layers.Add(layerName);
    var graphics = layer.CreateGraphics(loadedDocument.Pages[0] as PdfLoadedPage);
    graphics.Save();
    graphics.SetTransparency(0.5f);
    graphics.DrawRectangle(brush, rect);
    graphics.Restore();
    return layer;
}

By executing the above code, you will get the output PDF file as shown below.

Adding layers to an existing PDF file
Adding layers to an existing PDF file

Advanced PDF Layer management techniques

Take full control of your PDF documents by customizing layer properties using the Syncfusion® .NET PDF library. This guide covers advanced techniques to manage visibility, print behavior, interactivity, and security of layers in a structured and efficient way.

  1. Setting Layer visibility

    Control whether a layer is visible when the PDF is opened. This helps in displaying only relevant content to the user by default.

    using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument("data/Layers.pdf"))
    {
        //Get layers
        PdfDocumentLayerCollection layerCollection = loadedDocument.Layers;
        if(layerCollection != null  && layerCollection.Count > 0)
        {
            //Get the first layer
            PdfLayer layer1= layerCollection[0];
            //Modify the visibility of the layer
            layer1.Visible = false;
        }
        // Save the document
        loadedDocument.Save("LayerCustomization.pdf");
    }
  2. Setting Layer print state

    Define whether a layer should be included when the document is printed. This is useful for excluding annotations or guides from printouts.

    // Set the print state of layer
    layer1.PrintState = PdfPrintState.PrintWhenVisible;
    
  3. Renaming Layers

    Update layer names to improve clarity and organization, especially in documents with multiple nested or related layers.

    //Get the second layer and rename it
    PdfLayer layer2= layerCollection[1];
    
    //Rename the layer
    layer2.Name = "Renamed Layer";
  4. Locking or unlocking Layers

    Secure layers by locking them to prevent editing or accidental changes, or unlock them when modifications are needed.

    //Get the layer3
    PdfLayer layer3 = layerCollection[2];
    
    //Lock the layer
    layer3.Locked = true;

By executing the above code, you will get the output PDF file as shown below.

PDF with customized layer visibility, print state, and locked layers
PDF with customized layer visibility, print state, and locked layers

Extracting PDF Layer information

Understanding the structure and properties of layers in a PDF is essential for effective document analysis and editing. With the Syncfusion® .NET PDF library, you can programmatically extract detailed information about each layer in a PDF file. This includes:

  • Layer name: Retrieve the name assigned to each layer for identification.
  • Visibility status: Check whether a layer is visible when opening the PDF.
  • Print state: Determine if a layer is set to appear in printed output.
  • Lock/enable state: Identify whether a layer is locked or enabled for user interaction.
//Load the PDF document
PdfLoadedDocument document = new PdfLoadedDocument("data/Layers.pdf");

Console.WriteLine($"Total layers: {document.Layers.Count}");

//Extract layer information
foreach (PdfLayer layer in document.Layers)
{    
    Console.WriteLine($"Layer Name: {layer.Name}");
    Console.WriteLine($"Visible: {layer.Visible}");
    Console.WriteLine($"Print: {layer.PrintState.ToString()}");
    Console.WriteLine($"Locked: {layer.Locked}");
    Console.WriteLine("---");
}

//Close the PDF file
document.Close(true);
Console showing PDF layer names, visibility, print, and lock status
Console showing PDF layer names, visibility, print, and lock status

By executing the above code, you will get the extracted information, as shown below.

Flatten or remove PDF Layers

Managing layers in a PDF includes creating and customizing them and removing or flattening them when needed. This guide covers advanced techniques for cleaning up or finalizing layered PDF documents using the Syncfusion® .NET PDF library.

  1. Flatten or remove Layers

    Flatten layers to merge their content into the main page, making it non-editable, or remove the layer structure while keeping the content intact.

    //Load the PDF document
    using (PdfLoadedDocument document = new PdfLoadedDocument("data/Layers.pdf"))
    {
        //Remove layers from the PDF document
        for (int i = document.Layers.Count - 1; i >= 0; i--)
        {
            document.Layers.RemoveAt(i);
        }
        //Save the modified PDF document
        document.Save("RemoveLayers.pdf");
        //Close the PDF file
        document.Close(true);
    }

    By executing the above code, you will get the output PDF file as shown below.

    Flatten or Remove PDF Layers
    Flatten or Remove PDF Layers
  2. Remove Layers with Graphics

    Completely delete the layer and its associated visual elements from the PDF, ensuring no trace remains.

    //Load the PDF document
    using (PdfLoadedDocument document = new PdfLoadedDocument("data/Layers.pdf"))
    {
        //Remove layers from the PDF document
        for (int i = document.Layers.Count - 1; i >= 0; i--)
        {
            document.Layers.RemoveAt(i, true);
        }
        //Save the modified PDF document
        document.Save("RemoveLayerWithGraphics.pdf");
        //Close the PDF file
        document.Close(true);
    }
    PDF file with removed graphic layers
    PDF file with removed graphic layers

Real-world use cases and examples

PDF layers are powerful tools for organizing complex content in technical and professional documents. Using the Syncfusion® .NET PDF library, developers can create layered PDFs tailored to specific use cases such as multilingual support, technical drawings, and architectural plans. Below are practical examples and code snippets to help you implement these features.

  1. Creating a multilingual document

    Use layers to separate content by language, allowing users to toggle visibility based on their preferred language.

    Use case:

    Ideal for international manuals, brochures, or legal documents where multiple languages need to coexist without clutter.

    Example:
    using (PdfDocument document = new PdfDocument())
    {
        // Create a new PDF document
        PdfPage page = document.Pages.Add();
        // Create a font for the text
        PdfTrueTypeFont font = new PdfTrueTypeFont("data/arial.ttf", 12);
        // Create language-specific layers
        PdfLayer englishLayer = document.Layers.Add("English");
        // Add English text to the English layer
        AddTextToLayer(page, englishLayer, englishText, font);
        // Add Spanish text to the Spanish layer
        PdfLayer spanishLayer = document.Layers.Add("Español");
        spanishLayer.Visible = false;
        // Add Spanish text to the Spanish layer
        AddTextToLayer(page, spanishLayer, spanishText, font);
        // Add French text to the French layer
        PdfLayer frenchLayer = document.Layers.Add("Français");
        frenchLayer.Visible = false;
        // Add French text to the French layer
        AddTextToLayer(page, frenchLayer, frenchText, font);
        //Save the document to a file
        document.Save("MultilingualLayers.pdf");
    }
    
    
    static void AddTextToLayer(PdfPage page, PdfLayer layer, string text, PdfFont font)
    {
        // Create a graphics object for the layer
        PdfGraphics graphics = layer.CreateGraphics(page);
        // Draw the text on the specified layer
        graphics.DrawString(text, font, PdfBrushes.Black, new RectangleF(0, 0, page.GetClientSize().Width, 200));
    }

    By executing the above code, you will get the output document as follows,

    Multilingual Document
    Multilingual document
  2. Technical drawing with detail levels

    Organize technical drawings into layers based on detail levels (e.g., basic layout, electrical, plumbing).

    Use case:
    Useful for engineers and architects who need to present different aspects of a design without overwhelming the viewer.

    Example:
    // Create a new PDF document for technical drawing
    using (PdfDocument document = new PdfDocument())
    {
        // Add a new page to the document
        PdfPage page = document.Pages.Add();
        // Basic layout layer
        PdfPageLayer layoutLayer = page.Layers.Add("Basic Layout");
        layoutLayer.Graphics.DrawRectangle(PdfBrushes.Gray, new RectangleF(50, 50, 200, 100));
        // Electrical layer
        PdfPageLayer electricalLayer = page.Layers.Add("Electrical");
        electricalLayer.Graphics.DrawLine(new PdfPen(Color.Red, 2), new PointF(60, 60), new PointF(180, 60));
        // Plumbing layer
        PdfPageLayer plumbingLayer = page.Layers.Add("Plumbing");
        plumbingLayer.Graphics.DrawEllipse(PdfBrushes.Blue, new RectangleF(70, 70, 50, 50));
        // Save document
        document.Save("TechnicalDrawing.pdf");
        document.Close(true);
    }
  3. CAD drawings

    Use layers to separate structural, mechanical, and annotation elements for better control and clarity.

  4. Floor mappings

    Create layers for each floor in a building plan, allowing users to toggle between levels easily.

GitHub reference

You can find the complete sample project and all the code examples from this blog post in our GitHub demo.

Conclusion

PDF layers unlock a new level of control for developers working with dynamic documents. It provides immense flexibility for document control. Whether you’re building complex engineering diagrams, multilingual manuals, or interactive forms, mastering layers with Syncfusion’s .NET PDF Library will elevate your PDF workflows. 

With the Syncfusion® .NET PDF library, managing layers, creating nested structures, adding annotations, or flattening content becomes seamless and scalable. We encourage you to explore the Syncfusion® PDF Library’s documentation to discover advanced features and best practices for manipulating PDFs programmatically.

If you have any questions or need assistance, comment below or contact us through our support forumsupport portal, or feedback portal. We are always here to assist you!

Related blogs

Be the first to get updates

Chinnu MuniyappanChinnu Muniyappan profile icon

Meet the Author

Chinnu Muniyappan

Chinnu Muniyappan is a Product Manager at Syncfusion, managing the development and delivery of the PDF library. With experience in .NET development since 2014, he focuses on enhancing PDF solutions across multiple platforms.