How to add and remove the layers to and from an existing PDF Document?
Layers are collection of graphics that you can make visible or invisible according to your preference. You can add layers from the existing PDF Document by loading the layer using PdfLoadedPage. PdfLoadedPage contains its own PdfPageLayerCollection. From the PdfLoadedPage, add the layers by the name using Add method. In the same way you can remove the layers by its name using Remove method.
C#
Add Layers: //Loads the Pdf document PdfLoadedDocument doc = new PdfLoadedDocument("file.pdf"); //Gets the particular page from loaded pages. PdfLoadedPage page = doc.Pages[0] as PdfLoadedPage; //Gets the Layers of particular loaded page PdfPageLayerCollection coll = doc.Pages[0].Layers; //Add the first layer PdfPageLayer layer = page.Layers.Add("Layer1"); layer.PrintState = PdfPrintState.NeverPrint; //Layer1 graphics PdfGraphics graphics = layer.Graphics; //Applying translation graphics.TranslateTransform(100, 60); //Draw Arc PdfPen pen = new PdfPen(System.Drawing.Color.Red, 50); RectangleF rect = new RectangleF(0, 0, 50, 50); graphics.DrawArc(pen, rect, 360, 360); pen = new PdfPen(System.Drawing.Color.Blue, 30); graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360); //Add another layer on the page PdfPageLayer layer2 = page.Layers.Add("Layer2"); //Layers 2 graphics graphics = layer2.Graphics; graphics.TranslateTransform(100, 180); graphics.SkewTransform(0, 50); //Draw another set of elements pen = new PdfPen(System.Drawing.Color.Red, 50); graphics.DrawArc(pen, rect, 360, 360); pen = new PdfPen(System.Drawing.Color.Blue, 30); graphics.DrawArc(pen, 0, 0, 50, 50, 360, 360); //Add another layer PdfPageLayer layer3 = page.Layers.Add("Layer3"); // Layer 3 graphics graphics = layer3.Graphics; graphics.TranslateTransform(160, 120); //Draw another set of elements. pen = new PdfPen(System.Drawing.Color.Red, 50); graphics.DrawArc(pen, rect, -60, 60); pen = new PdfPen(System.Drawing.Color.Blue, 30); graphics.DrawArc(pen, 0, 0, 50, 50, -60, 60); pen = new PdfPen(System.Drawing.Color.Yellow, 20); //Save to disk doc.Save("Sample.pdf"); doc.Close(); Remove Layers: PdfLoadedDocument doc = new PdfLoadedDocument("sample.pdf"); //Gets the layers of particular loaded page PdfPageLayerCollection coll = doc.Pages[0].Layers; //Removes the layer using Name coll.Remove("Layer1");
Sample Link:
https://www.syncfusion.com/downloads/support/directtrac/general/Layers-329890938.zip
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion:
I hope you enjoyed learning about how to add and remove the layers to and from an existing PDF Document.
You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to explore our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!