Click or drag to resize
Pdf Library for .NET

PDF Layers

Layers (optional content) are parts of a PDF document a viewer can show or hide, as in Acrobat's Layers panel: watermarks, translations, draft marks, the levels of a technical drawing. SelectPdf Library for .NET adds layers to new documents and draws content in them, and reads and changes the layers of loaded documents, through Layers.

Content in layers
  • Add a layer with Add, shown or hidden when the document opens; a layer's own Layers nest layers under it.

  • Draw any element in a layer with Layer: text (on every page it runs onto), images, shapes, html and Word regions, and links and other annotations, which then show and hide with the layer too.

  • Put a whole header, footer or stamp in a layer with Layer: everything added to the template afterwards is drawn in it, unless an element names a layer of its own.

  • Decide when a layer prints with PrintState (when visible, always or never), and its place in a viewer's list with Move.

A layered element keeps its tags in an accessible document. PDF/A-2 and later and PDF/UA allow layers, with the rule that a print state is declared but does not act on its own there (the standards forbid the setting that makes it automatic); PDF/A-1 does not allow layers, and adding one to such a document is refused.

PdfDocument document = new PdfDocument();
PdfLayer draft = document.Layers.Add("Draft");
// hidden when the document opens
PdfLayer notes = document.Layers.Add("Review notes", false);
notes.PrintState = PdfLayerPrintState.NeverPrint;

PdfFont font = document.Fonts.Add(PdfStandardFont.Helvetica);
PdfPage page = document.AddPage();
page.Add(new PdfTextElement(50, 50, "DRAFT", font) { Layer = draft });
page.Add(new PdfTextElement(50, 100, "Check the totals", font) { Layer = notes });

PdfTemplate header = document.Templates.Add(500, 40);
header.Layer = document.Layers.Add("Header");
header.Add(new PdfTextElement(0, 10, "Quarterly report", font));
document.Templates.Header = header;

document.Save("layers.pdf");
document.Close();
The layers of a loaded document
  • List the layers in a viewer's order; the layers nested under one are in its Layers.

  • Show or hide a layer when the document opens (Visible), lock it (Locked), rename it (Name).

  • Add a layer, and move page objects into it with Layer, or draw new elements in it as above.

  • Remove a layer with what it draws, or keep its content as plain, always visible page content; Flatten does that for every layer.

Layers are a Full edition feature.

Sample Code
PdfDocument document = new PdfDocument("drawing.pdf");
foreach (PdfLayer layer in document.Layers)
    Console.WriteLine(layer.Name + (layer.Visible ? "" : " (hidden)"));

// hide the dimensions, drop the draft marks with what they draw
document.Layers["Dimensions"].Visible = false;
document.Layers.Remove(document.Layers["Draft"], true);

// put the page's images on a new layer
PdfLayer pictures = document.Layers.Add("Pictures");
foreach (PdfImageObject image in document.Pages[0].ObjectModel.GetImageObjects())
    image.Layer = pictures;

document.Save("out.pdf");
document.Close();
See Also