PDF layer is a feature that arranges the content of a PDF file in layers, which allows users to selectively set some content to be visible and others to be invisible in the same PDF file. PDF layers are a common element used in layered artwork, maps and CAD drawings. This article will demonstrate how to programmatically add, hide or delete layers in a PDF file using Spire.PDF for .NET.
- Add Layers to a PDF Document in C# and VB.NET
- Set Visibility of Layers in a PDF Document in C# and VB.NET
- Delete Layers in a PDF Document in C# and VB.NET
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Add Layers to a PDF Document in C# and VB.NET
Spire.PDF for .NET provides PdfDocument.Layers.AddLayer() method to add a layer in a PDF document, and you can then draw text, lines, images or shapes on the PDF layer. The detailed steps are as follows.
- Create a PdfDocument instance.
- Load a sample PDF file using PdfDocument.LoadFromFile() method.
- Add a layer with specified name in the PDF using PdfDocument.Layers.AddLayer(String) method. Or you can also set the visibility of the layer while adding it using PdfDocument.Layers.AddLayer(String, PdfVisibility) method.
- Create a canvas for the layer using PdfLayer.CreateGraphics() method.
- Draw text, image or other elements on the canvas.
- Save the result document using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf; using Spire.Pdf.Graphics; using Spire.Pdf.Graphics.Layer; using System.Drawing; namespace AddLayersToPdf { class Program { static void Main(string[] args) { //Create a PdfDocument instance and load a sample PDF file PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.pdf"); //Invoke AddLayerWatermark method to add a watermark layer AddLayerWatermark(pdf); //Invoke AddLayerHeader method to add a header layer AddLayerHeader(pdf); //Save to file pdf.SaveToFile("AddLayers.pdf"); pdf.Close(); } private static void AddLayerWatermark(PdfDocument doc) { //Create a layer named "Watermark" PdfLayer layer = doc.Layers.AddLayer("Watermark"); //Create a font PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 48), true); //Specify the watermark text string watermarkText = "CONFIDENTIAL"; //Get text size SizeF fontSize = font.MeasureString(watermarkText); //Calculate two offsets float offset1 = (float)(fontSize.Width * System.Math.Sqrt(2) / 4); float offset2 = (float)(fontSize.Height * System.Math.Sqrt(2) / 4); //Get page count int pageCount = doc.Pages.Count; //Declare two variables PdfPageBase page; PdfCanvas canvas; //Loop through the pages for (int i = 0; (i < pageCount); i++) { page = doc.Pages[i]; //Create a canvas from layer canvas = layer.CreateGraphics(page.Canvas); canvas.TranslateTransform(page.Canvas.Size.Width / 2 - offset1 - offset2, page.Canvas.Size.Height / 2 + offset1 - offset2); canvas.SetTransparency(0.4f); canvas.RotateTransform(-45); //Draw sting on the canvas of layer canvas.DrawString(watermarkText, font, PdfBrushes.DarkBlue, 0, 0); } } private static void AddLayerHeader(PdfDocument doc) { // Create a layer named "Header" PdfLayer layer = doc.Layers.AddLayer("Header"); //Get page size SizeF size = doc.Pages[0].Size; //Specify the initial values of X and y float x = 90; float y = 40; //Get page count int pageCount = doc.Pages.Count; //Declare two variables PdfPageBase page; PdfCanvas canvas; //Loop through the pages for (int i = 0; (i < pageCount); i++) { //Draw an image on the layer PdfImage pdfImage = PdfImage.FromFile(@"C:\Users\Administrator\Desktop\img.jpg"); float width = pdfImage.Width; float height = pdfImage.Height; page = doc.Pages[i]; canvas = layer.CreateGraphics(page.Canvas); canvas.DrawImage(pdfImage, x, y, width, height); //Draw a line on the layer PdfPen pen = new PdfPen(PdfBrushes.DarkGray, 2); canvas.DrawLine(pen, x, (y + (height + 5)), (size.Width - x), (y + (height + 2))); } } } }
Set Visibility of Layers in a PDF Document in C# and VB.NET
To set the visibility of an existing layer, you'll need to get a specified layer by its index or name using PdfDocument.Layers property, and then show or hide the layer using PdfLayer.Visibility property. The detailed steps are as follows.
- Create a PdfDocument instance.
- Load a sample PDF document using PdfDocument.LoadFromFile() method.
- Set the visibility of a specified layer using PdfDocument.Layers.Visibility property.
- Save the result document using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf; using Spire.Pdf.Graphics.Layer; namespace HideLayer { class Program { static void Main(string[] args) { //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); //Load a sample PDF document pdf.LoadFromFile("AddLayers.pdf"); //Hide a specified layer by index pdf.Layers[0].Visibility = PdfVisibility.Off; //Hide a specified layer by name //pdf.Layers["Watermark"].Visibility = PdfVisibility.Off; //Save the result document pdf.SaveToFile("HideLayer.pdf"); } } }
Delete Layers in a PDF Document in C# and VB.NET
Spire.PDF for .NET also allows you to remove an existing layer by its name using PdfDocument.Layers.RemoveLayer(String) method. But kindly note that the names of PDF layers may not be unique and this method will remove all PDF layers with the same name. The detailed steps are as follows.
- Create a PdfDocument instance.
- Load a sample PDF document using PdfDocument.LoadFromFile() method.
- Delete a specified layer by its name using PdfDocument.Layers.RemoveLayer(String) method.
- Save the result document using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf; namespace DeleteLayer { class Program { static void Main(string[] args) { //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); //Load a sample PDF document pdf.LoadFromFile("AddLayers.pdf"); //Remove a layer by name pdf.Layers.RemoveLayer(("Watermark")); //Save the result document pdf.SaveToFile("DeleteLayer.pdf", FileFormat.PDF); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.