How to add layers to PDF file in C#

How to add layers to PDF file in C#

2015-11-16 09:05:26 Written by  Administrator
Rate this item
(0 votes)

Developers can use PDF layer to set some content to be visible and others to be invisible in the same PDF file. It makes the PDF Layer widely be used to deal with related contents within the same PDF. Now developers can easily add page layers by using class PdfPageLayer offered by Spire.PDF. This article will focus on showing how to add layers to a PDF file in C# with the help of Spire.PDF.

Note: Before Start, please download the latest version of Spire.PDF and add Spire.PDF.dll in the bin folder as the reference of Visual Studio.

Here comes to the details:

Step 1: Create a new PDF document

PdfDocument pdfdoc = new PdfDocument();

Step 2: Add a new page to the PDF document.

PdfPageBase page = pdfdoc.Pages.Add();

Step 3: Add a layer named "red line" to the PDF page.

 PdfLayer layer = doc.Layers.AddLayer("red line", PdfVisibility.On);

Step 4: Draw a red line to the added layer.

// Create a graphics context for drawing on the specified page's canvas using the created layer
 PdfCanvas pcA = layer.CreateGraphics(page.Canvas);

 // Draw a red line on the graphics context using a pen with thickness 2, starting from (100, 350) to (300, 350)
 pcA.DrawLine(new PdfPen(PdfBrushes.Red, 2), new PointF(100, 350), new PointF(300, 350));

Step 5: Use the same method above to add the other two layers to the PDF page.

layer = doc.Layers.AddLayer("blue line");
PdfCanvas pcB = layer.CreateGraphics(doc.Pages[0].Canvas);
pcB.DrawLine(new PdfPen(PdfBrushes.Blue, 2), new PointF(100, 400), new PointF(300, 400));
layer = doc.Layers.AddLayer("green line");
PdfCanvas pcC = layer.CreateGraphics(doc.Pages[0].Canvas);
pcC.DrawLine(new PdfPen(PdfBrushes.Green, 2), new PointF(100, 450), new PointF(300, 450));

Step 6: Save the document to file.

pdfdoc.SaveToFile("AddLayers.pdf", FileFormat.PDF);

Effective screenshot:

How to add layers to PDF file in C#

Full codes:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace AddLayer
{
    class Program
    {
        static void Main(string[] args)
        {
  // Create a new PdfDocument object
  PdfDocument doc = new PdfDocument();

  // Load an existing PDF document from the specified file path
  doc.LoadFromFile(@"..\..\..\..\..\..\Data\AddLayers.pdf");

  // Get the first page of the loaded document
  PdfPageBase page = doc.Pages[0];

  // Create a new layer named "red line" with visibility set to "On"
  PdfLayer layer = doc.Layers.AddLayer("red line", PdfVisibility.On);

  // Create a graphics context for drawing on the specified page's canvas using the created layer
  PdfCanvas pcA = layer.CreateGraphics(page.Canvas);

  // Draw a red line on the graphics context using a pen with thickness 2, starting from (100, 350) to (300, 350)
  pcA.DrawLine(new PdfPen(PdfBrushes.Red, 2), new PointF(100, 350), new PointF(300, 350));

  // Create a new layer named "blue line" without specifying visibility (default is "Off")
  layer = doc.Layers.AddLayer("blue line");

  // Create a graphics context for drawing on the first page's canvas using the newly created layer
  PdfCanvas pcB = layer.CreateGraphics(doc.Pages[0].Canvas);

  // Draw a blue line on the graphics context using a pen with thickness 2, starting from (100, 400) to (300, 400)
  pcB.DrawLine(new PdfPen(PdfBrushes.Blue, 2), new PointF(100, 400), new PointF(300, 400));

  // Create a new layer named "green line" without specifying visibility (default is "Off")
  layer = doc.Layers.AddLayer("green line");

  // Create a graphics context for drawing on the first page's canvas using the newly created layer
  PdfCanvas pcC = layer.CreateGraphics(doc.Pages[0].Canvas);

  // Draw a green line on the graphics context using a pen with thickness 2, starting from (100, 450) to (300, 450)
  pcC.DrawLine(new PdfPen(PdfBrushes.Green, 2), new PointF(100, 450), new PointF(300, 450));

  // Specify the output file name for the modified PDF
  string output = "AddLayers.pdf";

  // Save the modified PDF document to the specified output file
  doc.SaveToFile(output);
        }
    }
}

Additional Info

  • tutorial_title: Add layers to PDF file in C#
Last modified on Tuesday, 30 December 2025 08:58