With the help of Spire.PDF, we can add several kinds of layers such as line, image, string, ellipse, rectangle and pie to any page of a new or an existing pdf document. At the same time, it also supports us to delete specific layer from a pdf document.
In this section, we're going to demonstrate how to delete layer in PDF using Spire.PDF for .NET. To add layer to PDF, please check this article: How to add layers to PDF file in C#.
Below is the screenshot of the original PDF document which contains three layers: a red line layer and two image layers.
Before start, download Spire.PDF and install it correctly, next add the corresponding dll file from the installation folder as reference of your project.
Detail steps:
Step 1: Initialize a new instance of PdfDocument class and load the sample document from file.
PdfDocument doc = new PdfDocument(); doc.LoadFromFile("AddLayer.pdf");
Step 2: Get its first page and delete the specific layer by name from page one.
PdfPageBase page = doc.Pages[0]; page.PageLayers.DeleteOldLayer("red line");
Step 3: Save and launch the file.
doc.SaveToFile("delete.pdf"); System.Diagnostics.Process.Start("delete.pdf");
Effective screenshot after deleting:
Full codes:
using Spire.Pdf; namespace Delete_page_layer_in_PDF { class Program { static void Main(string[] args) { PdfDocument doc = new PdfDocument(); doc.LoadFromFile("AddLayer.pdf"); PdfPageBase page = doc.Pages[0]; page.PageLayers.DeleteOldLayer("red line"); doc.SaveToFile("delete.pdf"); System.Diagnostics.Process.Start("delete.pdf"); } } }