Articles in this section
Category / Section

How to add and remove the layers to and from an existing PDF Document?

1 min read

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:

http://www.syncfusion.com/downloads/support/directtrac/general/Layers-329890938.zip

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied